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 电子邮件

Download history 7/week @ 2024-04-16 23/week @ 2024-04-23 41/week @ 2024-04-30 45/week @ 2024-05-07 57/week @ 2024-05-14 199/week @ 2024-05-21 36/week @ 2024-05-28 71/week @ 2024-06-04 40/week @ 2024-06-11 66/week @ 2024-06-18 113/week @ 2024-06-25 34/week @ 2024-07-02 87/week @ 2024-07-09 30/week @ 2024-07-16 90/week @ 2024-07-23 173/week @ 2024-07-30

390 每月下载次数
用于 bestool

MIT 许可证

13KB
207 代码行

mailgun-rs

Mailgun API的非官方客户端库

# Cargo.toml
[dependencies]
mailgun-rs = "0.1.11"

示例

异步发送

查看 examples/async

$ 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