minco-plugin-notifications 1.2.2

Provider-neutral notification and rich outbound mail ports with deterministic local test adapters for Minco
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
use crate::{
    MailAddress, MailError, MailErrorKind, MailMessage, MailReceipt, MailTransport, render_mime,
};
use async_trait::async_trait;
use chrono::Utc;
use std::{fmt, net::SocketAddr, time::Duration};
use tokio::{
    io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
    net::TcpStream,
    time::timeout,
};

const MAX_SMTP_RESPONSE_BYTES: usize = 16 * 1024;

#[derive(Debug, Clone)]
pub struct MailpitTransportConfig {
    pub endpoint: SocketAddr,
    pub from: MailAddress,
    pub connect_timeout: Duration,
    pub command_timeout: Duration,
}

impl MailpitTransportConfig {
    pub fn new(endpoint: SocketAddr, from: MailAddress) -> Result<Self, MailError> {
        if !endpoint.ip().is_loopback() {
            return Err(MailError::new(
                MailErrorKind::Configuration,
                "mailpit",
                "Mailpit SMTP endpoint must be loopback-only",
            ));
        }
        from.validate()?;
        Ok(Self {
            endpoint,
            from,
            connect_timeout: Duration::from_secs(2),
            command_timeout: Duration::from_secs(3),
        })
    }
}

impl Default for MailpitTransportConfig {
    fn default() -> Self {
        Self::new(
            SocketAddr::from(([127, 0, 0, 1], 1025)),
            MailAddress::new("minco@localhost").expect("static local address"),
        )
        .expect("static loopback Mailpit configuration")
    }
}

#[derive(Clone)]
pub struct MailpitTransport {
    config: MailpitTransportConfig,
}

impl MailpitTransport {
    pub fn new(config: MailpitTransportConfig) -> Result<Self, MailError> {
        if !config.endpoint.ip().is_loopback()
            || config.connect_timeout.is_zero()
            || config.command_timeout.is_zero()
        {
            return Err(MailError::new(
                MailErrorKind::Configuration,
                "mailpit",
                "Mailpit transport configuration is invalid",
            ));
        }
        config.from.validate()?;
        Ok(Self { config })
    }
}

impl Default for MailpitTransport {
    fn default() -> Self {
        Self::new(MailpitTransportConfig::default()).expect("static Mailpit configuration")
    }
}

impl fmt::Debug for MailpitTransport {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("MailpitTransport")
            .field("endpoint", &self.config.endpoint)
            .field("from", &"[REDACTED]")
            .field("connect_timeout", &self.config.connect_timeout)
            .field("command_timeout", &self.config.command_timeout)
            .finish()
    }
}

#[async_trait]
impl MailTransport for MailpitTransport {
    fn name(&self) -> &'static str {
        "mailpit"
    }

    async fn send(&self, message: &MailMessage, attempt: u32) -> Result<MailReceipt, MailError> {
        message.validate()?;
        let mime = render_mime(message, &self.config.from)?;
        let stream = timeout(
            self.config.connect_timeout,
            TcpStream::connect(self.config.endpoint),
        )
        .await
        .map_err(|_| {
            MailError::new(
                MailErrorKind::Unavailable,
                self.name(),
                "Mailpit SMTP connection timed out",
            )
        })?
        .map_err(|_| {
            MailError::new(
                MailErrorKind::Unavailable,
                self.name(),
                "Mailpit SMTP endpoint is unavailable",
            )
        })?;
        let mut connection = SmtpConnection::new(stream, self.config.command_timeout);

        connection.expect_response(220, false).await?;
        connection
            .command("EHLO minco.local\r\n", 250, false)
            .await?;
        connection
            .command(
                &format!("MAIL FROM:<{}>\r\n", self.config.from.address),
                250,
                false,
            )
            .await?;
        for recipient in message.recipients() {
            connection
                .command(&format!("RCPT TO:<{}>\r\n", recipient.address), 250, false)
                .await?;
        }
        connection.command("DATA\r\n", 354, false).await?;
        connection.write_message(&mime).await?;
        connection.expect_response(250, true).await?;
        let _ = connection.command("QUIT\r\n", 221, true).await;

        Ok(MailReceipt {
            message_id: message.id,
            transport: self.name().into(),
            provider_message_id: format!("mailpit:{}", message.id),
            accepted_at: Utc::now(),
            attempt,
        })
    }
}

