rusmes-smtp 0.1.2

Async SMTP server for RusMES — RFC 5321 compliant with STARTTLS, AUTH (PLAIN/LOGIN/CRAM-MD5/SCRAM-SHA-256), PIPELINING, DSN, and BDAT/CHUNKING
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! SMTP mail transport client implementation
//!
//! Provides [`SmtpMailTransport`] which implements [`MailTransport`] from
//! `rusmes_core` using a minimal RFC 5321 SMTP client over a plain TCP socket
//! (with optional AUTH LOGIN).
//!
//! When an [`crate::outbound_pool::OutboundPool`] is attached via
//! [`SmtpMailTransport::with_pool`] the transport reuses established
//! connections for consecutive deliveries to the same host instead of opening
//! a new TCP connection per message.

use crate::outbound_pool::{smtp_read_response_raw, smtp_write, OutboundPool, PooledConn};
use async_trait::async_trait;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use chrono::{DateTime, Utc};
use rusmes_core::transport::{MailTransport, SmtpEnvelope};
use rusmes_proto::{Mail, MessageBody};
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::Duration;
use tokio::io::{AsyncWriteExt, BufReader};
use tokio::net::TcpStream;
use tokio::sync::{Mutex, Notify};
use uuid::Uuid;

// ── Internal queue entry ─────────────────────────────────────────────────────

/// An entry held in the in-memory scheduled-send queue.
#[derive(Debug)]
struct QueuedSend {
    id: String,
    envelope: SmtpEnvelope,
    /// Serialised RFC 5322 message bytes (headers + body).
    message_bytes: Vec<u8>,
    deliver_at: DateTime<Utc>,
}

// ── Configuration ────────────────────────────────────────────────────────────

/// Connection parameters for the upstream SMTP relay.
#[derive(Debug, Clone)]
pub struct SmtpRelayConfig {
    pub host: String,
    pub port: u16,
    pub username: Option<String>,
    pub password: Option<String>,
    /// Per-connection I/O timeout.
    pub timeout: Duration,
}

impl Default for SmtpRelayConfig {
    fn default() -> Self {
        Self {
            host: "localhost".to_string(),
            port: 25,
            username: None,
            password: None,
            timeout: Duration::from_secs(30),
        }
    }
}

// ── SmtpMailTransport ────────────────────────────────────────────────────────

/// SMTP mail transport that delivers messages to a relay host.
///
/// Immediate sends open a TCP connection (or reuse a pooled one), handshake,
/// and deliver within [`MailTransport::send`].  Scheduled sends are queued in
/// memory and a background worker drains them when their `deliver_at` instant
/// is reached.
///
/// ## Connection pooling
///
/// Attach an [`OutboundPool`] via [`SmtpMailTransport::with_pool`] to reuse
/// SMTP connections across deliveries.  Without a pool every send opens and
/// closes a fresh TCP connection.
pub struct SmtpMailTransport {
    config: SmtpRelayConfig,
    queue: Arc<Mutex<VecDeque<QueuedSend>>>,
    notify: Arc<Notify>,
    /// Signals the background worker to stop.
    shutdown: Arc<tokio::sync::watch::Sender<bool>>,
    /// Optional outbound connection pool.  When `None` every delivery opens a
    /// fresh TCP connection (backward-compatible behaviour).
    pool: Option<Arc<OutboundPool>>,
}

impl SmtpMailTransport {
    /// Create a new transport and start the background drain worker.
    ///
    /// The worker shuts down when the returned `SmtpMailTransport` is dropped.
    pub fn new(
        host: String,
        port: u16,
        username: Option<String>,
        password: Option<String>,
    ) -> Self {
        let config = SmtpRelayConfig {
            host,
            port,
            username,
            password,
            ..Default::default()
        };

        let queue: Arc<Mutex<VecDeque<QueuedSend>>> = Arc::new(Mutex::new(VecDeque::new()));
        let notify = Arc::new(Notify::new());
        let (tx, mut rx) = tokio::sync::watch::channel(false);
        let shutdown = Arc::new(tx);

        // Spawn background worker that drains due entries.
        let worker_queue = queue.clone();
        let worker_notify = notify.clone();
        let worker_config = config.clone();

        tokio::spawn(async move {
            loop {
                tokio::select! {
                    // Wait for a notification (new entry enqueued) or a timeout.
                    _ = worker_notify.notified() => {}
                    _ = tokio::time::sleep(Duration::from_secs(5)) => {}
                    _ = rx.changed() => {
                        // Shutdown requested — exit the loop.
                        break;
                    }
                }

                let now = Utc::now();
                let due_entries: Vec<QueuedSend> = {
                    let mut q = worker_queue.lock().await;
                    let mut due = Vec::new();
                    while let Some(front) = q.front() {
                        if front.deliver_at <= now {
                            if let Some(entry) = q.pop_front() {
                                due.push(entry);
                            }
                        } else {
                            break;
                        }
                    }
                    due
                };

                for entry in due_entries {
                    if let Err(e) =
                        deliver_via_smtp(&entry.envelope, &entry.message_bytes, &worker_config)
                            .await
                    {
                        tracing::error!("Scheduled send {} failed: {}", entry.id, e);
                    } else {
                        tracing::info!("Scheduled send {} delivered", entry.id);
                    }
                }
            }
        });

        Self {
            config,
            queue,
            notify,
            shutdown,
            pool: None,
        }
    }

