#email #mailgun

bin+lib mailgun-rs

An unofficial client library for the Mailgun API

6 releases

0.1.5 Feb 1, 2023
0.1.4 Dec 29, 2022
0.1.3 Sep 26, 2019

#40 in Email

Download history 6/week @ 2022-12-06 10/week @ 2022-12-13 4/week @ 2022-12-20 57/week @ 2022-12-27 9/week @ 2023-01-03 7/week @ 2023-01-10 4/week @ 2023-01-17 13/week @ 2023-01-24 62/week @ 2023-01-31 15/week @ 2023-02-07 49/week @ 2023-02-14 116/week @ 2023-02-21 76/week @ 2023-02-28 63/week @ 2023-03-07 87/week @ 2023-03-14 114/week @ 2023-03-21

363 downloads per month

MIT license

11KB
162 lines

mailgun-rs

An unofficial client library for the Mailgun API

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

Examples

extern crate mailgun_rs;

use mailgun_rs::{EmailAddress, Mailgun, 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(&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(&sender) {
        Ok(_) => {
            println!("successful");
        }
        Err(err) => {
            println!("Error: {err}");
        }
    }
}

Dependencies

~4–12MB
~252K SLoC