polymail 0.1.3

Unified email sending interface for multiple providers
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use async_trait::async_trait;
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use lettre::message::header::{ContentType, HeaderName, HeaderValue};
use lettre::message::{Attachment as LettreAttachment, Mailbox, MultiPart, SinglePart};
use lettre::transport::smtp::Error as SmtpError;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{
    Address as LettreAddress, AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor,
};

use crate::email::{Body, Email};
use crate::error::SendError;
use crate::mailer::{Mailer, SendResult};

/// Transport security for the SMTP connection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SmtpTls {
    /// Plaintext, no encryption (e.g. mailcrab on 1025).
    None,
    /// Connect plaintext then upgrade via STARTTLS (typically port 587).
    StartTls,
    /// TLS from the first byte (typically port 465).
    Implicit,
}

fn map_smtp_error(e: SmtpError) -> SendError {
    if let Some(code) = e.status() {
        let code = u16::from(code);
        match code {
            535 | 530 | 534 | 538 | 454 => SendError::Authentication(e.to_string()),
            550 | 551 | 553 => SendError::HardBounce(e.to_string()),
            _ if (400..500).contains(&code) => SendError::ServiceUnavailable(e.to_string()),
            _ => SendError::Api {
                status: code,
                message: e.to_string(),
            },
        }
    } else {
        SendError::Provider(e.to_string())
    }
}

pub struct SmtpMailer {
    transport: AsyncSmtpTransport<Tokio1Executor>,
}

impl SmtpMailer {
    pub fn builder(host: impl Into<String>) -> SmtpBuilder {
        SmtpBuilder {
            host: host.into(),
            port: None,
            tls: SmtpTls::Implicit,
            credentials: None,
        }
    }

    /// Plaintext, no-auth mailer for local testing (e.g. mailcrab, MailHog).
    pub fn plaintext(host: impl Into<String>, port: u16) -> Self {
        let transport = AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(host.into())
            .port(port)
            .build();
        Self { transport }
    }

    /// Escape hatch for a fully-configured lettre transport (custom TLS roots,
    /// pool tuning, alternate auth mechanisms).
    pub fn with_transport(transport: AsyncSmtpTransport<Tokio1Executor>) -> Self {
        Self { transport }
    }
}

pub struct SmtpBuilder {
    host: String,
    port: Option<u16>,
    tls: SmtpTls,
    credentials: Option<(String, String)>,
}

impl SmtpBuilder {
    pub fn port(mut self, port: u16) -> Self {
        self.port = Some(port);
        self
    }

    pub fn tls(mut self, tls: SmtpTls) -> Self {
        self.tls = tls;
        self
    }

    pub fn credentials(mut self, user: impl Into<String>, pass: impl Into<String>) -> Self {
        self.credentials = Some((user.into(), pass.into()));
        self
    }

    pub fn build(self) -> Result<SmtpMailer, SendError> {
        if self.credentials.is_some() && self.tls == SmtpTls::None {
            return Err(SendError::Authentication(
                "refusing to send credentials over a plaintext (SmtpTls::None) connection".into(),
            ));
        }
        let mut b = match self.tls {
            SmtpTls::Implicit => {
                AsyncSmtpTransport::<Tokio1Executor>::relay(&self.host).map_err(map_smtp_error)?
            }
            SmtpTls::StartTls => AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&self.host)
                .map_err(map_smtp_error)?,
            SmtpTls::None => AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(&self.host),
        };
        if let Some(p) = self.port {
            b = b.port(p);
        }
        if let Some((u, pw)) = self.credentials {
            b = b.credentials(Credentials::new(u, pw));
        }
        Ok(SmtpMailer {
            transport: b.build(),
        })
    }
}

fn has_crlf(s: &str) -> bool {
    s.contains('\r') || s.contains('\n')
}

fn to_mailbox(addr: &crate::email::Address) -> Result<Mailbox, SendError> {
    // A CR/LF in the display name makes lettre's header encoder panic on send.
    if addr.name.as_deref().is_some_and(has_crlf) {
        return Err(SendError::InvalidAddress(format!(
            "{}: display name contains CR/LF",
            addr.email
        )));
    }
    let parsed = addr
        .email
        .parse::<LettreAddress>()
        .map_err(|e| SendError::InvalidAddress(format!("{}: {e}", addr.email)))?;
    Ok(Mailbox::new(addr.name.clone(), parsed))
}

enum Part {
    Single(SinglePart),
    Multi(MultiPart),
}

fn add_part(mp: MultiPart, part: Part) -> MultiPart {
    match part {
        Part::Single(sp) => mp.singlepart(sp),
        Part::Multi(m) => mp.multipart(m),
    }
}

