swadhinbiswas

[email protected]
2026-07-25 12:46:14 +0600 committed NaN years ago
549bf99
commit 549bf99
2 files changed
+85 -0
▼crates/justapi-core/src/mail/message.rs
+66-0
1
+use super::{Attachment, EmailAddress};
2
+
3
+/// An email message ready to be sent.
4
+#[derive(Debug, Clone)]
5
+pub struct EmailMessage {
6
+ pub to: Vec<EmailAddress>,
7
+ pub cc: Vec<EmailAddress>,
8
+ pub bcc: Vec<EmailAddress>,
9
+ pub reply_to: Option<EmailAddress>,
10
+ pub subject: String,
11
+ pub text_body: Option<String>,
12
+ pub html_body: Option<String>,
13
+ pub attachments: Vec<Attachment>,
14
+ pub headers: Vec<(String, String)>,
15
+}
16
+
17
+impl EmailMessage {
18
+ pub fn new(to: Vec<EmailAddress>, subject: impl Into<String>) -> Self {
19
+ Self {
20
+ to,
21
+ cc: Vec::new(),
22
+ bcc: Vec::new(),
23
+ reply_to: None,
24
+ subject: subject.into(),
25
+ text_body: None,
26
+ html_body: None,
27
+ attachments: Vec::new(),
28
+ headers: Vec::new(),
29
+ }
30
+ }
31
+
32
+ pub fn text(mut self, body: impl Into<String>) -> Self {
33
+ self.text_body = Some(body.into());
34
+ self
35
+ }
36
+
37
+ pub fn html(mut self, body: impl Into<String>) -> Self {
38
+ self.html_body = Some(body.into());
39
+ self
40
+ }
41
+
42
+ pub fn attach(mut self, attachment: Attachment) -> Self {
43
+ self.attachments.push(attachment);
44
+ self
45
+ }
46
+
47
+ pub fn cc(mut self, addresses: Vec<EmailAddress>) -> Self {
48
+ self.cc = addresses;
49
+ self
50
+ }
51
+
52
+ pub fn bcc(mut self, addresses: Vec<EmailAddress>) -> Self {
53
+ self.bcc = addresses;
54
+ self
55
+ }
56
+
57
+ pub fn reply_to(mut self, address: EmailAddress) -> Self {
58
+ self.reply_to = Some(address);
59
+ self
60
+ }
61
+
62
+ pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
63
+ self.headers.push((name.into(), value.into()));
64
+ self
65
+ }
66
+}
0
67
▼crates/justapi-core/src/mail/state.rs
+19-0
1
+use std::sync::Arc;
2
+
3
+use super::{smtp::SmtpSender, SmtpConfig};
4
+
5
+/// Global mailer state shared across requests.
6
+pub struct MailerState {
7
+ pub config: SmtpConfig,
8
+ pub sender: SmtpSender,
9
+}
10
+
11
+impl MailerState {
12
+ pub fn new(config: SmtpConfig) -> Self {
13
+ let sender = SmtpSender::new(config.clone());
14
+ Self { config, sender }
15
+ }
16
+}
17
+
18
+/// Thread-safe handle to the mailer.
19
+pub type SharedMailer = Arc<MailerState>;
0
20