Expand description

[feature: send-mail] provides the send_mail functionality

Send mail is a thin abstractions around sending commands, which combines the sending of the MAIL, RCPT, DATA commands with knowledge about wether or not SMTPUTF8 needs to be used.

Example

use std::iter::once as one;
use futures::stream::{self, Stream};
use futures::future::{self, lazy, Future};
use new_tokio_smtp::error::GeneralError;
use new_tokio_smtp::{Connection, ConnectionConfig};
use new_tokio_smtp::send_mail::{
    Mail, EncodingRequirement,
    MailAddress, MailEnvelop
};

let config = mock_connection_config();

let raw_mail = concat!(
    "Date: Thu, 14 Jun 2018 11:22:18 +0000\r\n",
    "From: <no-reply@test.test>\r\n",
    "\r\n",
    "...\r\n"
);

// this normally adapts to a higher level abstraction
// of mail then this crate provides
let mail_data = Mail::new(EncodingRequirement::None, raw_mail.to_owned());
// the from_unchecked normally can be used if we know the address is valid
// a mail address parser will be added at some point in the future
let sender = MailAddress::from_unchecked("test@sender.test");
let send_to = MailAddress::from_unchecked("test@receiver.test");
let mail = MailEnvelop::new(sender, vec1![ send_to ], mail_data);

let mail2 = mail.clone();
let config2 = config.clone();

mock_run_with_tokio(lazy(move || {
    Connection::connect(config)
        .map_err(GeneralError::from)
        .and_then(|con| con.send_mail(mail).map_err(Into::into))
        .and_then(|(con, mail_result)| {
            if let Err((idx, err)) = mail_result {
                println!("sending mail failed: {}", err)
            }
            con.quit().map_err(Into::into)
        }).then(|res|{
            match res {
                Ok(_) => println!("done, and closed connection"),
                Err(err) => println!("problem with connection: {}", err)
            }
            Result::Ok::<(),()>(())
        })
}));

// This is only overhead as we skipped any (fallible) mail encoding step
let mail2: Result<_, GeneralError> = Ok(mail2);

//or simpler
mock_run_with_tokio(lazy(move || {
    // it accepts a iterator over mails,
    Connection::connect_send_quit(config2, one(mail2))
        //Stream::for_each is conceptually broken in futures v0.1
        .then(|res| Ok(res))
        .for_each(|result| {
            if let Err(err) = result {
                println!("sending mail failed: {}", err);
            } else {
                println!("successfully send mail")
            }
            Ok(())
        })
}));

Structs

POD representing the smtp envelops from,to’s
A simplified representation of a mail consisting of an EncodingRequirement and a buffer
A simple MailAddress type
represents a mail envelop consisting of EnvelopData and a Mail
Stream adapt resolving one function/future after the stream completes
Adapter to send all mails from an iterable instance through a smtp connection.

Enums

Specifies if the mail requires SMTPUTF8 (or Mime8bit)

Functions

Sends a mail specified through MailEnvelop through the connection con.

Type Definitions

Future returned by send_mail
The result of sending a mail