rustlavel-mail 0.2.1

Rustlavel mail: MIME messages, an SMTP client written on Tokio, and multi-channel notifications
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Where a message actually goes: a mail server, the log, or a directory.
//!
//! The transport is a boot-time decision read from configuration, so nothing
//! in an application changes between a developer's laptop and production —
//! only `mail.transport` does.

use crate::address::Address;
use crate::config::{MailConfig, TransportKind};
use crate::fake::Fake;
use crate::mailable::Mailable;
use crate::message::Message;
use crate::smtp::{SmtpClient, SmtpConfig};
use rustlavel_core::events::Event;
use rustlavel_core::{Config, Error, Result};
use rustlavel_view::Engine;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::time::Instant;

/// A boxed future borrowed from the transport and the message it is sending.
///
/// The trait has to be dyn-compatible — the factory decides at boot which
/// transport an application has — and `async fn` in a trait is not.
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

/// Something that can deliver a message.
pub trait Transport: Send + Sync + 'static {
    /// The transport's name, as it appears in the `mail.sent` event.
    fn name(&self) -> &'static str;

    fn send<'a>(&'a self, message: &'a Message) -> BoxFuture<'a, Result<()>>;
}

/// Talks to a real SMTP server, one connection per message.
///
/// Connection reuse is not worth the complexity here: mail is not a hot path,
/// and a pooled SMTP connection that has gone stale fails in ways that are far
/// harder to explain than a reconnect.
pub struct SmtpTransport {
    settings: SmtpConfig,
}

impl SmtpTransport {
    pub fn new(settings: SmtpConfig) -> SmtpTransport {
        SmtpTransport { settings }
    }
}

impl Transport for SmtpTransport {
    fn name(&self) -> &'static str {
        "smtp"
    }

    fn send<'a>(&'a self, message: &'a Message) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move {
            let from = message
                .sender()
                .ok_or_else(|| Error::msg("this message has no From address"))?
                .email()
                .to_string();
            let recipients = message.envelope_recipients();
            let data = message.to_smtp_data()?;

            let mut client = SmtpClient::connect(&self.settings).await?;
            let result = client.send_message(&from, &recipients, &data).await;
            client.quit().await;
            result
        })
    }
}

/// Writes the rendered message to the log.
///
/// The local development default: mail you can read without a mail server, an
/// inbox, or a network — and without the risk of a test mailing a customer.
pub struct LogTransport {
    /// Off for the fake, which records rather than reports.
    write: bool,
}

impl Default for LogTransport {
    fn default() -> Self {
        LogTransport::new()
    }
}

impl LogTransport {
    pub fn new() -> LogTransport {
        LogTransport { write: true }
    }

    /// A transport that does nothing at all, for [`crate::Mail::fake`].
    pub fn silent() -> LogTransport {
        LogTransport { write: false }
    }
}

impl Transport for LogTransport {
    fn name(&self) -> &'static str {
        "log"
    }

    fn send<'a>(&'a self, message: &'a Message) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move {
            let mime = message.to_mime()?;
            if self.write {
                rustlavel_core::info!(
                    "mail to {}\n{mime}",
                    message.envelope_recipients().join(", ")
                );
            }
            Ok(())
        })
    }
}

/// Writes one `.eml` per message under a directory.
///
/// `.eml` is what every mail client opens, so a designer can double-click the
/// file and see the message exactly as a recipient would — including the
/// attachments, which a log line cannot show.
pub struct FileTransport {
    directory: PathBuf,
}

impl FileTransport {
    pub fn new(directory: impl Into<PathBuf>) -> FileTransport {
        FileTransport { directory: directory.into() }
    }

    pub fn directory(&self) -> &Path {
        &self.directory
    }
}

impl Transport for FileTransport {
    fn name(&self) -> &'static str {
        "file"
    }

    fn send<'a>(&'a self, message: &'a Message) -> BoxFuture<'a, Result<()>> {
        Box::pin(async move {
            let mime = message.to_mime()?;

            std::fs::create_dir_all(&self.directory).map_err(|error| {
                Error::msg(format!(
                    "cannot write mail to `{}`: {error}. Check mail.path.",
                    self.directory.display()
                ))
            })?;

            let path = self.directory.join(filename(message));
            std::fs::write(&path, mime).map_err(|error| {
                Error::msg(format!("cannot write `{}`: {error}", path.display()))
            })?;

            rustlavel_core::debug!("mail written to {}", path.display());
            Ok(())
        })
    }
}

