[SOLVED] Email with attachment

Hello, everybody. I have to find a way to send emails with the PDF file attached. Do you know some package for that? I have found this one, but it is deprecated, is not in the crates anymore and does not seem to have any docs. Does anyone know the better package?

Have you tried Mailstrom (the successor):


Or lettre together with MimeMessage:


I've only used lettre so far (worked really well), so I can't comment on the other crates.

Note that you can use crates that are not on crates.io but on github - see cargo guide:


rand = { git = "https://github.com/rust-lang-nursery/rand.git", rev = "9f35b8e" }

(example from the cargo guide)

1 Like

Thanks for the links!

I have looked at Mailstrom - it does not seem to support attachment.

As for the Lettre - I have found somewhere in it's docs that it does not support attachments yet. So you advice to use MimeMessage to add this support, but I am not sure I can do it w/o any example :slight_smile: I am very new to Rust, started learning it just a few days ago and decided to learn by implementing the real task with it. Already getting the data fom the db, sending push notifications to the mobile apps, generating PDFs with qr codes, but stuck with sending email :slight_smile:

Have you used Lettre to send multi-part messages or just the text part?

I've used lettre to just send text only, sorry!

Finished with just generating the command lines for "mail" and sending through Postfix. Works like a charm.

1 Like

See below example:

First of all, you must allow your gmail account to be used by less secure apps from https://myaccount.google.com/lesssecureapps

Secondly, use the mime crate along with the lettre crate, as below:


name = "email"
version = "0.1.0"
authors = ["Hasan Yousef"]
edition = "2018"

lettre = "0.9"
lettre_email = "0.9"
mime = "0.3.13"


use lettre_email::Email;
use lettre::smtp::authentication::Credentials;
use lettre::{SmtpClient, Transport};
use std::path::Path;

fn main() {
    let email = Email::builder()
        .html("<h1>Hi there</h1>")
        .attachment_from_file(Path::new("myAttachement.pdf"), None, &mime::APPLICATION_PDF)

    let creds = Credentials::new(

    // Open connection to gmail
    let mut mailer = SmtpClient::new_simple("smtp.gmail.com")

    // Send the email
    let result = mailer.send(email.into());

    if result.is_ok() {
        println!("Email sent");
    } else {
        println!("Could not send email: {:?}", result);