use serde::{Deserialize, Serialize}; use lettre::{ message::Message, transport::smtp::authentication::Credentials, SmtpTransport, Transport, }; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct EmailConfig { pub smtp_host: String, pub smtp_port: u16, pub smtp_username: String, pub smtp_password: String, pub from_address: String, pub from_name: Option, #[serde(default)] pub use_tls: bool, } pub struct EmailSender { config: EmailConfig, } impl EmailSender { pub fn new(config: EmailConfig) -> Self { Self { config } } pub fn send_email( &self, to: &str, subject: &str, body: &str, ) -> crate::error::Result<()> { let email = Message::builder() .from( format!( "{} <{}>", self.config.from_name.as_deref().unwrap_or("Telfax"), self.config.from_address ) .parse() .unwrap(), ) .to(to.parse().unwrap()) .subject(subject) .body(body.to_string()) .unwrap(); let creds = Credentials::new( self.config.smtp_username.clone(), self.config.smtp_password.clone(), ); let mailer = SmtpTransport::relay(&self.config.smtp_host) .unwrap() .credentials(creds) .port(self.config.smtp_port) .build(); mailer.send(&email) .map_err(|e| crate::error::FaxError::Other(format!("Failed to send email: {}", e)))?; Ok(()) } pub fn send_success_notification( &self, to: &str, fax_number: &str, job_id: &str, ) -> crate::error::Result<()> { let subject = format!("Fax sent successfully to {}", fax_number); let body = format!( "Your fax to {} has been sent successfully.\n\nJob ID: {}\n\nThis is an automated message from Telfax Fax Server.", fax_number, job_id ); self.send_email(to, &subject, &body) } pub fn send_failure_notification( &self, to: &str, fax_number: &str, job_id: &str, error: &str, ) -> crate::error::Result<()> { let subject = format!("Fax failed to {}", fax_number); let body = format!( "Your fax to {} has failed.\n\nJob ID: {}\nError: {}\n\nPlease check the fax number and try again.\n\nThis is an automated message from Telfax Fax Server.", fax_number, job_id, error ); self.send_email(to, &subject, &body) } pub fn send_received_notification( &self, to: &str, from_number: &str, ) -> crate::error::Result<()> { let subject = format!("Fax received from {}", from_number); let body = format!( "You have received a fax from {}.\n\nPlease check the fax server for the attachment.\n\nThis is an automated message from Telfax Fax Server.", from_number ); self.send_email(to, &subject, &body) } }