Search

Lib.rs

› Email
#email-api

mailgun-rs

An unofficial client library for the Mailgun API

by Dongri Jin and 8 contributors

  • Install
  • API reference
  • GitHub repo (dongri)

18 releases (5 stable)

2.0.2 Feb 6, 2026
2.0.1 Jun 5, 2025
1.0.2 Apr 11, 2025
1.0.1 Dec 24, 2024
0.1.3 Sep 26, 2019

#56 in Email

Download history 327/week @ 2026-01-02 253/week @ 2026-01-09 272/week @ 2026-01-16 258/week @ 2026-01-23 372/week @ 2026-01-30 254/week @ 2026-02-06 544/week @ 2026-02-13 312/week @ 2026-02-20 206/week @ 2026-02-27 180/week @ 2026-03-06 294/week @ 2026-03-13 112/week @ 2026-03-20 126/week @ 2026-03-27 122/week @ 2026-04-03 178/week @ 2026-04-10 267/week @ 2026-04-17

707 downloads per month
Used in 2 crates

MIT license

450KB
287 lines

mailgun-rs

An unofficial client library for the Mailgun API

# Cargo.toml
[dependencies]
mailgun-rs = "2.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 = "dongri.org";
    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@dongri.org");

    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@dongri.org");

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

Send an email with attachments

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@dongri.org");
    let attachments = vec![
        Attachment::builder()
            .path("/path/to/attachment-1.txt".to_string())
            .attachment_type(AttachmentType::Attachment)
            .build(),
        Attachment::builder()
            .path("/path/to/attachment-2.txt".to_string())
            .attachment_type(AttachmentType::Inline)
            .build(),
    ];
    match client.send(MailgunRegion::US, &sender, message, Some(attachments)) {
        Ok(_) => {
            println!("successful");
        }
        Err(err) => {
            println!("Error: {err}");
        }
    }
}

Dependencies

~4–21MB
~254K SLoC

  • reqwest 0.13+charset+http2+json+blocking+multipart+stream
  • serde+derive
  • serde_json
  • thiserror 2.0
  • typed-builder 0.23.2

Other features

  • native-tls
  • rustls-tls
See also: sendgrid, postmark, mailjet_api_wrapper, aws-sdk-workmail, aws-sdk-pinpointemail, fusionauth-rust-client, aws-sdk-workmailmessageflow, aws-sdk-mailmanager, email-cli, clerk-rs, resend-rs

Lib.rs is an unofficial list of Rust/Cargo crates, created by kornelski. It contains data from multiple sources, including heuristics, and manually curated data. Content of this page is not necessarily endorsed by the authors of the crate. This site is not affiliated with nor endorsed by the Rust Project. If something is missing or incorrect, please file a bug.