/// A name a human can scan in a directory listing: when, and what about.
fn filename(message: &Message) -> String {
    let slug: String = message
        .subject_text()
        .chars()
        .map(|c| if c.is_ascii_alphanumeric() { c.to_ascii_lowercase() } else { '-' })
        .collect::<String>()
        .trim_matches('-')
        .split('-')
        .filter(|part| !part.is_empty())
        .collect::<Vec<_>>()
        .join("-");

    let slug = if slug.is_empty() { "message".to_string() } else { slug.chars().take(60).collect() };
    format!("{}-{slug}.eml", crate::encode::unique_token())
}

/// The application's handle on mail.
///
/// Cloning is cheap and shares one transport, so a handler can hold a `Mailer`
/// in context the same way it holds a database pool.
#[derive(Clone)]
pub struct Mailer {
    transport: Arc<dyn Transport>,
    from: Option<Address>,
    fake: Option<Arc<Fake>>,
}

impl Mailer {
    pub fn new(transport: impl Transport) -> Mailer {
        Mailer { transport: Arc::new(transport), from: None, fake: None }
    }

    /// Build the transport named in `mail.transport`.
    ///
    /// Deliberately does not connect: an application must boot even while the
    /// mail server is restarting.
    pub fn from_config(config: &Config) -> Result<Mailer> {
        Mailer::build(&MailConfig::from_app_config(config)?)
    }

    pub fn build(settings: &MailConfig) -> Result<Mailer> {
        let transport: Arc<dyn Transport> = match settings.transport {
            TransportKind::Smtp => Arc::new(SmtpTransport::new(settings.smtp())),
            TransportKind::Log => Arc::new(LogTransport::new()),
            TransportKind::File => Arc::new(FileTransport::new(settings.path.clone())),
        };

        Ok(Mailer { transport, from: settings.from()?, fake: None })
    }

    /// The sender used by any message that does not set its own.
    pub fn from(mut self, address: Address) -> Mailer {
        self.from = Some(address);
        self
    }

    /// Record instead of sending. This is `Mail::fake()`.
    pub fn faking(mut self, fake: Fake) -> Mailer {
        let fake = Arc::new(fake);
        self.fake = Some(Arc::clone(&fake));
        self.transport = fake;
        self
    }

    /// The recorder, when this mailer is faking.
    pub fn fake(&self) -> Option<&Arc<Fake>> {
        self.fake.as_ref()
    }

