14 releases (1 stable)

1.0.0 Nov 30, 2024
0.1.12 Oct 11, 2024
0.1.11 May 23, 2024
0.1.10 Sep 14, 2023
0.1.3 Sep 26, 2019

#82 in Email

Download history 140/week @ 2024-08-19 162/week @ 2024-08-26 47/week @ 2024-09-02 194/week @ 2024-09-09 25/week @ 2024-09-16 105/week @ 2024-09-23 62/week @ 2024-09-30 268/week @ 2024-10-07 262/week @ 2024-10-14 153/week @ 2024-10-21 190/week @ 2024-10-28 75/week @ 2024-11-04 121/week @ 2024-11-11 383/week @ 2024-11-18 326/week @ 2024-11-25 313/week @ 2024-12-02

1,149 downloads per month
Used in bestool

MIT license

12KB
212 lines

mailgun-rs

An unofficial client library for the Mailgun API

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

Examples

Send with async

See examples/async

$ cd examples/async
$ cargo run

Send a simple email

use mailgun_rs::{EmailAddress, Mailgun, MailgunRegion, Message};
use std::collections::HashMap;

fn main() {
    let domain = "huatuo.xyz";
    let key = "key-xxxxxx";
    let recipient = "dongrium@gmail.com";

    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),
    };
    let sender = EmailAddress::name_address("no-reply", "no-reply@hackerth.com");

    match client.send(MailgunRegion::US, &sender, message) {
        Ok(_) => {
            println!("successful");
        }
        Err(err) => {
            println!("Error: {err}");
        }
    }
}

Send a template email

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),
    };
    let sender = EmailAddress::name_address("no-reply", "no-reply@hackerth.com");

    match client.send(MailgunRegion::US, &sender, message) {
        Ok(_) => {
            println!("successful");
        }
        Err(err) => {
            println!("Error: {err}");
        }
    }
}

Dependencies

~5–17MB
~224K SLoC