https://github.com/dongri/mailgun-rs
Mailgun API for Rust
https://github.com/dongri/mailgun-rs
mailgun rust
Last synced: 19 days ago
JSON representation
Mailgun API for Rust
- Host: GitHub
- URL: https://github.com/dongri/mailgun-rs
- Owner: dongri
- License: mit
- Created: 2019-09-09T13:09:10.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-12T09:55:07.000Z (20 days ago)
- Last Synced: 2025-04-13T07:12:18.623Z (19 days ago)
- Topics: mailgun, rust
- Language: Rust
- Homepage: https://crates.io/crates/mailgun-rs
- Size: 111 KB
- Stars: 17
- Watchers: 2
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mailgun-rs
An unofficial client library for the Mailgun API
```toml
# Cargo.toml
[dependencies]
mailgun-rs = "1.0.2"
```### Examples
### Send with async
See [examples/async](examples/async)```
$ cd examples/async
$ cargo run
```#### Send a simple email
```rust
use mailgun_rs::{EmailAddress, Mailgun, MailgunRegion, Message};
use std::collections::HashMap;fn main() {
let domain = "huatuo.xyz";
let key = "key-xxxxxx";
let recipient = "[email protected]";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("hello from mailgun
"),
..Default::default()
};let client = Mailgun {
api_key: String::from(key),
domain: String::from(domain),
};
let sender = EmailAddress::name_address("no-reply", "[email protected]");match client.send(MailgunRegion::US, &sender, message, None) {
Ok(_) => {
println!("successful");
}
Err(err) => {
println!("Error: {err}");
}
}
}
```### Send a template email
```rust
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", "[email protected]");match client.send(MailgunRegion::US, &sender, message, None) {
Ok(_) => {
println!("successful");
}
Err(err) => {
println!("Error: {err}");
}
}
}
```#### Send an email with attachments
```rust
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("hello from mailgun with attachments
"),
..Default::default()
};let client = Mailgun {
api_key: String::from(key),
domain: String::from(domain),
};
let sender = EmailAddress::name_address("no-reply", "[email protected]");
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}");
}
}
}
```