12个版本
0.1.11 | 2024年5月23日 |
---|---|
0.1.10 | 2023年9月14日 |
0.1.8 | 2023年7月11日 |
0.1.7 | 2023年5月20日 |
0.1.3 | 2019年9月26日 |
#67 in 电子邮件
390 每月下载次数
用于 bestool
13KB
207 代码行
mailgun-rs
Mailgun API的非官方客户端库
# Cargo.toml
[dependencies]
mailgun-rs = "0.1.11"
示例
异步发送
$ cd examples/async
$ cargo run
发送简单电子邮件
use mailgun_rs::{EmailAddress, Mailgun, MailgunRegion, Message};
use std::collections::HashMap;
fn main() {
let domain = "huatuo.xyz";
let key = "key-xxxxxx";
let recipient = "[email protected]";
send_html(recipient, key, domain);
send_template(recipient, key, domain);
}
fn send_html(recipient: &str, key: &str, domain: &str) {
let recipient = EmailAddress::address(recipient);
let message = Message {
to: vec![recipient],
subject: String::from("mailgun-rs"),
html: String::from("<h1>hello from mailgun</h1>"),
..Default::default()
};
let client = Mailgun {
api_key: String::from(key),
domain: String::from(domain),
message,
};
let sender = EmailAddress::name_address("no-reply", "[email protected]");
match client.send(MailgunRegion::US, &sender) {
Ok(_) => {
println!("successful");
}
Err(err) => {
println!("Error: {err}");
}
}
}
发送模板电子邮件
fn send_template(recipient: &str, key: &str, domain: &str) {
let mut template_vars = HashMap::new();
template_vars.insert(String::from("firstname"), String::from("Dongri"));
let recipient = EmailAddress::address(recipient);
let message = Message {
to: vec![recipient],
subject: String::from("mailgun-rs"),
template: String::from("template-1"),
template_vars,
..Default::default()
};
let client = Mailgun {
api_key: String::from(key),
domain: String::from(domain),
message,
};
let sender = EmailAddress::name_address("no-reply", "[email protected]");
match client.send(MailgunRegion::US, &sender) {
Ok(_) => {
println!("successful");
}
Err(err) => {
println!("Error: {err}");
}
}
}
依赖关系
~4–18MB
~231K SLoC