#mailgun #client #api #message #mailgun-region

mailgun-rs

An unofficial client library for the Mailgun API

11 releases

0.1.10 Sep 14, 2023
0.1.9 Sep 13, 2023
0.1.8 Jul 11, 2023
0.1.7 May 20, 2023
0.1.3 Sep 26, 2019

#69 in Email

Download history 110/week @ 2023-12-11 112/week @ 2023-12-18 47/week @ 2023-12-25 56/week @ 2024-01-01 98/week @ 2024-01-08 113/week @ 2024-01-15 127/week @ 2024-01-22 21/week @ 2024-01-29 148/week @ 2024-02-05 169/week @ 2024-02-12 146/week @ 2024-02-19 195/week @ 2024-02-26 138/week @ 2024-03-04 136/week @ 2024-03-11 126/week @ 2024-03-18 148/week @ 2024-03-25

553 downloads per month

MIT license

12KB
207 lines

mailgun-rs

An unofficial client library for the Mailgun API

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

Examples

extern crate mailgun_rs;

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

fn main() {
    let domain = "mailgun.hackerth.com";
    let key = "key-xxxxxx";
    let recipient = "dongrify@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}");
        }
    }
}

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–18MB
~258K SLoC