use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MailMessage {
pub subject: String,
pub body: String,
pub html: Option<String>,
pub from: Option<String>,
pub reply_to: Option<String>,
pub cc: Vec<String>,
pub bcc: Vec<String>,
pub headers: Vec<(String, String)>,
}
impl MailMessage {
pub fn new() -> Self {
Self::default()
}
pub fn subject(mut self, subject: impl Into<String>) -> Self {
self.subject = subject.into();
self
}
pub fn body(mut self, body: impl Into<String>) -> Self {
self.body = body.into();
self
}
pub fn html(mut self, html: impl Into<String>) -> Self {
self.html = Some(html.into());
self
}
pub fn from(mut self, from: impl Into<String>) -> Self {
self.from = Some(from.into());
self
}
pub fn reply_to(mut self, reply_to: impl Into<String>) -> Self {
self.reply_to = Some(reply_to.into());
self
}
pub fn cc(mut self, email: impl Into<String>) -> Self {
self.cc.push(email.into());
self
}
pub fn bcc(mut self, email: impl Into<String>) -> Self {
self.bcc.push(email.into());
self
}
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.push((name.into(), value.into()));
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mail_message_builder() {
let mail = MailMessage::new()
.subject("Welcome!")
.body("Hello, welcome to our service.")
.html("<h1>Hello!</h1>")
.from("noreply@example.com")
.cc("manager@example.com")
.bcc("archive@example.com");
assert_eq!(mail.subject, "Welcome!");
assert_eq!(mail.body, "Hello, welcome to our service.");
assert_eq!(mail.html, Some("<h1>Hello!</h1>".into()));
assert_eq!(mail.from, Some("noreply@example.com".into()));
assert_eq!(mail.cc, vec!["manager@example.com"]);
assert_eq!(mail.bcc, vec!["archive@example.com"]);
}
}