another layer of email guard
This commit is contained in:
parent
473d131246
commit
66329819a9
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,5 +1,6 @@
|
|||
/target
|
||||
/.idea
|
||||
/src/debug_emails.txt
|
||||
Cargo.lock
|
||||
*.sqlite
|
||||
*.sqlite3
|
||||
|
|
26
src/email.rs
26
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 <intern@berlin.ccc.de>";
|
||||
|
||||
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 <intern@berlin.ccc.de>",
|
||||
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<String>,
|
||||
in_reply_to: Option<String>,
|
||||
}
|
||||
|
||||
impl SimpleEmail {
|
||||
pub fn new(base: Email, from: &str, to: &str, in_reply_to: Option<String>) -> 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<String, Box<dyn Error>> {
|
||||
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<String>,
|
||||
&self, from: String, to: &[String], subject: String, body: String, in_reply_to: Option<String>,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
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)
|
||||
|
|
Loading…
Reference in a new issue