swadhinbiswas 📧 p4: mail EmailMessage + MailerState · 7/25/2026
549bf99
67 lines · 1.7 KB
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
use super::{Attachment, EmailAddress};

/// An email message ready to be sent.
#[derive(Debug, Clone)]
pub struct EmailMessage {
    pub to: Vec<EmailAddress>,
    pub cc: Vec<EmailAddress>,
    pub bcc: Vec<EmailAddress>,
    pub reply_to: Option<EmailAddress>,
    pub subject: String,
    pub text_body: Option<String>,
    pub html_body: Option<String>,
    pub attachments: Vec<Attachment>,
    pub headers: Vec<(String, String)>,
}

impl EmailMessage {
    pub fn new(to: Vec<EmailAddress>, subject: impl Into<String>) -> Self {
        Self {
            to,
            cc: Vec::new(),
            bcc: Vec::new(),
            reply_to: None,
            subject: subject.into(),
            text_body: None,
            html_body: None,
            attachments: Vec::new(),
            headers: Vec::new(),
        }
    }

    pub fn text(mut self, body: impl Into<String>) -> Self {
        self.text_body = Some(body.into());
        self
    }

    pub fn html(mut self, body: impl Into<String>) -> Self {
        self.html_body = Some(body.into());
        self
    }

    pub fn attach(mut self, attachment: Attachment) -> Self {
        self.attachments.push(attachment);
        self
    }

    pub fn cc(mut self, addresses: Vec<EmailAddress>) -> Self {
        self.cc = addresses;
        self
    }

    pub fn bcc(mut self, addresses: Vec<EmailAddress>) -> Self {
        self.bcc = addresses;
        self
    }

    pub fn reply_to(mut self, address: EmailAddress) -> Self {
        self.reply_to = Some(address);
        self
    }

    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }
}