    pub fn transport_name(&self) -> &'static str {
        self.transport.name()
    }

    /// Send one message.
    pub async fn send(&self, message: Message) -> Result<()> {
        let message = match &self.from {
            Some(address) => message.default_from(address.clone()),
            None => message,
        };
        message.validate()?;

        let started = Instant::now();
        let result = self.transport.send(&message).await;
        let elapsed = started.elapsed();

        result.map_err(|error| {
            Error::msg(format!(
                "could not send `{}` to {}: {error}",
                message.subject_text(),
                message.envelope_recipients().join(", ")
            ))
        })?;

        // Recipients and subject, never the body and never a credential: this
        // event is written to a Telescope database an operator can read.
        Event::new("mail.sent")
            .with("to", message.envelope_recipients().join(", "))
            .with("subject", message.subject_text())
            .with("transport", self.transport.name())
            .took(elapsed)
            .dispatch();

        Ok(())
    }

    /// Render a [`Mailable`] and send it to one recipient.
    pub async fn send_mailable<M: Mailable + ?Sized>(
        &self,
        engine: &Engine,
        to: impl crate::address::IntoAddress,
        mailable: &M,
    ) -> Result<()> {
        let message = mailable.build(engine)?.to(to);
        self.send(message).await
    }

    /// Assert a message went to this address. Only meaningful while faking.
    #[track_caller]
    pub fn assert_sent_to(&self, address: &str) {
        self.recorder().assert_sent_to(address);
    }

    #[track_caller]
    pub fn assert_sent_times(&self, expected: usize) {
        self.recorder().assert_sent_times(expected);
    }

    #[track_caller]
    pub fn assert_nothing_sent(&self) {
        self.recorder().assert_nothing_sent();
    }

    #[track_caller]
    fn recorder(&self) -> &Fake {
        self.fake
            .as_deref()
            .expect("this mailer is not faking — build it with `Mail::fake()` before asserting")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rustlavel_core::events;

    fn message() -> Message {
        Message::new()
            .from(("Rustlavel", "no-reply@example.com"))
            .to("ada@example.com")
            .subject("Your receipt")
            .text("Thank you.")
    }

    /// Each test writes its own directory: tests run concurrently, and a shared
    /// one would be read while another test was writing into it.
    fn fixture(test: &str) -> PathBuf {
        let directory = std::env::temp_dir().join(format!("rustlavel-mail-{test}"));
        let _ = std::fs::remove_dir_all(&directory);
        directory
    }

    #[tokio::test]
    async fn the_log_transport_renders_the_message_and_reports_success() {
        let mailer = Mailer::new(LogTransport::new());

        assert_eq!(mailer.transport_name(), "log");
        mailer.send(message()).await.unwrap();
    }

    #[tokio::test]
    async fn the_file_transport_writes_one_eml_per_message() {
        let directory = fixture("file-transport");
        let mailer = Mailer::new(FileTransport::new(&directory));

        mailer.send(message()).await.unwrap();
        mailer.send(message().subject("Second")).await.unwrap();

        let mut written: Vec<PathBuf> =
            std::fs::read_dir(&directory).unwrap().map(|e| e.unwrap().path()).collect();
        written.sort();

        assert_eq!(written.len(), 2);
        assert!(written.iter().all(|path| path.extension().is_some_and(|e| e == "eml")));
        assert!(
            written.iter().any(|path| path.to_string_lossy().contains("your-receipt")),
            "the subject should be in the filename: {written:?}"
        );

        let body = std::fs::read_to_string(&written[0]).unwrap();
        assert!(body.contains("From: Rustlavel <no-reply@example.com>\r\n"), "{body}");
        assert!(body.contains("Thank you."));

        let _ = std::fs::remove_dir_all(&directory);
    }

    #[tokio::test]
    async fn the_file_transport_explains_a_directory_it_cannot_create() {
        // A path under a regular file cannot become a directory.
        let blocker = fixture("file-blocked");
        std::fs::create_dir_all(&blocker).unwrap();
        let file = blocker.join("not-a-directory");
        std::fs::write(&file, b"x").unwrap();

        let mailer = Mailer::new(FileTransport::new(file.join("mail")));
        let error = mailer.send(message()).await.unwrap_err().to_string();

        assert!(error.contains("mail.path"), "{error}");
        let _ = std::fs::remove_dir_all(&blocker);
    }

    #[tokio::test]
    async fn the_configured_sender_fills_in_a_message_that_has_none() {
        let config = Config::new();
        config.set("mail.transport", "log");
        config.set("mail.from.address", "no-reply@example.com");
        config.set("mail.from.name", "Rustlavel");

        let mailer = Mailer::from_config(&config).unwrap();
        let sent = Message::new().to("ada@example.com").subject("Hi").text("hi");

        mailer.send(sent).await.unwrap();
    }

    #[tokio::test]
    async fn a_message_with_no_sender_at_all_is_refused_before_any_transport_runs() {
        let mailer = Mailer::new(LogTransport::new());

        let error = mailer
            .send(Message::new().to("ada@example.com").subject("Hi"))
            .await
            .unwrap_err()
            .to_string();

        assert!(error.contains("mail.from.address"), "{error}");
    }

    #[tokio::test]
    async fn a_sent_message_dispatches_an_event_without_the_body() {
        // The event bus is process-wide and other tests in this crate send mail
        // at the same time, so the subscriber keeps only the message this test
        // sent, identified by a subject nothing else uses.
        const SUBJECT: &str = "event-probe-2c1f";

        events::clear_subscribers();
        let seen = Arc::new(std::sync::Mutex::new(Vec::<Event>::new()));
        let sink = Arc::clone(&seen);
        events::subscribe(move |event: &Event| {
            let mine = event.field("subject").and_then(rustlavel_core::Json::as_str)
                == Some(SUBJECT);
            if event.kind == "mail.sent" && mine {
                sink.lock().unwrap().push(event.clone());
            }
        });

        Mailer::new(LogTransport::silent())
            .send(message().subject(SUBJECT).text("a secret body nobody should record"))
            .await
            .unwrap();

        let recorded = seen.lock().unwrap();
        let event = recorded.first().expect("a mail.sent event");

        assert_eq!(
            event.field("to").and_then(rustlavel_core::Json::as_str),
            Some("ada@example.com")
        );
        assert_eq!(
            event.field("transport").and_then(rustlavel_core::Json::as_str),
            Some("log")
        );
        assert!(event.duration.is_some());

        let rendered = format!("{:?}", event.fields);
        assert!(!rendered.contains("secret body"), "the body reached the event: {rendered}");

        drop(recorded);
        events::clear_subscribers();
    }

    #[tokio::test]
    async fn a_transport_failure_names_the_subject_and_the_recipients() {
        struct Broken;
        impl Transport for Broken {
            fn name(&self) -> &'static str {
                "broken"
            }
            fn send<'a>(&'a self, _message: &'a Message) -> BoxFuture<'a, Result<()>> {
                Box::pin(async { Err(Error::msg("the server hung up")) })
            }
        }

        let error = Mailer::new(Broken).send(message()).await.unwrap_err().to_string();

        assert!(error.contains("Your receipt"), "{error}");
        assert!(error.contains("ada@example.com"), "{error}");
        assert!(error.contains("the server hung up"), "{error}");
    }

    #[tokio::test]
    async fn the_smtp_transport_reports_a_server_it_cannot_reach() {
        // Nothing listens on port 1. The protocol itself is covered against a
        // scripted duplex server in `smtp::client`; what matters here is that
        // the transport's failure says where it was trying to go.
        let settings = SmtpConfig::new("127.0.0.1", 1);
        let mailer = Mailer::new(SmtpTransport::new(settings));

        assert_eq!(mailer.transport_name(), "smtp");

        let error = mailer.send(message()).await.unwrap_err().to_string();
        assert!(error.contains("127.0.0.1:1"), "{error}");
        assert!(error.contains("Your receipt"), "{error}");
    }

    #[tokio::test]
    async fn a_mailable_can_be_rendered_and_sent_in_one_call() {
        use rustlavel_view::EXTENSION;

        let root = fixture("send-mailable");
        let path = root.join(format!("mail/hello.{EXTENSION}"));
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, "<p>Hello {{ name }}</p>").unwrap();

        struct Hello;
        impl Mailable for Hello {
            fn subject(&self) -> String {
                "Hello".into()
            }
            fn view(&self) -> &str {
                "mail.hello"
            }
            fn context(&self) -> rustlavel_view::Context {
                rustlavel_view::Context::new().with("name", "Ada")
            }
        }

        let mailer = crate::Mail::fake().from(Address::new("no-reply@example.com").unwrap());
        mailer.send_mailable(&Engine::new(&root), "ada@example.com", &Hello).await.unwrap();

        mailer.assert_sent_to("ada@example.com");
        let sent = &mailer.fake().unwrap().sent()[0];
        assert_eq!(sent.subject_text(), "Hello");
        assert_eq!(sent.html_body(), Some("<p>Hello Ada</p>"));
        assert_eq!(sent.text_body(), Some("Hello Ada"));

        let _ = std::fs::remove_dir_all(&root);
    }

    #[test]
    fn a_filename_survives_a_subject_that_is_not_a_filename() {
        let awkward = Message::new().subject("Re: your / order #7!!");
        let name = filename(&awkward);

        assert!(name.ends_with("-re-your-order-7.eml"), "{name}");
        assert!(!filename(&Message::new()).is_empty());
        assert!(filename(&Message::new()).contains("message"));
    }
}