fn body_part(body: &Body) -> Part {
    match body {
        Body::Text(t) => Part::Single(SinglePart::plain(t.clone())),
        Body::Html(h) => Part::Single(SinglePart::html(h.clone())),
        Body::Both { html, text } => Part::Multi(
            MultiPart::alternative()
                .singlepart(SinglePart::plain(text.clone()))
                .singlepart(SinglePart::html(html.clone())),
        ),
    }
}

fn attachment_content_type(raw: &str) -> ContentType {
    ContentType::parse(raw)
        .unwrap_or_else(|_| ContentType::parse("application/octet-stream").expect("valid mime"))
}

fn attachment_part(att: &crate::email::Attachment) -> Result<SinglePart, SendError> {
    let bytes = STANDARD
        .decode(&att.content)
        .map_err(|e| SendError::Serialization(e.to_string()))?;
    let ct = attachment_content_type(&att.content_type);
    let sp = match &att.content_id {
        Some(cid) => LettreAttachment::new_inline(cid.clone()).body(bytes, ct),
        None => LettreAttachment::new(att.filename.clone()).body(bytes, ct),
    };
    Ok(sp)
}

fn to_message(email: &Email) -> Result<Message, SendError> {
    let mut builder = Message::builder().from(to_mailbox(&email.from)?);
    for a in &email.to {
        builder = builder.to(to_mailbox(a)?);
    }
    for a in &email.cc {
        builder = builder.cc(to_mailbox(a)?);
    }
    for a in &email.bcc {
        builder = builder.bcc(to_mailbox(a)?);
    }
    if let Some(rt) = email.reply_to.first() {
        builder = builder.reply_to(to_mailbox(rt)?);
    }
    builder = builder.subject(email.subject.clone());
    for (k, v) in &email.headers {
        // lettre's HeaderName accepts CR/LF, which would inject arbitrary
        // headers (e.g. a hidden Bcc); reject them here.
        if has_crlf(k) || has_crlf(v) {
            return Err(SendError::Serialization(format!(
                "header {k:?} contains CR/LF"
            )));
        }
        let name = HeaderName::new_from_ascii(k.clone())
            .map_err(|e| SendError::Serialization(e.to_string()))?;
        builder = builder.raw_header(HeaderValue::new(name, v.clone()));
    }

    let (inline, regular): (Vec<_>, Vec<_>) = email
        .attachments
        .iter()
        .partition(|a| a.content_id.is_some());

    let content = if inline.is_empty() {
        body_part(&email.body)
    } else {
        let mut mp = add_part(MultiPart::related().build(), body_part(&email.body));
        for a in &inline {
            mp = mp.singlepart(attachment_part(a)?);
        }
        Part::Multi(mp)
    };

    let final_part = if regular.is_empty() {
        content
    } else {
        let mut mp = add_part(MultiPart::mixed().build(), content);
        for a in &regular {
            mp = mp.singlepart(attachment_part(a)?);
        }
        Part::Multi(mp)
    };

    let msg = match final_part {
        Part::Single(sp) => builder.singlepart(sp),
        Part::Multi(m) => builder.multipart(m),
    };
    msg.map_err(|e| SendError::Serialization(e.to_string()))
}

