9 个版本
0.3.2 | 2024 年 4 月 5 日 |
---|---|
0.3.1 | 2024 年 4 月 5 日 |
0.3.0 | 2023 年 10 月 24 日 |
0.2.3 | 2023 年 5 月 31 日 |
0.1.1 | 2023 年 5 月 16 日 |
#220 in 电子邮件
每月 191 次下载
在 async-mailer 中使用
18KB
137 行
一个 SMTP 邮件发送器,既可以独立使用,也可以作为泛型 Mailer
或动态 dyn DynMailer
使用 mail-send
crate。
建议使用 async-mailer
,它重新导出此 crate,而不是直接使用 async-mailer-smtp
。
您可以通过 async-mailer
功能切换 来控制重新导出的邮件发送器实现以及 tracing
支持。
注意:如果您打算始终使用 SmtpMailer
并且不需要 async_mailer_outlook::OutlookMailer
或 async_mailer::BoxMailer
,则考虑直接使用 mail-send
crate。
示例
使用静态类型的 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::Secret::new("<password>".into())
);
// 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", "[email protected]"))
.to("[email protected]")
.subject("Subject")
.text_body("Mail body")
.into_message()?;
// Send the message using the statically typed `Mailer`.
mailer.send_mail(message).await?;
使用动态类型的 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::Secret::new("<password>".into())
);
// 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", "[email protected]"))
.to("[email protected]")
.subject("Subject")
.text_body("Mail body")
.into_message()?;
// Send the message using the implementation-agnostic `dyn DynMailer`.
mailer.send_mail(message).await?;
功能标志
tracing
:使用tracing
crate 启用调试和错误日志记录。所有相关函数都已进行仪器化。clap
:为SmtpInvalidCertsPolicy
实现clap::ValueEnum
。这允许配置简单的 CLI 选项,如--invalid-certs <allow|deny>
。
默认:tracing
。
路线图
计划在 SmtpMailer
上实现 DKIM 支持。
依赖项
~11–21MB
~361K SLoC