Module file

Source
Available on crate feature file-transport only.
Expand description

The file transport writes the emails to the given directory. The name of the file will be message_id.eml. It can be useful for testing purposes, or if you want to keep track of sent messages.

§Sync example

use std::env::temp_dir;

use lettre::{message::header::ContentType, FileTransport, Message, Transport};

// Write to the local temp directory
let sender = FileTransport::new(temp_dir());
let email = Message::builder()
    .from("NoBody <nobody@domain.tld>".parse()?)
    .reply_to("Yuin <yuin@domain.tld>".parse()?)
    .to("Hei <hei@domain.tld>".parse()?)
    .subject("Happy new year")
    .header(ContentType::TEXT_PLAIN)
    .body(String::from("Be happy!"))?;

sender.send(&email)?;

§Sync example with envelope

It is possible to also write the envelope content in a separate JSON file by using the with_envelope builder. The JSON file will be written in the target directory with same name and a json extension.

use std::env::temp_dir;

use lettre::{message::header::ContentType, FileTransport, Message, Transport};

// Write to the local temp directory
let sender = FileTransport::with_envelope(temp_dir());
let email = Message::builder()
    .from("NoBody <nobody@domain.tld>".parse()?)
    .reply_to("Yuin <yuin@domain.tld>".parse()?)
    .to("Hei <hei@domain.tld>".parse()?)
    .subject("Happy new year")
    .header(ContentType::TEXT_PLAIN)
    .body(String::from("Be happy!"))?;

sender.send(&email)?;

§Async tokio 1.x

use std::env::temp_dir;

use lettre::{
    message::header::ContentType, AsyncFileTransport, AsyncTransport, Message, Tokio1Executor,
};

// Write to the local temp directory
let sender = AsyncFileTransport::<Tokio1Executor>::new(temp_dir());
let email = Message::builder()
    .from("NoBody <nobody@domain.tld>".parse()?)
    .reply_to("Yuin <yuin@domain.tld>".parse()?)
    .to("Hei <hei@domain.tld>".parse()?)
    .subject("Happy new year")
    .header(ContentType::TEXT_PLAIN)
    .body(String::from("Be happy!"))?;

sender.send(email).await?;

§Async async-std 1.x

use std::env::temp_dir;

use lettre::{
    message::header::ContentType, AsyncFileTransport, AsyncStd1Executor, AsyncTransport,
    Message,
};

// Write to the local temp directory
let sender = AsyncFileTransport::<AsyncStd1Executor>::new(temp_dir());
let email = Message::builder()
    .from("NoBody <nobody@domain.tld>".parse()?)
    .reply_to("Yuin <yuin@domain.tld>".parse()?)
    .to("Hei <hei@domain.tld>".parse()?)
    .subject("Happy new year")
    .header(ContentType::TEXT_PLAIN)
    .body(String::from("Be happy!"))?;

sender.send(email).await?;

Example email content result

From: NoBody <nobody@domain.tld>
Reply-To: Yuin <yuin@domain.tld>
To: Hei <hei@domain.tld>
Subject: Happy new year
Content-Type: text/plain; charset=utf-8
Date: Tue, 18 Aug 2020 22:50:17 GMT

Be happy!

Example envelope result

{"forward_path":["hei@domain.tld"],"reverse_path":"nobody@domain.tld"}

Structs§

AsyncFileTransporttokio1 or async-std1
Asynchronously writes the content and the envelope information to a file
Error
The Errors that may occur when sending an email over SMTP
FileTransport
Writes the content and the envelope information to a file