struct SmtpConnection {
    stream: BufReader<TcpStream>,
    command_timeout: Duration,
}

impl SmtpConnection {
    fn new(stream: TcpStream, command_timeout: Duration) -> Self {
        Self {
            stream: BufReader::new(stream),
            command_timeout,
        }
    }

    async fn command(
        &mut self,
        command: &str,
        expected: u16,
        after_data: bool,
    ) -> Result<String, MailError> {
        self.write_all(command.as_bytes(), after_data).await?;
        self.expect_response(expected, after_data).await
    }

    async fn write_message(&mut self, mime: &[u8]) -> Result<(), MailError> {
        let mut body = dot_stuff(mime);
        if !body.ends_with(b"\r\n") {
            body.extend_from_slice(b"\r\n");
        }
        body.extend_from_slice(b".\r\n");
        self.write_all(&body, true).await
    }

    async fn write_all(&mut self, bytes: &[u8], after_data: bool) -> Result<(), MailError> {
        timeout(self.command_timeout, async {
            self.stream.get_mut().write_all(bytes).await?;
            self.stream.get_mut().flush().await
        })
        .await
        .map_err(|_| smtp_io_error(after_data, "SMTP write timed out"))?
        .map_err(|_| smtp_io_error(after_data, "SMTP connection closed during write"))
    }

    async fn expect_response(
        &mut self,
        expected: u16,
        after_data: bool,
    ) -> Result<String, MailError> {
        let (status, response) = timeout(self.command_timeout, self.read_response())
            .await
            .map_err(|_| smtp_io_error(after_data, "SMTP response timed out"))?
            .map_err(|_| smtp_io_error(after_data, "SMTP connection closed during response"))?;
        if status == expected {
            return Ok(response);
        }
        let kind = match status / 100 {
            4 if after_data => MailErrorKind::Unavailable,
            4 => MailErrorKind::Unavailable,
            5 => MailErrorKind::Rejected,
            _ if after_data => MailErrorKind::Ambiguous,
            _ => MailErrorKind::Protocol,
        };
        Err(MailError::new(
            kind,
            "mailpit",
            format!("SMTP server returned status {status}"),
        ))
    }

    async fn read_response(&mut self) -> std::io::Result<(u16, String)> {
        let mut complete = String::new();
        let mut status = None;
        loop {
            let mut line = String::new();
            if self.stream.read_line(&mut line).await? == 0 {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::UnexpectedEof,
                    "SMTP response ended unexpectedly",
                ));
            }
            if complete.len() + line.len() > MAX_SMTP_RESPONSE_BYTES {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "SMTP response exceeds the bounded size",
                ));
            }
            let bytes = line.as_bytes();
            if bytes.len() < 4 || !bytes[..3].iter().all(u8::is_ascii_digit) {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "SMTP response is malformed",
                ));
            }
            let code = line[..3].parse::<u16>().map_err(|_| {
                std::io::Error::new(std::io::ErrorKind::InvalidData, "SMTP status is malformed")
            })?;
            if status
                .replace(code)
                .is_some_and(|previous| previous != code)
            {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "SMTP multiline status changed",
                ));
            }
            let continuation = bytes[3] == b'-';
            if !continuation && bytes[3] != b' ' {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    "SMTP response separator is malformed",
                ));
            }
            complete.push_str(line.trim_end_matches(['\r', '\n']));
            complete.push('\n');
            if !continuation {
                return Ok((code, complete));
            }
        }
    }
}

