Skip to content

Commit

Permalink
feat(webserver): Create template system for emails (#1447)
Browse files Browse the repository at this point in the history
* feat(webserver): Create template system for emails

* Put newlines between lines of email body

* Move email template folder
  • Loading branch information
boxbeam authored Feb 13, 2024
1 parent 80cb148 commit a5a37a3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ee/tabby-webserver/email_templates/invitation.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
You've been invited to join a Tabby workspace!
Welcome to Tabby! You have been invited to join a Tabby Server, where you can tap into AI-driven code completions and chat assistants.

Go to {external_url}/auth/signup?invitationCode={code} to join!
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use lettre::{
use tabby_db::{DbConn, DbEnum};
use tokio::{sync::RwLock, task::JoinHandle};
use tracing::warn;
mod templates;

use crate::schema::{
email::{
Expand Down Expand Up @@ -223,11 +224,9 @@ impl EmailService for EmailServiceImpl {
) -> Result<JoinHandle<()>, SendEmailError> {
let network_setting = self.db.read_network_setting().await?;
let external_url = network_setting.external_url;
self.send_email_in_background(
email,
"You've been invited to join a Tabby workspace!".into(),
format!("Welcome to Tabby! You have been invited to join a Tabby Server, where you can tap into AI-driven code completions and chat assistants.\n\nGo to {external_url}/auth/signup?invitationCode={code} to join!"),
).await
let contents = templates::invitation_email(&external_url, &code);
self.send_email_in_background(email, contents.subject, contents.body)
.await
}
}

Expand Down
26 changes: 26 additions & 0 deletions ee/tabby-webserver/src/service/email/templates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pub struct EmailContents {
pub subject: String,
pub body: String,
}

fn format_email(template: &'static str, replacements: &[(&str, &str)]) -> EmailContents {
let mut lines = template.lines();
let mut subject = lines
.next()
.expect("Email must have subject line")
.to_string();
let body: Vec<&str> = lines.collect();
let mut body = body.join("\n");
for (name, replacement) in replacements {
body = body.replace(name, replacement);
subject = subject.replace(name, replacement);
}
EmailContents { subject, body }
}

pub fn invitation_email(external_url: &str, code: &str) -> EmailContents {
format_email(
include_str!("../../../email_templates/invitation.html"),
&[("{external_url}", external_url), ("{code}", code)],
)
}

0 comments on commit a5a37a3

Please sign in to comment.