Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: separate smtp username from email #994

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deploy.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
[email protected]
[email protected]
APPFLOWY_MAILER_SMTP_PASSWORD=email_sender_password

RUST_LOG=info
Expand Down
1 change: 1 addition & 0 deletions libs/mailer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}
12 changes: 7 additions & 5 deletions libs/mailer/src/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ 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)
.build();
let handlers = Handlebars::new();
Ok(Self {
smtp_transport,
smtp_username,
smtp_email,
handlers,
})
}
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion services/appflowy-worker/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
6 changes: 6 additions & 0 deletions services/appflowy-worker/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]"),
// `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", "[email protected]"),
smtp_password: get_env_var("APPFLOWY_MAILER_SMTP_PASSWORD", "password").into(),
},
Expand Down
5 changes: 3 additions & 2 deletions services/appflowy-worker/src/mailer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
3 changes: 2 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
1 change: 1 addition & 0 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]"),
smtp_email: get_env_var("APPFLOWY_MAILER_SMTP_EMAIL", "[email protected]"),
smtp_password: get_env_var("APPFLOWY_MAILER_SMTP_PASSWORD", "password").into(),
},
apple_oauth: AppleOAuthSetting {
Expand Down
Loading