    /// Attach an outbound connection pool so that consecutive deliveries to the
    /// same relay can reuse established TCP connections.
    ///
    /// Call this immediately after [`SmtpMailTransport::new`] before the
    /// transport is used.
    pub fn with_pool(mut self, pool: Arc<OutboundPool>) -> Self {
        self.pool = Some(pool);
        self
    }
}

impl Drop for SmtpMailTransport {
    fn drop(&mut self) {
        // Signal the background worker to exit gracefully.
        let _ = self.shutdown.send(true);
    }
}

#[async_trait]
impl MailTransport for SmtpMailTransport {
    async fn send(&self, envelope: SmtpEnvelope, mail: &Mail) -> anyhow::Result<String> {
        let msg_bytes = serialize_message(mail).await;
        deliver_via_smtp_pooled(&envelope, &msg_bytes, &self.config, self.pool.as_deref()).await?;
        let id = Uuid::new_v4().to_string();
        Ok(id)
    }

    async fn send_at(
        &self,
        envelope: SmtpEnvelope,
        mail: &Mail,
        at: DateTime<Utc>,
    ) -> anyhow::Result<String> {
        let id = Uuid::new_v4().to_string();
        let threshold = Utc::now() + chrono::Duration::seconds(5);

        if at <= threshold {
            // Near-immediate — deliver right away.
            let msg_bytes = serialize_message(mail).await;
            deliver_via_smtp(&envelope, &msg_bytes, &self.config).await?;
            return Ok(id);
        }

        // Enqueue for later delivery, inserting in sorted order.
        let entry = QueuedSend {
            id: id.clone(),
            envelope,
            message_bytes: serialize_message(mail).await,
            deliver_at: at,
        };

        {
            let mut q = self.queue.lock().await;
            // Insert in deliver_at order (earliest first) for efficient front-pop.
            let pos = q.iter().position(|e| e.deliver_at > at).unwrap_or(q.len());
            q.insert(pos, entry);
        }

        // Wake the background worker so it can compute the next sleep interval.
        self.notify.notify_one();

        Ok(id)
    }

    async fn cancel(&self, submission_id: &str) -> anyhow::Result<bool> {
        let mut q = self.queue.lock().await;
        if let Some(pos) = q.iter().position(|e| e.id == submission_id) {
            q.remove(pos);
            Ok(true)
        } else {
            Ok(false)
        }
    }
}

// ── Serialisation ────────────────────────────────────────────────────────────

/// Convert a [`Mail`] object to RFC 5322 wire bytes (headers + blank line + body).
///
/// For `MessageBody::Large`, the bytes are read asynchronously before serialisation.
pub(crate) async fn serialize_message(mail: &Mail) -> Vec<u8> {
    let mut data: Vec<u8> = Vec::new();

    for (name, values) in mail.message().headers().iter() {
        for value in values {
            data.extend_from_slice(name.as_bytes());
            data.extend_from_slice(b": ");
            data.extend_from_slice(value.as_bytes());
            data.extend_from_slice(b"\r\n");
        }
    }

    data.extend_from_slice(b"\r\n");

    match mail.message().body() {
        MessageBody::Small(bytes) => {
            data.extend_from_slice(bytes);
        }
        MessageBody::Large(large) => match large.read_to_bytes().await {
            Ok(bytes) => {
                data.extend_from_slice(&bytes);
            }
            Err(e) => {
                tracing::warn!("Failed to read large message body for SMTP delivery: {e}");
            }
        },
    }

    data
}

