#mailgun #async #api #mailgun-rs

mailgun-rs

An unofficial client library for the Mailgun API

16 releases (3 stable)

1.0.2 Apr 11, 2025
1.0.1 Dec 24, 2024
1.0.0 Nov 30, 2024
0.1.12 Oct 11, 2024
0.1.3 Sep 26, 2019

#59 in Email

Download history 51/week @ 2024-12-27 180/week @ 2025-01-03 125/week @ 2025-01-10 72/week @ 2025-01-17 327/week @ 2025-01-24 173/week @ 2025-01-31 88/week @ 2025-02-07 215/week @ 2025-02-14 114/week @ 2025-02-21 198/week @ 2025-02-28 197/week @ 2025-03-07 280/week @ 2025-03-14 123/week @ 2025-03-21 44/week @ 2025-03-28 42/week @ 2025-04-04 187/week @ 2025-04-11

444 downloads per month
Used in bestool

MIT license

17KB
245 lines

mailgun-rs

An unofficial client library for the Mailgun API

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

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);
    send_html_with_attachment(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@huatuo.xyz");

    match client.send(MailgunRegion::US, &sender, message, None) {
        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("name"), String::from("Dongri Jin"));

    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@huatuo.xyz");

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

Send a simple email

fn send_html_with_attachment(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 with attachments</h1>"),
        ..Default::default()
    };

    let client = Mailgun {
        api_key: String::from(key),
        domain: String::from(domain),
    };
    let sender = EmailAddress::name_address("no-reply", "no-reply@huatuo.xyz");
    let attachments = vec!["/path/to/attachment-1.txt".to_string(), "/path/to/attachment-2.txt".to_string()];

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

Dependencies

~4–18MB
~262K SLoC