[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):

https://github.com/mikedilger/mailstrom

Or lettre together with MimeMessage:

https://lettre.github.io/lettre/v0.6.2/lettre/email/struct.PartBuilder.html

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:

http://doc.crates.io/guide.html

[dependencies]
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:

Cargo.toml:

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

[dependencies]
lettre = "0.9"
lettre_email = "0.9"
mime = "0.3.13"

main.rs:

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

fn main() {
    let email = Email::builder()
        .to("to@xxx.zz")
        .from("YOUR_EMAIL@gmail.com")
        .subject("subject")
        .html("<h1>Hi there</h1>")
        .text("message")
        .attachment_from_file(Path::new("myAttachement.pdf"), None, &mime::APPLICATION_PDF)
        .unwrap()
        .build()
        .unwrap();

    let creds = Credentials::new(
        "YOUR_EMAIL@gmail.com".to_string(),
        "YOUR_PASSWORD".to_string(),
    );

    // Open connection to gmail
    let mut mailer = SmtpClient::new_simple("smtp.gmail.com")
        .unwrap()
        .credentials(creds)
        .transport();

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

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

    assert!(result.is_ok());
}
3 Likes