// ── Low-level SMTP client ────────────────────────────────────────────────────

/// Deliver pre-serialised `msg_bytes` through an existing pooled connection.
///
/// Expects the connection to already have completed greeting + EHLO (as done
/// by [`OutboundPool::get_or_connect`]).  Sends MAIL FROM / RCPT TO / DATA /
/// message.  Does **not** send QUIT — the caller is responsible for returning
/// the connection to the pool.
async fn run_smtp_transaction(
    conn: &mut PooledConn,
    envelope: &SmtpEnvelope,
    msg_bytes: &[u8],
    config: &SmtpRelayConfig,
) -> anyhow::Result<()> {
    // AUTH LOGIN if credentials provided (only on fresh connections — reused
    // connections have already authenticated).  We send AUTH on every reuse
    // here for simplicity; servers that disallow re-auth will reject it, which
    // is treated as a fatal error and the connection is dropped.
    if let (Some(user), Some(pass)) = (&config.username, &config.password) {
        smtp_write(&mut conn.reader, "AUTH LOGIN\r\n").await?;
        let _ = smtp_read_response_raw(&mut conn.reader).await?;

        smtp_write(&mut conn.reader, &format!("{}\r\n", BASE64.encode(user))).await?;
        let _ = smtp_read_response_raw(&mut conn.reader).await?;

        smtp_write(&mut conn.reader, &format!("{}\r\n", BASE64.encode(pass))).await?;
        let auth_resp = smtp_read_response_raw(&mut conn.reader).await?;
        if !auth_resp.starts_with("235") {
            anyhow::bail!("SMTP AUTH LOGIN failed: {}", auth_resp.trim());
        }
    }

    // MAIL FROM
    smtp_write(
        &mut conn.reader,
        &format!("MAIL FROM:<{}>\r\n", envelope.mail_from),
    )
    .await?;
    let mf_resp = smtp_read_response_raw(&mut conn.reader).await?;
    if !mf_resp.starts_with("250") {
        anyhow::bail!("SMTP MAIL FROM rejected: {}", mf_resp.trim());
    }

    // RCPT TO
    if envelope.rcpt_to.is_empty() {
        anyhow::bail!("No recipients in SmtpEnvelope");
    }
    let mut accepted = 0usize;
    for rcpt in &envelope.rcpt_to {
        smtp_write(&mut conn.reader, &format!("RCPT TO:<{}>\r\n", rcpt)).await?;
        let rcpt_resp = smtp_read_response_raw(&mut conn.reader).await?;
        if rcpt_resp.starts_with("250") || rcpt_resp.starts_with("251") {
            accepted += 1;
        } else {
            tracing::warn!("RCPT TO <{}> rejected: {}", rcpt, rcpt_resp.trim());
        }
    }
    if accepted == 0 {
        anyhow::bail!("All RCPT TO addresses rejected by relay");
    }

    // DATA
    smtp_write(&mut conn.reader, "DATA\r\n").await?;
    let data_resp = smtp_read_response_raw(&mut conn.reader).await?;
    if !data_resp.starts_with("354") {
        anyhow::bail!("SMTP DATA command failed: {}", data_resp.trim());
    }

    // Send message bytes + terminating dot.
    {
        let writer = conn.reader.get_mut();
        writer.write_all(msg_bytes).await?;
        if !msg_bytes.ends_with(b"\r\n") {
            writer.write_all(b"\r\n").await?;
        }
        writer.write_all(b".\r\n").await?;
        writer.flush().await?;
    }

    let send_resp = smtp_read_response_raw(&mut conn.reader).await?;
    if !send_resp.starts_with("250") {
        anyhow::bail!("SMTP message send rejected: {}", send_resp.trim());
    }

    Ok(())
}