#[async_trait]
impl Mailer for SmtpMailer {
    async fn send(&self, email: &Email) -> Result<SendResult, SendError> {
        let msg = to_message(email)?;
        self.transport.send(msg).await.map_err(map_smtp_error)?;
        Ok(SendResult { message_id: None })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::email::{Address, Attachment};

    fn body_text() -> Body {
        Body::Text("plain body".into())
    }

    fn base_email(body: Body) -> Email {
        Email::builder(Address::new("sender@example.com"), "Subject", body)
            .to("recipient@example.com")
            .build()
            .unwrap()
    }

    fn formatted(email: &Email) -> String {
        String::from_utf8(to_message(email).unwrap().formatted()).unwrap()
    }

    #[test]
    fn text_body_is_plain() {
        let out = formatted(&base_email(Body::Text("hello text".into())));
        assert!(out.contains("Content-Type: text/plain"));
        assert!(out.contains("hello text"));
        assert!(!out.contains("multipart"));
    }

    #[test]
    fn html_body_is_html() {
        let out = formatted(&base_email(Body::Html("<p>hi</p>".into())));
        assert!(out.contains("Content-Type: text/html"));
        assert!(out.contains("<p>hi</p>"));
    }

    #[test]
    fn both_body_is_alternative() {
        let out = formatted(&base_email(Body::Both {
            html: "<p>rich</p>".into(),
            text: "poor".into(),
        }));
        assert!(out.contains("multipart/alternative"));
        assert!(out.contains("Content-Type: text/plain"));
        assert!(out.contains("Content-Type: text/html"));
    }

    #[test]
    fn regular_attachment_is_mixed_and_decoded() {
        // "aGVsbG8=" is base64 for "hello"; decoded before handoff, so "hello" appears and the base64 does not.
        let email = Email::builder(Address::new("s@example.com"), "S", body_text())
            .to("r@example.com")
            .attachment(Attachment {
                filename: "note.txt".into(),
                content: "aGVsbG8=".into(),
                content_type: "text/plain".into(),
                content_id: None,
            })
            .build()
            .unwrap();
        let out = formatted(&email);
        assert!(out.contains("multipart/mixed"));
        assert!(out.contains("note.txt"));
        assert!(out.contains("hello"));
        assert!(!out.contains("aGVsbG8="));
    }

    #[test]
    fn inline_attachment_is_related() {
        let email = Email::builder(Address::new("s@example.com"), "S", body_text())
            .to("r@example.com")
            .attachment(Attachment {
                filename: "logo.png".into(),
                content: "aGVsbG8=".into(),
                content_type: "image/png".into(),
                content_id: Some("logo123".into()),
            })
            .build()
            .unwrap();
        let out = formatted(&email);
        assert!(out.contains("multipart/related"));
        assert!(out.contains("logo123"));
    }

    #[test]
    fn inline_and_regular_nest_related_in_mixed() {
        let email = Email::builder(Address::new("s@example.com"), "S", body_text())
            .to("r@example.com")
            .attachment(Attachment {
                filename: "logo.png".into(),
                content: "aGVsbG8=".into(),
                content_type: "image/png".into(),
                content_id: Some("logo123".into()),
            })
            .attachment(Attachment {
                filename: "note.txt".into(),
                content: "aGVsbG8=".into(),
                content_type: "text/plain".into(),
                content_id: None,
            })
            .build()
            .unwrap();
        let out = formatted(&email);
        assert!(out.contains("multipart/mixed"));
        assert!(out.contains("multipart/related"));
        let mixed = out.find("multipart/mixed").unwrap();
        let related = out.find("multipart/related").unwrap();
        assert!(mixed < related);
    }

    #[test]
    fn explicit_headers_present_tags_metadata_absent() {
        let email = Email::builder(Address::new("s@example.com"), "S", body_text())
            .to("r@example.com")
            .header("X-Custom", "custom-value")
            .tag("secret-tag-xyz")
            .metadata("secret-meta-key", "secret-meta-value")
            .build()
            .unwrap();
        let out = formatted(&email);
        assert!(out.contains("X-Custom: custom-value"));
        assert!(!out.contains("secret-tag-xyz"));
        assert!(!out.contains("secret-meta-key"));
        assert!(!out.contains("secret-meta-value"));
    }

    #[test]
    fn invalid_address_errors() {
        let email = base_email(body_text());
        let mut bad = email;
        bad.from = Address::new("not-an-email");
        assert!(matches!(
            to_message(&bad),
            Err(SendError::InvalidAddress(_))
        ));
    }

    #[test]
    fn bad_base64_attachment_errors() {
        let email = Email::builder(Address::new("s@example.com"), "S", body_text())
            .to("r@example.com")
            .attachment(Attachment {
                filename: "note.txt".into(),
                content: "!!!not base64!!!".into(),
                content_type: "text/plain".into(),
                content_id: None,
            })
            .build()
            .unwrap();
        assert!(matches!(
            to_message(&email),
            Err(SendError::Serialization(_))
        ));
    }

    #[test]
    fn invalid_header_name_errors() {
        let email = Email::builder(Address::new("s@example.com"), "S", body_text())
            .to("r@example.com")
            .header("Bad Name", "value")
            .build()
            .unwrap();
        assert!(matches!(
            to_message(&email),
            Err(SendError::Serialization(_))
        ));
    }

    #[test]
    fn crlf_in_header_is_rejected() {
        let email = Email::builder(Address::new("s@example.com"), "S", body_text())
            .to("r@example.com")
            .header("X-Foo\r\nBcc: evil@example.com", "x")
            .build()
            .unwrap();
        assert!(matches!(
            to_message(&email),
            Err(SendError::Serialization(_))
        ));
    }

    #[test]
    fn crlf_in_display_name_is_rejected() {
        let email = Email::builder(
            Address::with_name("s@example.com", "Bad\r\nName"),
            "S",
            body_text(),
        )
        .to("r@example.com")
        .build()
        .unwrap();
        assert!(matches!(
            to_message(&email),
            Err(SendError::InvalidAddress(_))
        ));
    }

    #[test]
    fn credentials_over_plaintext_are_rejected() {
        let result = SmtpMailer::builder("localhost")
            .tls(SmtpTls::None)
            .credentials("user", "pass")
            .build();
        assert!(matches!(result, Err(SendError::Authentication(_))));
    }

    #[tokio::test]
    #[ignore = "requires a running SMTP sink on localhost:1025 (mailcrab in CI)"]
    async fn plaintext_send_returns_no_message_id() {
        let mailer = SmtpMailer::plaintext("localhost", 1025);
        let email = base_email(Body::Text("hello sink".into()));
        let result = mailer.send(&email).await.unwrap();
        assert!(result.message_id.is_none());
    }
}