I am using the lettre library and this is my rust code ..
#[derive(Debug, Clone)]
pub struct EmailService {
mailer: AsyncSmtpTransport<Tokio1Executor>,
from_email: String,
from_name: String,
}
impl EmailService {
pub fn init_service(config: &SmtpConfig) -> ServiceResult<Self> {
let creds = Credentials::new(
config.get_username().to_string(),
config.get_password().to_string(),
);
let mailer = AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(config.get_host())?
.port(config.get_port())
.credentials(creds)
.build();
Ok(Self {
from_email: config.get_from_email().to_string(),
from_name: config.get_from_name().to_string(),
mailer,
})
}
pub async fn send_email(&self, req: SendEmailRequest) -> ServiceResult<()> {
let email = Message::builder()
.from(format!("{0} <{1}>", self.from_name, self.from_email).parse()?)
.to(req.to.parse()?)
.subject(req.subject)
.singlepart(
SinglePart::builder()
.header(header::ContentType::TEXT_HTML)
.body(req.body),
)?;
let _result = self.mailer.send(email).await?;
Ok(())
}
}
}
I am using this service in Axum.. please help me.