fn smtp_io_error(after_data: bool, message: &str) -> MailError {
    MailError::new(
        if after_data {
            MailErrorKind::Ambiguous
        } else {
            MailErrorKind::Unavailable
        },
        "mailpit",
        message,
    )
}

fn dot_stuff(message: &[u8]) -> Vec<u8> {
    let mut output = Vec::with_capacity(message.len() + 16);
    let mut at_line_start = true;
    for byte in message {
        if at_line_start && *byte == b'.' {
            output.push(b'.');
        }
        output.push(*byte);
        at_line_start = *byte == b'\n';
    }
    output
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::MailMessage;
    use tokio::{
        io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
        net::TcpListener,
    };

    #[tokio::test]
    async fn loopback_smtp_captures_rich_mime_without_bcc_header() {
        let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
        let endpoint = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            let (stream, _) = listener.accept().await.unwrap();
            let mut stream = BufReader::new(stream);
            stream
                .get_mut()
                .write_all(b"220 mailpit ESMTP\r\n")
                .await
                .unwrap();
            let mut captured = Vec::new();
            let mut recipients = Vec::new();
            loop {
                let mut line = String::new();
                if stream.read_line(&mut line).await.unwrap() == 0 {
                    break;
                }
                if line.starts_with("EHLO") {
                    stream
                        .get_mut()
                        .write_all(b"250-mailpit\r\n250 8BITMIME\r\n")
                        .await
                        .unwrap();
                } else if line.starts_with("MAIL FROM") {
                    stream.get_mut().write_all(b"250 ok\r\n").await.unwrap();
                } else if line.starts_with("RCPT TO") {
                    recipients.push(line.trim().to_owned());
                    stream.get_mut().write_all(b"250 ok\r\n").await.unwrap();
                } else if line == "DATA\r\n" {
                    stream
                        .get_mut()
                        .write_all(b"354 send data\r\n")
                        .await
                        .unwrap();
                    loop {
                        let mut data_line = Vec::new();
                        stream.read_until(b'\n', &mut data_line).await.unwrap();
                        if data_line == b".\r\n" {
                            break;
                        }
                        captured.extend_from_slice(&data_line);
                    }
                    stream
                        .get_mut()
                        .write_all(b"250 accepted\r\n")
                        .await
                        .unwrap();
                } else if line.starts_with("QUIT") {
                    stream.get_mut().write_all(b"221 bye\r\n").await.unwrap();
                    break;
                }
            }
            (captured, recipients)
        });

        let transport = MailpitTransport::new(
            MailpitTransportConfig::new(
                endpoint,
                MailAddress::new("no-reply@example.com").unwrap(),
            )
            .unwrap(),
        )
        .unwrap();
        let message = MailMessage::builder("account.welcome", "Welcome")
            .to(MailAddress::new("person@example.com").unwrap())
            .bcc(MailAddress::new("audit@example.com").unwrap())
            .text("Hello")
            .html("<p>Hello</p>")
            .build()
            .unwrap();
        let receipt = transport.send(&message, 1).await.unwrap();
        assert_eq!(receipt.transport, "mailpit");
        let (captured, recipients) = server.await.unwrap();
        let captured = String::from_utf8(captured).unwrap();
        assert!(
            recipients
                .iter()
                .any(|recipient| recipient == "RCPT TO:<audit@example.com>")
        );
        assert!(!captured.contains("Bcc:"));
        assert!(!captured.contains("audit@example.com"));
        assert!(captured.contains("multipart/alternative"));
    }

    #[test]
    fn remote_plaintext_smtp_is_rejected() {
        let endpoint = SocketAddr::from(([192, 0, 2, 1], 1025));
        assert!(
            MailpitTransportConfig::new(
                endpoint,
                MailAddress::new("no-reply@example.com").unwrap()
            )
            .is_err()
        );
    }

    #[test]
    fn dot_stuffing_covers_every_line_start() {
        assert_eq!(dot_stuff(b".a\r\n.b\r\n"), b"..a\r\n..b\r\n");
    }
}