14 个版本
0.4.2 | 2024年4月5日 |
---|---|
0.4.1 | 2024年4月5日 |
0.4.0 | 2023年10月24日 |
0.3.5 | 2023年5月31日 |
0.1.2 | 2023年5月16日 |
#54 在 邮件
27KB
108 行
async-mailer
一套异步泛型 Mailer
和动态 dyn DynMailer
特性,具有运行时可插拔的 Microsoft Outlook (Office365) 和 SMTP 实现。
安装
将以下内容添加到您的 Cargo.toml
async-mailer = "0.4.2"
您可以通过 crate 功能切换 来控制重新导出的邮件实现以及 tracing
支持。
默认情况下,启用了功能 smtp
、outlook
和 tracing
。使用 default-features = false
和 features = [...]
逐个选择功能。
示例
使用 new
创建静态类型的邮件实例,或使用 new_box
/ new_arc
创建类型擦除的动态邮件。
Microsoft Outlook (Office365) 和 SMTP 变体可用。
使用静态类型的 Mailer
// Both `OutlookMailer` and `SmtpMailer` implement `Mailer`
// and can be used with `impl Mailer` or `<M: Mailer>` bounds.
use async_mailer::{IntoMessage, Mailer, OutlookMailer, SmtpMailer};
let mailer: OutlookMailer = OutlookMailer::new(
"<Microsoft Identity service tenant>".into(),
"<OAuth2 app GUID>".into(),
async_mailer::Secret::new("<OAuth2 app secret>".into())
).await?;
// Alternative:
let mailer: SmtpMailer = SmtpMailer::new(
"smtp.example.com".into(),
465,
async_mailer::SmtpInvalidCertsPolicy::Deny,
"<username>".into(),
async_mailer::Secret::new("<password>".into())
);
// 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::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?;
使用动态类型的 dyn DynMailer
/ BoxMailer
/ ArcMailer
use async_mailer::{BoxMailer, IntoMessage, OutlookMailer, SmtpMailer};
// Both `OutlookMailer` and `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 = OutlookMailer::new_box( // Or `OutlookMailer::new_arc()`.
"<Microsoft Identity service tenant>".into(),
"<OAuth2 app GUID>".into(),
async_mailer::Secret::new("<OAuth2 app secret>".into())
).await?;
// Alternative:
let mailer: BoxMailer = SmtpMailer::new_box( // Or `SmtpMailer::new_arc()`.
"smtp.example.com".into(),
465,
async_mailer::SmtpInvalidCertsPolicy::Deny,
"<username>".into(),
async_mailer::Secret::new("<password>".into())
);
// 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::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?;
功能标志
outlook
: 启用OutlookMailer
。smtp
:启用SmtpMailer
。tracing
:使用tracing
包启用调试和错误日志。clap
:为SmtpInvalidCertsPolicy
实现clap::ValueEnum
。这允许配置如--invalid-certs <允许|拒绝>
这样的CLI选项。
默认:outlook
,smtp
,tracing
。
路线图
- 计划在
SmtpMailer
上实现DKIM支持。 - 计划在
OutlookMailer
上实现访问令牌自动刷新。
可能进一步实现其他邮件发送器。请提交一个问题,并尽可能提供pull request以添加您选择的邮件发送器实现!
依赖项
~11–25MB
~467K SLoC