1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use data_encoding::BASE64;
use serde::Serialize;
#[derive(Default, Serialize)]
pub struct Message {
from: Address,
subject: String,
personalizations: Vec<super::Personalization>,
#[serde(skip_serializing_if = "Option::is_none")]
content: Option<Vec<Content>>,
#[serde(skip_serializing_if = "Option::is_none")]
attachments: Option<Vec<Attachment>>,
#[serde(skip_serializing_if = "Option::is_none")]
template_id: Option<String>,
}
impl Message {
pub fn new() -> Message {
Message::default()
}
pub fn set_from(mut self, from: Address) -> Message {
self.from = from;
self
}
pub fn set_subject(mut self, subject: &str) -> Message {
self.subject = String::from(subject);
self
}
pub fn set_template_id(mut self, template_id: &str) -> Message {
self.template_id = Some(String::from(template_id));
self
}
pub fn add_content(mut self, c: Content) -> Message {
match self.content {
None => {
let mut content = Vec::new();
content.push(c);
self.content = Some(content);
}
Some(ref mut content) => content.push(c),
};
self
}
pub fn add_personalization(mut self, p: super::Personalization) -> Message {
self.personalizations.push(p);
self
}
pub fn add_attachment(mut self, a: Attachment) -> Message {
match self.attachments {
None => {
let mut attachments = Vec::new();
attachments.push(a);
self.attachments = Some(attachments);
}
Some(ref mut attachments) => attachments.push(a),
};
self
}
}
#[derive(Clone, Default, Serialize)]
pub struct Address {
email: String,
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
}
impl Address {
pub fn new() -> Address {
Address::default()
}
pub fn set_email(mut self, email: &str) -> Address {
self.email = String::from(email);
self
}
pub fn set_name(mut self, name: &str) -> Address {
self.name = Some(String::from(name));
self
}
}
#[derive(Clone, Default, Serialize)]
pub struct Content {
#[serde(rename = "type")]
content_type: String,
value: String,
}
impl Content {
pub fn new() -> Content {
Content::default()
}
pub fn set_content_type(mut self, content_type: &str) -> Content {
self.content_type = String::from(content_type);
self
}
pub fn set_value(mut self, value: &str) -> Content {
self.value = String::from(value);
self
}
}
#[derive(Default, Serialize)]
pub struct Attachment {
content: String,
filename: String,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
disposition: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
content_id: Option<String>,
}
impl Attachment {
pub fn new() -> Attachment {
Attachment::default()
}
pub fn set_content(mut self, c: &[u8]) -> Attachment {
self.content = BASE64.encode(c);
self
}
pub fn set_base64_content(mut self, c: &str) -> Attachment {
self.content = String::from(c);
self
}
pub fn set_filename(mut self, filename: &str) -> Attachment {
self.filename = filename.into();
self
}
pub fn set_mime_type(mut self, mime: &str) -> Attachment {
self.mime_type = Some(String::from(mime));
self
}
}