Module lettre::transport::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::{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")
    .body(String::from("Be happy!"))?;

let result = sender.send(&email);
assert!(result.is_ok());

§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::{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")
    .body(String::from("Be happy!"))?;

let result = sender.send(&email);
assert!(result.is_ok());

§Async tokio 1.x

use std::env::temp_dir;

use lettre::{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")
    .body(String::from("Be happy!"))?;

let result = sender.send(email).await;
assert!(result.is_ok());

§Async async-std 1.x

use std::env::temp_dir;

use lettre::{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")
    .body(String::from("Be happy!"))?;

let result = sender.send(email).await;
assert!(result.is_ok());

Example email content result

From: NoBody <nobody@domain.tld>
Reply-To: Yuin <yuin@domain.tld>
To: Hei <hei@domain.tld>
Subject: Happy new year
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
  • The Errors that may occur when sending an email over SMTP
  • Writes the content and the envelope information to a file