/// Deliver pre-serialised `msg_bytes` to the relay described by `config`,
/// optionally reusing a connection from `pool`.
///
/// If `pool` is `Some`, the function tries to get a pooled connection; on
/// success returns it to the pool after delivery.  On delivery error the
/// connection is dropped (not returned).  If `pool` is `None`, a fresh
/// connection is opened and closed (QUIT) after delivery.
async fn deliver_via_smtp_pooled(
    envelope: &SmtpEnvelope,
    msg_bytes: &[u8],
    config: &SmtpRelayConfig,
    pool: Option<&OutboundPool>,
) -> anyhow::Result<()> {
    let addr = format!("{}:{}", config.host, config.port);

    if let Some(p) = pool {
        // Pooled path.
        let mut conn = tokio::time::timeout(config.timeout, p.get_or_connect(&addr))
            .await
            .map_err(|_| anyhow::anyhow!("SMTP pool get_or_connect timeout for {}", addr))??;

        match run_smtp_transaction(&mut conn, envelope, msg_bytes, config).await {
            Ok(()) => {
                p.return_conn(conn).await;
                Ok(())
            }
            Err(e) => {
                // Drop conn (not returned) on error.
                Err(e)
            }
        }
    } else {
        // Non-pooled path — classic open / deliver / quit.
        deliver_via_smtp_direct(envelope, msg_bytes, config).await
    }
}

/// Open a fresh TCP connection, run the full SMTP session (greeting + EHLO +
/// transaction + QUIT) and close.
async fn deliver_via_smtp_direct(
    envelope: &SmtpEnvelope,
    msg_bytes: &[u8],
    config: &SmtpRelayConfig,
) -> anyhow::Result<()> {
    let addr = format!("{}:{}", config.host, config.port);

    let stream = tokio::time::timeout(config.timeout, TcpStream::connect(&addr))
        .await
        .map_err(|_| anyhow::anyhow!("SMTP connection timeout to {}", addr))??;

    let mut reader = BufReader::new(stream);

    // Read greeting (220 …)
    let greeting = smtp_read_response_raw(&mut reader).await?;
    if !greeting.starts_with("220") {
        anyhow::bail!("Unexpected SMTP greeting: {}", greeting.trim());
    }

    // EHLO
    smtp_write(&mut reader, &format!("EHLO {}\r\n", config.host)).await?;
    let ehlo = smtp_read_response_raw(&mut reader).await?;
    tracing::debug!("SMTP EHLO: {}", ehlo.trim());

    // Wrap in a synthetic PooledConn to reuse run_smtp_transaction.
    let mut conn = PooledConn {
        reader,
        last_used: std::time::SystemTime::now(),
        extensions: crate::outbound_pool::SmtpExtensions::from_ehlo(&ehlo),
        remote_key: addr.clone(),
    };

    run_smtp_transaction(&mut conn, envelope, msg_bytes, config).await?;

    // QUIT
    smtp_write(&mut conn.reader, "QUIT\r\n").await?;
    let _ = smtp_read_response_raw(&mut conn.reader).await;

    Ok(())
}

