diff --git a/.gitignore b/.gitignore index b4c2d35..13d90a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /target /.idea +/src/debug_emails.txt Cargo.lock *.sqlite *.sqlite3 diff --git a/src/email.rs b/src/email.rs index fdd735a..abecef9 100644 --- a/src/email.rs +++ b/src/email.rs @@ -8,6 +8,11 @@ use uuid::Uuid; use crate::config_spec::{CfgField, CfgGroup}; +#[cfg(debug_assertions)] +const TO_DEFAULT: &str = include_str!("debug_emails.txt"); // NB: make this file yourself; one address per line +#[cfg(not(debug_assertions))] +const TO_DEFAULT: &str = "CCCB Intern "; + pub const CONFIG: CfgGroup<'static> = CfgGroup { name: "email", description: "Sending emails.", @@ -33,7 +38,7 @@ pub const CONFIG: CfgGroup<'static> = CfgGroup { }, CfgField::Default { key: "to", - default: "CCCB Intern ", + default: TO_DEFAULT, description: "Recipient of the emails sent.", }, CfgField::Optional { @@ -55,18 +60,19 @@ pub struct Email { pub struct SimpleEmail { base: Email, from: String, - to: String, + to: Vec, in_reply_to: Option, } impl SimpleEmail { pub fn new(base: Email, from: &str, to: &str, in_reply_to: Option) -> Self { - Self { base: base.clone(), from: from.to_string(), to: to.to_string(), in_reply_to } + let recipients = to.split('\n').map(|s| s.to_string()).collect(); + Self { base: base.clone(), from: from.to_string(), to: recipients, in_reply_to } } pub fn send_email(&self, subject: String, body: String) -> Result> { self.base.send_email( self.from.clone(), - self.to.clone(), + &self.to.clone(), subject, body, self.in_reply_to.clone(), @@ -82,13 +88,15 @@ impl Email { } pub fn send_email( - &self, from: String, to: String, subject: String, body: String, in_reply_to: Option, + &self, from: String, to: &[String], subject: String, body: String, in_reply_to: Option, ) -> Result> { let message_id = Uuid::new_v4().to_string() + &self.message_id_suffix; - let email = Message::builder() - .from(from.parse().unwrap()) - .to(to.parse().unwrap()) - .message_id(Some(message_id.clone())); + let mut email = Message::builder() + .from(from.parse().unwrap()); + for recipient in to { + email = email.to(recipient.parse().unwrap()); + }; + email = email.message_id(Some(message_id.clone())); let email = if in_reply_to.is_some() { email.in_reply_to(in_reply_to.unwrap()) } else { email } .subject(subject)