1use std::{env, fs, path::Path};
2
3use mailgun_rs::{Attachment, AttachmentType, EmailAddress, Mailgun, MailgunRegion, Message};
4use std::collections::HashMap;
5
6fn main() {
7 let domain = &env::var("MAILGUN_DOMAIN").expect("MAILGUN_DOMAIN not set");
8 let key = &env::var("MAILGUN_API_KEY").expect("MAILGUN_API_KEY not set");
9 let recipient = "dongrium@gmail.com";
10
11 send_html(recipient, key, domain);
12 send_template(recipient, key, domain);
13 send_with_attachment(recipient, key, domain);
14 send_with_inline_attachment(recipient, key, domain);
15 println!("All emails sent successfully.");
16}
17
18fn send_html(recipient: &str, key: &str, domain: &str) {
19 let recipient = EmailAddress::address(recipient);
20 let message = Message {
21 to: vec![recipient],
22 subject: String::from("mailgun-rs"),
23 html: String::from("<h1>hello from mailgun</h1>"),
24 ..Default::default()
25 };
26 let client = Mailgun {
27 api_key: String::from(key),
28 domain: String::from(domain),
29 };
30 let sender = EmailAddress::name_address("no-reply", "no-reply@huatuo.xyz");
31
32 match client.send(MailgunRegion::US, &sender, message, None) {
33 Ok(_) => {
34 println!("successful");
35 }
36 Err(err) => {
37 println!("Error: {err}");
38 }
39 }
40}
41
42fn send_template(recipient: &str, key: &str, domain: &str) {
43 let mut template_vars = HashMap::new();
44 template_vars.insert(String::from("name"), String::from("Dongri Jin"));
45 let recipient = EmailAddress::address(recipient);
46 let message = Message {
47 to: vec![recipient],
48 subject: String::from("mailgun-rs"),
49 template: String::from("template-1"),
50 template_vars,
51 ..Default::default()
52 };
53 let client = Mailgun {
54 api_key: String::from(key),
55 domain: String::from(domain),
56 };
57 let sender = EmailAddress::name_address("no-reply", "no-reply@huatuo.xyz");
58
59 match client.send(MailgunRegion::US, &sender, message, None) {
60 Ok(_) => {
61 println!("successful");
62 }
63 Err(err) => {
64 println!("Error: {err}");
65 }
66 }
67}
68
69fn send_with_attachment(recipient: &str, key: &str, domain: &str) {
70 let recipient = EmailAddress::address(recipient);
71 let message = Message {
72 to: vec![recipient],
73 subject: String::from("mailgun-rs"),
74 html: String::from("<h1>hello from mailgun with attachment</h1>"),
75 ..Default::default()
76 };
77
78 let mut attachments = Vec::new();
79 for item in ["file-1", "file-2"] {
80 let file_name = format!("examples/attachments/sample-{item}.txt");
81 let absolute_path =
82 fs::canonicalize(Path::new(&file_name)).expect("cannot get absolute path");
83
84 attachments.push(
85 Attachment::builder()
86 .path(absolute_path.to_string_lossy().to_string())
87 .attachment_type(AttachmentType::Attachment)
88 .build(),
89 );
90 }
91
92 let client = Mailgun {
93 api_key: String::from(key),
94 domain: String::from(domain),
95 };
96
97 let sender = EmailAddress::name_address("no-reply", "no-reply@huatuo.xyz");
98
99 match client.send(MailgunRegion::US, &sender, message, Some(attachments)) {
100 Ok(_) => {
101 println!("successful");
102 }
103 Err(err) => {
104 println!("Error: {err}");
105 }
106 }
107}
108
109fn send_with_inline_attachment(recipient: &str, key: &str, domain: &str) {
110 let recipient = EmailAddress::address(recipient);
111 let message = Message {
112 to: vec![recipient],
113 subject: String::from("mailgun-rs"),
114 html: String::from(
115 "<h1>hello from mailgun with inline attachment</h1><img src=\"cid:inline.png\">",
116 ),
117 ..Default::default()
118 };
119
120 let mut attachments = Vec::new();
121 let file_name = "examples/attachments/sushi.png";
122 let absolute_path = fs::canonicalize(Path::new(&file_name)).expect("cannot get absolute path");
123
124 attachments.push(
126 Attachment::builder()
127 .path(absolute_path.to_string_lossy().to_string())
128 .attachment_type(AttachmentType::Inline)
129 .build(),
130 );
131
132 let client = Mailgun {
133 api_key: String::from(key),
134 domain: String::from(domain),
135 };
136
137 let sender = EmailAddress::name_address("no-reply", "no-reply@huatuo.xyz");
138
139 match client.send(MailgunRegion::US, &sender, message, Some(attachments)) {
140 Ok(_) => {
141 println!("successful");
142 }
143 Err(err) => {
144 println!("Error: {err}");
145 }
146 }
147}
148
149