Skip to content

Commit

Permalink
attach images in email
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan0xC committed Oct 4, 2022
1 parent 2868a66 commit 7aaa41c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@
## but might need to be changed in case it trips some anti-spam filters
# HELO_NAME=

## Embed images as email attachments
## When set to false without adapting the templates `email_header.hbs` and `email_footer.hbs`
## the mails will be missing their attachments.
# SMTP_EMBED_IMAGES=false

## SMTP debugging
## When set to true this will output very detailed SMTP messages.
## WARNING: This could contain sensitive information like passwords and usernames! Only enable this during troubleshooting!
Expand Down
4 changes: 2 additions & 2 deletions src/api/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ fn alive(_conn: DbConn) -> Json<String> {
#[get("/vw_static/<filename>")]
fn static_files(filename: String) -> Result<(ContentType, &'static [u8]), Error> {
match filename.as_ref() {
"mail-github.png" => Ok((ContentType::PNG, include_bytes!("../static/images/mail-github.png"))),
"logo-gray.png" => Ok((ContentType::PNG, include_bytes!("../static/images/logo-gray.png"))),
"mail-github.png" => Ok((ContentType::PNG, crate::mail::MAIL_GITHUB)),
"logo-gray.png" => Ok((ContentType::PNG, crate::mail::LOGO_GRAY)),
"error-x.svg" => Ok((ContentType::SVG, include_bytes!("../static/images/error-x.svg"))),
"hibp.png" => Ok((ContentType::PNG, include_bytes!("../static/images/hibp.png"))),
"vaultwarden-icon.png" => Ok((ContentType::PNG, include_bytes!("../static/images/vaultwarden-icon.png"))),
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ make_config! {
smtp_timeout: u64, true, def, 15;
/// Server name sent during HELO |> By default this value should be is on the machine's hostname, but might need to be changed in case it trips some anti-spam filters
helo_name: String, true, option;
/// Embed images as email attachments.|> When set to false without adapting the templates `email_header.hbs` and `email_footer.hbs` the mails will be missing their attachments.
smtp_embed_images: bool, true, def, true;
/// Enable SMTP debugging (Know the risks!) |> DANGEROUS: Enabling this will output very detailed SMTP messages. This could contain sensitive information like passwords and usernames! Only enable this during troubleshooting!
smtp_debug: bool, false, def, false;
/// Accept Invalid Certs (Know the risks!) |> DANGEROUS: Allow invalid certificates. This option introduces significant vulnerabilities to man-in-the-middle attacks!
Expand Down
38 changes: 22 additions & 16 deletions src/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,31 +466,37 @@ pub async fn send_test(address: &str) -> EmptyResult {
send_email(address, &subject, body_html, body_text).await
}

pub const MAIL_GITHUB: &[u8] = include_bytes!("static/images/mail-github.png");
pub const LOGO_GRAY: &[u8] = include_bytes!("static/images/logo-gray.png");

async fn send_email(address: &str, subject: &str, body_html: String, body_text: String) -> EmptyResult {
const LOGO_GRAY: &[u8] = include_bytes!("static/images/logo-gray.png");
const MAIL_GITHUB: &[u8] = include_bytes!("static/images/mail-github.png");
let logo_gray_body = Body::new(LOGO_GRAY.to_vec());
let mail_github_body = Body::new(MAIL_GITHUB.to_vec());
let smtp_from = &CONFIG.smtp_from();

let body = if CONFIG.smtp_embed_images() {
MultiPart::alternative().singlepart(SinglePart::plain(body_text)).multipart(
MultiPart::related()
.singlepart(SinglePart::html(body_html))
.singlepart(
Attachment::new_inline(String::from("logo-gray"))
.body(logo_gray_body, "image/png".parse().unwrap()),
)
.singlepart(
Attachment::new_inline(String::from("mail-github"))
.body(mail_github_body, "image/png".parse().unwrap()),
),
)
} else {
MultiPart::alternative_plain_html(body_text, body_html)
};

let email = Message::builder()
.message_id(Some(format!("<{}@{}>", crate::util::get_uuid(), smtp_from.split('@').collect::<Vec<&str>>()[1])))
.to(Mailbox::new(None, Address::from_str(address)?))
.from(Mailbox::new(Some(CONFIG.smtp_from_name()), Address::from_str(smtp_from)?))
.subject(subject)
.multipart(
MultiPart::alternative().singlepart(SinglePart::plain(body_text)).multipart(
MultiPart::related()
.singlepart(SinglePart::html(body_html))
.singlepart(
Attachment::new_inline(String::from("logo-gray"))
.body(logo_gray_body, "image/png".parse().unwrap()),
)
.singlepart(
Attachment::new_inline(String::from("github-logo"))
.body(mail_github_body, "image/png".parse().unwrap()),
),
),
)?;
.multipart(body)?;

match mailer().send(email).await {
Ok(_) => Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion src/static/templates/email/email_header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
<table class="body-wrap" cellpadding="0" cellspacing="0" style="-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; box-sizing: border-box; color: #333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25px; margin: 0; width: 100%;" bgcolor="#f6f6f6">
<tr style="-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; box-sizing: border-box; color: #333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25px; margin: 0;">
<td valign="middle" class="aligncenter middle logo" style="-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; box-sizing: border-box; color: #333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25px; margin: 0; padding: 20px 0 10px;" align="center">
<img src="cid:logo-gray" alt="" width="190" height="39" style="-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; border: none; box-sizing: border-box; color: #333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25px; margin: 0; max-width: 100%;" />
<img src="cid:logo-gray" alt="Vaultwarden" width="190" height="39" style="-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; border: none; box-sizing: border-box; color: #333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25px; margin: 0; max-width: 100%;" />
</td>
</tr>
<tr style="-webkit-font-smoothing: antialiased; -webkit-text-size-adjust: none; box-sizing: border-box; color: #333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 25px; margin: 0;">
Expand Down

0 comments on commit 7aaa41c

Please sign in to comment.