Crate async_mailer_smtp

Source
Expand description

An SMTP mailer, usable either stand-alone or as either generic Mailer or dynamic dyn DynMailer using the mail-send crate.

Preferably, use async-mailer, which re-exports from this crate, rather than using async-mailer-smtp directly.

You can control the re-exported mailer implementations, as well as tracing support, via async-mailer feature toggles.

Note: If you are planning to always use SmtpMailer and do not need async_mailer_outlook::OutlookMailer or async_mailer::BoxMailer, then consider using the mail-send crate directly.

§Examples

§Using the statically typed Mailer:

// Both `async_mailer::OutlookMailer` and `async_mailer::SmtpMailer` implement `Mailer`
// and can be used with `impl Mailer` or `<M: Mailer>` bounds.

let mailer = SmtpMailer::new(
    "smtp.example.com".into(),
    465,
    SmtpInvalidCertsPolicy::Deny,
    "<username>".into(),
    secrecy::SecretString::from("<password>")
);

// An alternative `OutlookMailer` can be found at `async-mailer-outlook`.
// Further alternative mailers can be implemented by third parties.

// Build a message using the re-exported `mail_builder::MessageBuilder'.
//
// For blazingly fast rendering of beautiful HTML mail,
// I recommend combining `askama` with `mrml`.

let message = async_mailer_core::mail_send::mail_builder::MessageBuilder::new()
    .from(("From Name", "from@example.com"))
    .to("to@example.com")
    .subject("Subject")
    .text_body("Mail body")
    .into_message()?;

// Send the message using the statically typed `Mailer`.

mailer.send_mail(message).await?;

§Using the dynamically typed DynMailer:

// Both `async_mailer::OutlookMailer` and `async_mailer::SmtpMailer`
// implement `DynMailer` and can be used as trait objects.
//
// Here they are used as `BoxMailer`, which is an alias to `Box<dyn DynMailer>`.

let mailer: BoxMailer = SmtpMailer::new_box( // Or `SmtpMailer::new_arc()`.
    "smtp.example.com".into(),
    465,
    SmtpInvalidCertsPolicy::Deny,
    "<username>".into(),
    secrecy::SecretString::from("<password>")
);

// An alternative `OutlookMailer` can be found at `async-mailer-outlook`.
// Further alternative mailers can be implemented by third parties.

// The trait object is `Send` and `Sync` and may be stored e.g. as part of your server state.

// Build a message using the re-exported `mail_builder::MessageBuilder'.
//
// For blazingly fast rendering of beautiful HTML mail,
// I recommend combining `askama` with `mrml`.

let message = async_mailer_core::mail_send::mail_builder::MessageBuilder::new()
    .from(("From Name", "from@example.com"))
    .to("to@example.com")
    .subject("Subject")
    .text_body("Mail body")
    .into_message()?;

// Send the message using the implementation-agnostic `dyn DynMailer`.

mailer.send_mail(message).await?;

§Feature flags

  • tracing: Enable debug and error logging using the tracing crate. All relevant functions are instrumented.
  • clap: Implement clap::ValueEnum for SmtpInvalidCertsPolicy. This allows for easily configured CLI options like --invalid-certs <allow|deny>.

Default: tracing.

§Roadmap

DKIM support is planned to be implemented on the SmtpMailer.

Structs§

SmtpMailer
An SMTP mailer client, implementing the async_mailer_core::Mailer and async_mailer_core::DynMailer traits to be used as generic mailer or runtime-pluggable trait object.

Enums§

SmtpInvalidCertsPolicy
Pass to SmtpMailer::new to either allow or deny invalid SMTP certificates.
SmtpMailerError
Error returned by SmtpMailer::new and SmtpMailer::send_mail.