3 个版本
新版本 0.0.7 | 2024年8月21日 |
---|---|
0.0.6 | 2024年8月17日 |
0.0.4 | 2024年8月9日 |
#93 在 电子邮件
每月 250 次下载
32KB
633 行
依赖项
spring-mail = { version = "0.0.7" }
配置项
[mail]
host = "smtp.gmail.com" # SMTP mail server address,
port = 465 # SMTP server port number
secure = true # Response timeout, in milliseconds
auth = { user = "[email protected]", password = "passwd" } # Authentication information
组件
配置以上配置项后,插件将自动注册一个 Mailer
STMP 异步客户端。此对象是 lettre::AsyncSmtpTransport<Tokio1Executor>
的别名。
pub type Mailer = lettre::AsyncSmtpTransport<Tokio1Executor>;
提取插件注册的组件
MailPlugin
插件会自动为我们注册 SMTP 客户端。我们可以使用 Component
从 AppState 中提取这个连接池。 Component
是 axum 的一个 提取器。
async fn send_mail(Component(mailer): Component<Mailer>) -> Result<impl IntoResponse> {
let email = Message::builder()
.from("NoBody <[email protected]>".parse().unwrap())
.reply_to("Yuin <[email protected]>".parse().unwrap())
.to("[email protected]".parse().unwrap())
.subject("Happy new year")
.header(ContentType::TEXT_PLAIN)
.body(String::from("Be happy!"))
.unwrap();
let resp = mailer.send(email).await.context("send mail failed")?;
Ok(Json(resp))
}
有关完整代码,请参阅 mail-example
依赖项
~12–43MB
~695K SLoC