1use crate::{Address, Attachment, Body, MailerError};
4
5#[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 #[must_use]
21 pub fn new() -> Self {
22 Self::default()
23 }
24
25 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 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 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 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 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 #[must_use]
92 pub fn subject(mut self, subject: impl Into<String>) -> Self {
93 self.subject = subject.into();
94 self
95 }
96
97 #[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 #[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 #[must_use]
121 pub fn body(mut self, body: Body) -> Self {
122 self.body = body;
123 self
124 }
125
126 #[must_use]
128 pub fn attach(mut self, attachment: Attachment) -> Self {
129 self.attachments.push(attachment);
130 self
131 }
132
133 #[must_use]
135 pub fn embed(mut self, attachment: Attachment) -> Self {
136 self.attachments.push(attachment);
137 self
138 }
139
140 #[must_use]
142 pub fn text_part(&self) -> Option<&str> {
143 self.body.text_part()
144 }
145
146 #[must_use]
148 pub fn html_part(&self) -> Option<&str> {
149 self.body.html_part()
150 }
151
152 #[must_use]
154 pub fn mime_tree(&self) -> crate::MimeTree {
155 crate::MimeTree::from_email(self)
156 }
157
158 #[must_use]
160 pub fn from_addresses(&self) -> &[Address] {
161 &self.from
162 }
163
164 #[must_use]
166 pub fn to_addresses(&self) -> &[Address] {
167 &self.to
168 }
169
170 #[must_use]
172 pub fn cc_addresses(&self) -> &[Address] {
173 &self.cc
174 }
175
176 #[must_use]
178 pub fn bcc_addresses(&self) -> &[Address] {
179 &self.bcc
180 }
181
182 #[must_use]
184 pub fn reply_to_addresses(&self) -> &[Address] {
185 &self.reply_to
186 }
187
188 #[must_use]
190 pub fn subject_line(&self) -> &str {
191 &self.subject
192 }
193
194 #[must_use]
196 pub const fn message_body(&self) -> &Body {
197 &self.body
198 }
199
200 #[must_use]
202 pub fn attachments(&self) -> &[Attachment] {
203 &self.attachments
204 }
205}