feat: separate smtp username from email

This commit is contained in:
Zack Fu Zi Xiang 2024-11-14 16:00:15 +08:00
parent f6fef9918b
commit 4b2eb1b1ba
No known key found for this signature in database
9 changed files with 24 additions and 9 deletions

View File

@ -113,6 +113,7 @@ APPFLOWY_S3_BUCKET=appflowy
APPFLOWY_MAILER_SMTP_HOST=smtp.gmail.com
APPFLOWY_MAILER_SMTP_PORT=465
APPFLOWY_MAILER_SMTP_USERNAME=email_sender@some_company.com
APPFLOWY_MAILER_SMTP_EMAIL=email_sender@some_company.com
APPFLOWY_MAILER_SMTP_PASSWORD=email_sender_password
# Log level for the appflowy-cloud service

View File

@ -88,6 +88,7 @@ APPFLOWY_S3_BUCKET=appflowy
# Note that smtps (TLS) is always required, even for ports other than 465
APPFLOWY_MAILER_SMTP_HOST=smtp.gmail.com
APPFLOWY_MAILER_SMTP_USERNAME=notify@appflowy.io
APPFLOWY_MAILER_SMTP_EMAIL=notify@appflowy.io
APPFLOWY_MAILER_SMTP_PASSWORD=email_sender_password
RUST_LOG=info

View File

@ -5,5 +5,6 @@ pub struct MailerSetting {
pub smtp_host: String,
pub smtp_port: u16,
pub smtp_username: String,
pub smtp_email: String,
pub smtp_password: Secret<String>,
}

View File

@ -5,21 +5,23 @@ use lettre::transport::smtp::authentication::Credentials;
use lettre::Address;
use lettre::AsyncSmtpTransport;
use lettre::AsyncTransport;
use secrecy::ExposeSecret;
#[derive(Clone)]
pub struct Mailer {
smtp_transport: AsyncSmtpTransport<lettre::Tokio1Executor>,
smtp_username: String,
smtp_email: String,
handlers: Handlebars<'static>,
}
impl Mailer {
pub async fn new(
smtp_username: String,
smtp_password: String,
smtp_email: String,
smtp_password: secrecy::Secret<String>,
smtp_host: &str,
smtp_port: u16,
) -> Result<Self, anyhow::Error> {
let creds = Credentials::new(smtp_username.clone(), smtp_password);
let creds = Credentials::new(smtp_username, smtp_password.expose_secret().to_string());
let smtp_transport = AsyncSmtpTransport::<lettre::Tokio1Executor>::relay(smtp_host)?
.credentials(creds)
.port(smtp_port)
@ -27,7 +29,7 @@ impl Mailer {
let handlers = Handlebars::new();
Ok(Self {
smtp_transport,
smtp_username,
smtp_email,
handlers,
})
}
@ -64,7 +66,7 @@ impl Mailer {
let email = Message::builder()
.from(lettre::message::Mailbox::new(
Some("AppFlowy Notification".to_string()),
self.smtp_username.parse::<Address>()?,
self.smtp_email.parse::<Address>()?,
))
.to(lettre::message::Mailbox::new(
recipient_name,

View File

@ -145,7 +145,8 @@ pub struct AppState {
async fn get_worker_mailer(config: &Config) -> Result<AFWorkerMailer, Error> {
let mailer = Mailer::new(
config.mailer.smtp_username.clone(),
config.mailer.smtp_password.expose_secret().clone(),
config.mailer.smtp_email.clone(),
config.mailer.smtp_password.clone(),
&config.mailer.smtp_host,
config.mailer.smtp_port,
)

View File

@ -48,6 +48,12 @@ impl Config {
mailer: MailerSetting {
smtp_host: get_env_var("APPFLOWY_MAILER_SMTP_HOST", "smtp.gmail.com"),
smtp_port: get_env_var("APPFLOWY_MAILER_SMTP_PORT", "465").parse()?,
smtp_email: get_env_var("APPFLOWY_MAILER_SMTP_EMAIL", "sender@example.com"),
// `smtp_username` could be the same as `smtp_email`, but may not have to be.
// For example:
// - Azure Communication services uses a string of the format <resource name>.<app id>.<tenant id>
// - SendGrid uses the string apikey
// Adapted from: https://github.com/AppFlowy-IO/AppFlowy-Cloud/issues/984
smtp_username: get_env_var("APPFLOWY_MAILER_SMTP_USERNAME", "sender@example.com"),
smtp_password: get_env_var("APPFLOWY_MAILER_SMTP_PASSWORD", "password").into(),
},

View File

@ -58,8 +58,9 @@ mod tests {
#[tokio::test]
async fn render_import_report() {
let mailer = Mailer::new(
"test mailer".to_string(),
"123".to_string(),
"smtp_username".to_string(),
"stmp_email".to_string(),
"smtp_password".to_string().into(),
"localhost",
465,
)

View File

@ -511,7 +511,8 @@ async fn create_bucket_if_not_exists(
async fn get_mailer(config: &Config) -> Result<AFCloudMailer, Error> {
let mailer = Mailer::new(
config.mailer.smtp_username.clone(),
config.mailer.smtp_password.expose_secret().clone(),
config.mailer.smtp_email.clone(),
config.mailer.smtp_password.clone(),
&config.mailer.smtp_host,
config.mailer.smtp_port,
)

View File

@ -261,6 +261,7 @@ pub fn get_configuration() -> Result<Config, anyhow::Error> {
smtp_host: get_env_var("APPFLOWY_MAILER_SMTP_HOST", "smtp.gmail.com"),
smtp_port: get_env_var("APPFLOWY_MAILER_SMTP_PORT", "465").parse()?,
smtp_username: get_env_var("APPFLOWY_MAILER_SMTP_USERNAME", "sender@example.com"),
smtp_email: get_env_var("APPFLOWY_MAILER_SMTP_EMAIL", "sender@example.com"),
smtp_password: get_env_var("APPFLOWY_MAILER_SMTP_PASSWORD", "password").into(),
},
apple_oauth: AppleOAuthSetting {