Initial Implementation of EmailService and Ignores EnvFile
This commit is contained in:
25
src/config/config_email.rs
Normal file
25
src/config/config_email.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
use cached::proc_macro::cached;
|
||||
use std::env;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ConfigEmail {
|
||||
pub smtp_server: String,
|
||||
pub smtp_port: u16,
|
||||
pub smtp_username: String,
|
||||
pub smtp_password: String,
|
||||
}
|
||||
|
||||
#[cached]
|
||||
pub fn get_config_email() -> ConfigEmail {
|
||||
let server = env::var("SMTP_SERVER").expect("SMTP_SERVER must be set");
|
||||
let port = env::var("SMTP_PORT").expect("SMTP_PORT must be set");
|
||||
let username = env::var("SMTP_USERNAME").expect("SMTP_USERNAME must be set");
|
||||
let password = env::var("SMTP_PASSWORD").expect("SMTP_PASSWORD must be set");
|
||||
|
||||
ConfigEmail{
|
||||
smtp_server: server,
|
||||
smtp_port: port.parse::<u16>().unwrap(),
|
||||
smtp_username: username,
|
||||
smtp_password: password,
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod config_auth;
|
||||
pub mod config_server;
|
||||
pub mod config_email;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::config::config_auth;
|
||||
use crate::config::{config_auth, config_email};
|
||||
use crate::handler::health::health_check;
|
||||
use crate::handler::message::send_message;
|
||||
use crate::middleware::auth_middleware::auth_middleware;
|
||||
@@ -16,7 +16,7 @@ fn configure_message_endpoint(router: Router) -> Router {
|
||||
.route("/message", post(send_message))
|
||||
.layer(middleware::from_fn(auth_middleware))
|
||||
.layer(Extension(AuthService::new(config_auth::get_config_auth())))
|
||||
.layer(Extension(EmailService::new()))
|
||||
.layer(Extension(EmailService::new(config_email::get_config_email())))
|
||||
}
|
||||
|
||||
fn configure_health_endpoint(router: Router) -> Router {
|
||||
|
||||
@@ -1,8 +1,41 @@
|
||||
use lettre::{
|
||||
transport::smtp::authentication::Credentials, AsyncSmtpTransport, AsyncTransport, Message,
|
||||
Tokio1Executor,
|
||||
};
|
||||
use crate::config::config_email::ConfigEmail;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct EmailService {}
|
||||
pub struct EmailService {
|
||||
mailer: AsyncSmtpTransport<Tokio1Executor>
|
||||
}
|
||||
|
||||
impl EmailService {
|
||||
pub fn new() -> Self {
|
||||
EmailService {}
|
||||
pub fn new(config_email: ConfigEmail) -> Self {
|
||||
EmailService {
|
||||
mailer: AsyncSmtpTransport::<Tokio1Executor>::relay(&config_email.smtp_server)
|
||||
.unwrap()
|
||||
.credentials(Credentials::new(config_email.smtp_username, config_email.smtp_password))
|
||||
.port(config_email.smtp_port)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_email_smtp(
|
||||
&self,
|
||||
from: &str,
|
||||
to: &str,
|
||||
subject: &str,
|
||||
body: String,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let email = Message::builder()
|
||||
.from(from.parse()?)
|
||||
.to(to.parse()?)
|
||||
.subject(subject)
|
||||
.body(body.to_string())?;
|
||||
|
||||
self.mailer.send(email).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user