read SMTP configuration

This commit is contained in:
realaravinth
2021-06-29 21:08:40 +05:30
parent 481cb95cd2
commit 804c81da38
6 changed files with 391 additions and 16 deletions

77
src/email.rs Normal file
View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// The html we want to send.
const HTML: &str = r#"<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello from Lettre!</title>
</head>
<body>
<div style="display: flex; flex-direction: column; align-items: center;">
<h2 style="font-family: Arial, Helvetica, sans-serif;">Hello from Lettre!</h2>
<h4 style="font-family: Arial, Helvetica, sans-serif;">A mailer library for Rust</h4>
</div>
</body>
</html>"#;
use lettre::{
message::{header, MultiPart, SinglePart},
transport::smtp::authentication::Credentials,
AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor,
};
async fn email() {
let email = Message::builder()
.from("NoBody <nobody@domain.tld>".parse().unwrap())
.reply_to("Yuin <yuin@domain.tld>".parse().unwrap())
.to("Hei <hei@domain.tld>".parse().unwrap())
.subject("Happy new async year")
.multipart(
MultiPart::alternative() // This is composed of two parts.
.singlepart(
SinglePart::builder()
.header(header::ContentType::TEXT_PLAIN)
.body(String::from(
"Hello from Lettre! A mailer library for Rust",
)), // Every message should have a plain text fallback.
)
.singlepart(
SinglePart::builder()
.header(header::ContentType::TEXT_HTML)
.body(String::from(HTML)),
),
)
.unwrap();
let creds =
Credentials::new("smtp_username".to_string(), "smtp_password".to_string());
let mailer: AsyncSmtpTransport<Tokio1Executor> =
AsyncSmtpTransport::<Tokio1Executor>::relay("smtp.gmail.com")
.unwrap()
.credentials(creds)
.build();
// Send the email
match mailer.send(email).await {
Ok(_) => println!("Email sent successfully!"),
Err(e) => panic!("Could not send email: {:?}", e),
}
}

View File

@@ -28,6 +28,7 @@ use log::info;
mod api;
mod data;
mod docs;
//mod email;
mod errors;
mod middleware;
#[macro_use]

View File

@@ -38,6 +38,14 @@ pub struct Captcha {
pub gc: u64,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Smtp {
pub from: String,
pub url: String,
pub username: String,
pub password: String,
}
impl Server {
#[cfg(not(tarpaulin_include))]
pub fn get_ip(&self) -> String {
@@ -93,6 +101,7 @@ pub struct Settings {
pub server: Server,
pub pow: Captcha,
pub source_code: String,
pub smtp: Option<Smtp>,
}
#[cfg(not(tarpaulin_include))]
@@ -206,4 +215,12 @@ fn set_database_url(s: &mut Config) {
// settings.server.check_url_prefix();
// assert!(settings.server.url_prefix.is_none());
// }
//
// #[test]
// fn smtp_config_works() {
// let settings = Settings::new().unwrap();
// assert!(settings.smtp.is_some());
// assert_eq!(settings.smtp.as_ref().unwrap().password, "password");
// assert_eq!(settings.smtp.as_ref().unwrap().username, "admin");
// }
//}