Skip to content

Commit

Permalink
new templates for confirmation email
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcclung committed Mar 17, 2024
1 parent 912c652 commit 1eebcca
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ pub struct LettreEmailSender;

impl EmailSender for LettreEmailSender {
fn send(&self, port: u16, host: &str, creds: Credentials, message: Message) -> Result<()> {
let tls_parameters = TlsParameters::builder(host.into())
.build()
.unwrap();

let tls_parameters = TlsParameters::builder(host.into()).build().unwrap();

let mailer = SmtpTransport::relay(host)?
.tls(Tls::Required(tls_parameters))
.port(port)
Expand Down
23 changes: 20 additions & 3 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};
use actix_web::{web, HttpResponse};
use anyhow::Result;
use askama::Template;
use chrono::Utc;
use serde::Deserialize;
use sqlx::{Pool, Postgres};
Expand Down Expand Up @@ -81,17 +82,33 @@ pub async fn subscribe<'a, T: EmailSender + Debug>(
}
}

#[derive(Template)]
#[template(path = "confirmation/email.html")]
struct ConfirmationEmailHtmlTemplate {}

#[derive(Template)]
#[template(path = "confirmation/email.txt")]
struct ConfirmationEmailTxtTemplate {}

#[derive(Template)]
#[template(path = "confirmation/subject.txt")]
struct ConfirmationEmailSubject {}

fn send_confirmation_email<T: EmailSender>(
new_subscriber_email: &str,
email_service: web::Data<Mutex<EmailService<'_, T>>>,
) -> Result<()> {
let confirm_email_html = ConfirmationEmailHtmlTemplate {};
let confirm_email_plaintext = ConfirmationEmailTxtTemplate {};
let confirm_subject = ConfirmationEmailSubject {};

let email = Email {
to: new_subscriber_email,
from: "",
subject: "Welcome to zero2prod.xyz",
subject: &confirm_subject.render().unwrap(),
reply_to: "",
plaintext: "We're glad you're here, confirm your subscription https://zero2prod.xyz/confirm?token=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0",
html: "<h1>We're glad you're here</h1><p>confirm your <a href='https://zero2prod.xyz/confirm?token=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0'>subscription</a></p>",
plaintext: &confirm_email_plaintext.render().unwrap(),
html: &confirm_email_html.render().unwrap(),
};
email_service.lock().unwrap().send_email(email)
}
4 changes: 4 additions & 0 deletions templates/confirmation/email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>We're glad you're here</h1>
<p>
Confirm your <a href='https://zero2prod.xyz/confirm?token=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0'>subscription</a>
</p>
1 change: 1 addition & 0 deletions templates/confirmation/email.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
We're glad you're here, confirm your subscription https://zero2prod.xyz/confirm?token=a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
1 change: 1 addition & 0 deletions templates/confirmation/subject.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Welcome to zero2prod.xyz

0 comments on commit 1eebcca

Please sign in to comment.