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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
use crate::misc::{MiscDetails, MiscError};
use crate::mx::{MxDetails, MxError};
use crate::smtp::{SmtpDetails, SmtpError, SmtpErrorDesc};
use crate::syntax::SyntaxDetails;
use async_smtp::{ClientSecurity, ClientTlsParameters};
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
use std::time::Duration;
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
pub struct CheckEmailInputProxy {
pub host: String,
pub port: u16,
pub username: Option<String>,
pub password: Option<String>,
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub enum SmtpSecurity {
None,
Opportunistic,
Required,
Wrapper,
}
impl SmtpSecurity {
pub fn to_client_security(self, tls_params: ClientTlsParameters) -> ClientSecurity {
match self {
Self::None => ClientSecurity::None,
Self::Opportunistic => ClientSecurity::Opportunistic(tls_params),
Self::Required => ClientSecurity::Required(tls_params),
Self::Wrapper => ClientSecurity::Wrapper(tls_params),
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CheckEmailInput {
pub to_emails: Vec<String>,
pub from_email: String,
pub hello_name: String,
pub proxy: Option<CheckEmailInputProxy>,
pub smtp_port: u16,
pub smtp_timeout: Option<Duration>,
pub yahoo_use_api: bool,
pub retries: usize,
pub smtp_security: SmtpSecurity,
}
impl Default for CheckEmailInput {
fn default() -> Self {
CheckEmailInput {
to_emails: vec![],
from_email: "user@example.org".into(),
hello_name: "localhost".into(),
proxy: None,
smtp_port: 25,
smtp_security: SmtpSecurity::None,
smtp_timeout: None,
yahoo_use_api: true,
retries: 2,
}
}
}
impl CheckEmailInput {
pub fn new(to_emails: Vec<String>) -> CheckEmailInput {
CheckEmailInput {
to_emails,
..Default::default()
}
}
#[deprecated(since = "0.8.24", note = "Please use set_from_email instead")]
pub fn from_email(&mut self, email: String) -> &mut CheckEmailInput {
self.from_email = email;
self
}
pub fn set_from_email(&mut self, email: String) -> &mut CheckEmailInput {
self.from_email = email;
self
}
#[deprecated(since = "0.8.24", note = "Please use set_hello_name instead")]
pub fn hello_name(&mut self, name: String) -> &mut CheckEmailInput {
self.hello_name = name;
self
}
pub fn set_hello_name(&mut self, name: String) -> &mut CheckEmailInput {
self.hello_name = name;
self
}
#[deprecated(since = "0.8.24", note = "Please use set_proxy instead")]
pub fn proxy(&mut self, proxy_host: String, proxy_port: u16) -> &mut CheckEmailInput {
self.proxy = Some(CheckEmailInputProxy {
host: proxy_host,
port: proxy_port,
..Default::default()
});
self
}
pub fn set_proxy(&mut self, proxy: CheckEmailInputProxy) -> &mut CheckEmailInput {
self.proxy = Some(proxy);
self
}
pub fn set_retries(&mut self, retries: usize) -> &mut CheckEmailInput {
self.retries = retries;
self
}
#[deprecated(since = "0.8.24", note = "Please use set_smtp_timeout instead")]
pub fn smtp_timeout(&mut self, duration: Duration) -> &mut CheckEmailInput {
self.smtp_timeout = Some(duration);
self
}
pub fn set_smtp_port(&mut self, port: u16) -> &mut CheckEmailInput {
self.smtp_port = port;
self
}
pub fn set_smtp_security(&mut self, smtp_security: SmtpSecurity) -> &mut CheckEmailInput {
self.smtp_security = smtp_security;
self
}
pub fn set_smtp_timeout(&mut self, duration: Duration) -> &mut CheckEmailInput {
self.smtp_timeout = Some(duration);
self
}
#[deprecated(since = "0.8.24", note = "Please use set_yahoo_use_api instead")]
pub fn yahoo_use_api(&mut self, use_api: bool) -> &mut CheckEmailInput {
self.yahoo_use_api = use_api;
self
}
pub fn set_yahoo_use_api(&mut self, use_api: bool) -> &mut CheckEmailInput {
self.yahoo_use_api = use_api;
self
}
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Reachable {
Safe,
Risky,
Invalid,
Unknown,
}
#[derive(Debug)]
pub struct CheckEmailOutput {
pub input: String,
pub is_reachable: Reachable,
pub misc: Result<MiscDetails, MiscError>,
pub mx: Result<MxDetails, MxError>,
pub smtp: Result<SmtpDetails, SmtpError>,
pub syntax: SyntaxDetails,
}
impl Default for CheckEmailOutput {
fn default() -> Self {
CheckEmailOutput {
input: String::default(),
is_reachable: Reachable::Unknown,
misc: Ok(MiscDetails::default()),
mx: Ok(MxDetails::default()),
smtp: Ok(SmtpDetails::default()),
syntax: SyntaxDetails::default(),
}
}
}
impl Serialize for CheckEmailOutput {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
#[derive(Serialize)]
struct MyError<E> {
error: E,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<SmtpErrorDesc>,
}
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("input", &self.input)?;
map.serialize_entry("is_reachable", &self.is_reachable)?;
match &self.misc {
Ok(t) => map.serialize_entry("misc", &t)?,
Err(error) => map.serialize_entry(
"misc",
&MyError {
error,
description: None,
},
)?,
}
match &self.mx {
Ok(t) => map.serialize_entry("mx", &t)?,
Err(error) => map.serialize_entry(
"mx",
&MyError {
error,
description: None,
},
)?,
}
match &self.smtp {
Ok(t) => map.serialize_entry("smtp", &t)?,
Err(error) => map.serialize_entry(
"smtp",
&MyError {
error,
description: error.get_description(),
},
)?,
}
map.serialize_entry("syntax", &self.syntax)?;
map.end()
}
}
#[cfg(test)]
mod tests {
use super::CheckEmailOutput;
use async_smtp::smtp::response::{Category, Code, Detail, Response, Severity};
#[test]
fn should_serialize_correctly() {
fn dummy_response_with_message(m: &str) -> CheckEmailOutput {
let r = Response::new(
Code {
severity: Severity::TransientNegativeCompletion,
category: Category::MailSystem,
detail: Detail::Zero,
},
vec![m.to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()],
);
CheckEmailOutput {
input: "foo".to_string(),
is_reachable: super::Reachable::Unknown,
misc: Ok(super::MiscDetails::default()),
mx: Ok(super::MxDetails::default()),
syntax: super::SyntaxDetails::default(),
smtp: Err(super::SmtpError::SmtpError(r.into())),
}
}
let res = dummy_response_with_message("blacklist");
let actual = serde_json::to_string(&res).unwrap();
let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: blacklist"},"description":"IpBlacklisted"},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#;
assert_eq!(expected, actual);
let res =
dummy_response_with_message("Client host rejected: cannot find your reverse hostname");
let actual = serde_json::to_string(&res).unwrap();
let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: Client host rejected: cannot find your reverse hostname"},"description":"NeedsRDNS"},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#;
assert_eq!(expected, actual);
let res = dummy_response_with_message("foobar");
let actual = serde_json::to_string(&res).unwrap();
let expected = r#"{"input":"foo","is_reachable":"unknown","misc":{"is_disposable":false,"is_role_account":false},"mx":{"accepts_mail":false,"records":[]},"smtp":{"error":{"type":"SmtpError","message":"transient: foobar"}},"syntax":{"address":null,"domain":"","is_valid_syntax":false,"username":""}}"#;
assert_eq!(expected, actual);
}
}