#mailgun #send-email #client #client-send #api #template

mailgun-rs

An unofficial client library for the Mailgun API

12 releases

new 0.1.11 May 23, 2024
0.1.10 Sep 14, 2023
0.1.8 Jul 11, 2023
0.1.7 May 20, 2023
0.1.3 Sep 26, 2019

#71 in Email

Download history 81/week @ 2024-02-01 191/week @ 2024-02-08 147/week @ 2024-02-15 178/week @ 2024-02-22 122/week @ 2024-02-29 148/week @ 2024-03-07 176/week @ 2024-03-14 104/week @ 2024-03-21 109/week @ 2024-03-28 66/week @ 2024-04-04 44/week @ 2024-04-11 10/week @ 2024-04-18 43/week @ 2024-04-25 23/week @ 2024-05-02 69/week @ 2024-05-09 29/week @ 2024-05-16

168 downloads per month

MIT license

13KB
207 lines

mailgun-rs

An unofficial client library for the Mailgun API

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

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

    match client.send(MailgunRegion::US, &sender) {
        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),
        message,
    };
    let sender = EmailAddress::name_address("no-reply", "no-reply@hackerth.com");

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

Dependencies

~4–19MB
~258K SLoC