/// Keep backward compatibility: the old `deliver_via_smtp` name is now an alias.
async fn deliver_via_smtp(
    envelope: &SmtpEnvelope,
    msg_bytes: &[u8],
    config: &SmtpRelayConfig,
) -> anyhow::Result<()> {
    deliver_via_smtp_direct(envelope, msg_bytes, config).await
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use bytes::Bytes;
    use rusmes_proto::{HeaderMap, MailAddress, MessageBody, MimeMessage};
    use std::str::FromStr;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    fn make_mail(sender: &str, recipient: &str) -> Mail {
        let mut headers = HeaderMap::new();
        headers.insert("From", sender);
        headers.insert("To", recipient);
        headers.insert("Subject", "Test");
        let body = MessageBody::Small(Bytes::from("Hello, world!"));
        let msg = MimeMessage::new(headers, body);
        Mail::new(
            Some(MailAddress::from_str(sender).expect("addr")),
            vec![MailAddress::from_str(recipient).expect("addr")],
            msg,
            None,
            None,
        )
    }

    /// Spawn a minimal SMTP echo server that returns 250 to everything.
    async fn spawn_mock_smtp(responses: Vec<String>) -> u16 {
        let listener = TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind mock smtp");
        let port = listener.local_addr().expect("local addr").port();

        tokio::spawn(async move {
            if let Ok((mut socket, _)) = listener.accept().await {
                for resp in responses {
                    socket
                        .write_all(resp.as_bytes())
                        .await
                        .expect("write response");
                }
                // Drain any incoming data so the client doesn't get ECONNRESET.
                let mut buf = [0u8; 4096];
                let _ = socket.read(&mut buf).await;
            }
        });

        port
    }

    #[tokio::test]
    async fn test_serialize_message() {
        let mail = make_mail("sender@example.com", "recipient@example.com");
        let bytes = serialize_message(&mail).await;
        let text = String::from_utf8_lossy(&bytes);
        assert!(text.contains("from: sender@example.com"));
        assert!(text.contains("to: recipient@example.com"));
        assert!(text.contains("Hello, world!"));
    }

    #[tokio::test]
    async fn test_smtp_mail_transport_send() {
        // Responses for: greeting, EHLO, MAIL FROM, RCPT TO, DATA, (message), QUIT
        let responses = vec![
            "220 localhost ESMTP\r\n".to_string(),
            "250-localhost\r\n250 PIPELINING\r\n".to_string(),
            "250 OK\r\n".to_string(),
            "250 OK\r\n".to_string(),
            "354 Go ahead\r\n".to_string(),
            "250 Queued\r\n".to_string(),
            "221 Bye\r\n".to_string(),
        ];

        let port = spawn_mock_smtp(responses).await;

        let transport = SmtpMailTransport::new("127.0.0.1".to_string(), port, None, None);

        let envelope = SmtpEnvelope {
            mail_from: "sender@example.com".to_string(),
            rcpt_to: vec!["recipient@example.com".to_string()],
        };

        let mail = make_mail("sender@example.com", "recipient@example.com");
        let result = transport.send(envelope, &mail).await;
        assert!(result.is_ok(), "send should succeed: {:?}", result);
        let id = result.expect("submission id");
        // Should be a valid UUID
        assert_eq!(id.len(), 36);
    }

    #[tokio::test]
    async fn test_smtp_mail_transport_send_at_immediate() {
        let responses = vec![
            "220 localhost ESMTP\r\n".to_string(),
            "250-localhost\r\n250 PIPELINING\r\n".to_string(),
            "250 OK\r\n".to_string(),
            "250 OK\r\n".to_string(),
            "354 Go ahead\r\n".to_string(),
            "250 Queued\r\n".to_string(),
            "221 Bye\r\n".to_string(),
        ];

        let port = spawn_mock_smtp(responses).await;
        let transport = SmtpMailTransport::new("127.0.0.1".to_string(), port, None, None);

        let envelope = SmtpEnvelope {
            mail_from: "sender@example.com".to_string(),
            rcpt_to: vec!["recipient@example.com".to_string()],
        };

        // Send at a time within the 5-second threshold → immediate delivery
        let at = Utc::now() + chrono::Duration::seconds(2);
        let mail = make_mail("sender@example.com", "recipient@example.com");
        let result = transport.send_at(envelope, &mail, at).await;
        assert!(
            result.is_ok(),
            "send_at immediate should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn test_smtp_mail_transport_send_at_queued() {
        // Future send - should be queued, not delivered immediately.
        let transport = SmtpMailTransport::new("127.0.0.1".to_string(), 9999, None, None);

        let envelope = SmtpEnvelope {
            mail_from: "sender@example.com".to_string(),
            rcpt_to: vec!["recipient@example.com".to_string()],
        };

        let at = Utc::now() + chrono::Duration::hours(2);
        let mail = make_mail("sender@example.com", "recipient@example.com");
        let id = transport
            .send_at(envelope, &mail, at)
            .await
            .expect("queue entry");

        // Entry should be in the queue
        let q = transport.queue.lock().await;
        assert_eq!(q.len(), 1);
        assert_eq!(q.front().map(|e| e.id.as_str()), Some(id.as_str()));
    }

    #[tokio::test]
    async fn test_smtp_mail_transport_cancel() {
        let transport = SmtpMailTransport::new("127.0.0.1".to_string(), 9999, None, None);

        let envelope = SmtpEnvelope {
            mail_from: "sender@example.com".to_string(),
            rcpt_to: vec!["recipient@example.com".to_string()],
        };

        let at = Utc::now() + chrono::Duration::hours(1);
        let mail = make_mail("sender@example.com", "recipient@example.com");
        let id = transport
            .send_at(envelope, &mail, at)
            .await
            .expect("queue entry");

        // Cancel it
        let canceled = transport.cancel(&id).await.expect("cancel ok");
        assert!(canceled, "cancel should return true for queued entry");

        // Queue should be empty
        let q = transport.queue.lock().await;
        assert!(q.is_empty());

        // Cancel again → returns false
        drop(q);
        let again = transport.cancel(&id).await.expect("cancel again ok");
        assert!(!again, "cancel of already-canceled should return false");
    }
}