Skip to main content

serenade_mailer/
email.rs

1//! Email message builder (Symfony `Email` analogue).
2
3use crate::{Address, Attachment, Body, MailerError};
4
5/// Outgoing email before a transport sends it.
6#[derive(Clone, Debug, Default, Eq, PartialEq)]
7pub struct Email {
8    from: Vec<Address>,
9    to: Vec<Address>,
10    cc: Vec<Address>,
11    bcc: Vec<Address>,
12    reply_to: Vec<Address>,
13    subject: String,
14    body: Body,
15    attachments: Vec<Attachment>,
16}
17
18impl Email {
19    /// Empty message.
20    #[must_use]
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    /// Adds a From address.
26    ///
27    /// # Errors
28    ///
29    /// Returns [`MailerError`] when `address` is invalid.
30    pub fn from(
31        mut self,
32        address: impl TryInto<Address, Error = MailerError>,
33    ) -> Result<Self, MailerError> {
34        self.from.push(address.try_into()?);
35        Ok(self)
36    }
37
38    /// Adds a To address.
39    ///
40    /// # Errors
41    ///
42    /// Returns [`MailerError`] when `address` is invalid.
43    pub fn to(
44        mut self,
45        address: impl TryInto<Address, Error = MailerError>,
46    ) -> Result<Self, MailerError> {
47        self.to.push(address.try_into()?);
48        Ok(self)
49    }
50
51    /// Adds a Cc address.
52    ///
53    /// # Errors
54    ///
55    /// Returns [`MailerError`] when `address` is invalid.
56    pub fn cc(
57        mut self,
58        address: impl TryInto<Address, Error = MailerError>,
59    ) -> Result<Self, MailerError> {
60        self.cc.push(address.try_into()?);
61        Ok(self)
62    }
63
64    /// Adds a Bcc address.
65    ///
66    /// # Errors
67    ///
68    /// Returns [`MailerError`] when `address` is invalid.
69    pub fn bcc(
70        mut self,
71        address: impl TryInto<Address, Error = MailerError>,
72    ) -> Result<Self, MailerError> {
73        self.bcc.push(address.try_into()?);
74        Ok(self)
75    }
76
77    /// Adds a Reply-To address.
78    ///
79    /// # Errors
80    ///
81    /// Returns [`MailerError`] when `address` is invalid.
82    pub fn reply_to(
83        mut self,
84        address: impl TryInto<Address, Error = MailerError>,
85    ) -> Result<Self, MailerError> {
86        self.reply_to.push(address.try_into()?);
87        Ok(self)
88    }
89
90    /// Sets the subject.
91    #[must_use]
92    pub fn subject(mut self, subject: impl Into<String>) -> Self {
93        self.subject = subject.into();
94        self
95    }
96
97    /// Sets a text-only body (clears HTML unless [`Self::html`] is used after via [`Self::body`]).
98    #[must_use]
99    pub fn text(mut self, text: impl Into<String>) -> Self {
100        let html = self.body.html_part().map(str::to_owned);
101        self.body = match html {
102            Some(html) => Body::both(text, html),
103            None => Body::text(text),
104        };
105        self
106    }
107
108    /// Sets an HTML-only body (keeps text when already set).
109    #[must_use]
110    pub fn html(mut self, html: impl Into<String>) -> Self {
111        let text = self.body.text_part().map(str::to_owned);
112        self.body = match text {
113            Some(text) => Body::both(text, html),
114            None => Body::html(html),
115        };
116        self
117    }
118
119    /// Replaces the body.
120    #[must_use]
121    pub fn body(mut self, body: Body) -> Self {
122        self.body = body;
123        self
124    }
125
126    /// Adds a downloadable attachment.
127    #[must_use]
128    pub fn attach(mut self, attachment: Attachment) -> Self {
129        self.attachments.push(attachment);
130        self
131    }
132
133    /// Adds an inline (CID) part for HTML `cid:` references.
134    #[must_use]
135    pub fn embed(mut self, attachment: Attachment) -> Self {
136        self.attachments.push(attachment);
137        self
138    }
139
140    /// Plain-text body when set.
141    #[must_use]
142    pub fn text_part(&self) -> Option<&str> {
143        self.body.text_part()
144    }
145
146    /// HTML body when set.
147    #[must_use]
148    pub fn html_part(&self) -> Option<&str> {
149        self.body.html_part()
150    }
151
152    /// Mime multipart tree for this message (see [`crate::MimeTree`]).
153    #[must_use]
154    pub fn mime_tree(&self) -> crate::MimeTree {
155        crate::MimeTree::from_email(self)
156    }
157
158    /// From addresses.
159    #[must_use]
160    pub fn from_addresses(&self) -> &[Address] {
161        &self.from
162    }
163
164    /// To addresses.
165    #[must_use]
166    pub fn to_addresses(&self) -> &[Address] {
167        &self.to
168    }
169
170    /// Cc addresses.
171    #[must_use]
172    pub fn cc_addresses(&self) -> &[Address] {
173        &self.cc
174    }
175
176    /// Bcc addresses.
177    #[must_use]
178    pub fn bcc_addresses(&self) -> &[Address] {
179        &self.bcc
180    }
181
182    /// Reply-To addresses.
183    #[must_use]
184    pub fn reply_to_addresses(&self) -> &[Address] {
185        &self.reply_to
186    }
187
188    /// Subject line.
189    #[must_use]
190    pub fn subject_line(&self) -> &str {
191        &self.subject
192    }
193
194    /// Body parts.
195    #[must_use]
196    pub const fn message_body(&self) -> &Body {
197        &self.body
198    }
199
200    /// Attachments.
201    #[must_use]
202    pub fn attachments(&self) -> &[Attachment] {
203        &self.attachments
204    }
205}