another layer of email guard
This commit is contained in:
		
							parent
							
								
									473d131246
								
							
						
					
					
						commit
						66329819a9
					
				
					 2 changed files with 18 additions and 9 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							|  | @ -1,5 +1,6 @@ | ||||||
| /target | /target | ||||||
| /.idea | /.idea | ||||||
|  | /src/debug_emails.txt | ||||||
| Cargo.lock | Cargo.lock | ||||||
| *.sqlite | *.sqlite | ||||||
| *.sqlite3 | *.sqlite3 | ||||||
|  |  | ||||||
							
								
								
									
										26
									
								
								src/email.rs
									
										
									
									
									
								
							
							
						
						
									
										26
									
								
								src/email.rs
									
										
									
									
									
								
							|  | @ -8,6 +8,11 @@ use uuid::Uuid; | ||||||
| 
 | 
 | ||||||
| use crate::config_spec::{CfgField, CfgGroup}; | 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 { | pub const CONFIG: CfgGroup<'static> = CfgGroup { | ||||||
|     name: "email", |     name: "email", | ||||||
|     description: "Sending emails.", |     description: "Sending emails.", | ||||||
|  | @ -33,7 +38,7 @@ pub const CONFIG: CfgGroup<'static> = CfgGroup { | ||||||
|         }, |         }, | ||||||
|         CfgField::Default { |         CfgField::Default { | ||||||
|             key: "to", |             key: "to", | ||||||
|             default: "CCCB Intern <intern@berlin.ccc.de>", |             default: TO_DEFAULT, | ||||||
|             description: "Recipient of the emails sent.", |             description: "Recipient of the emails sent.", | ||||||
|         }, |         }, | ||||||
|         CfgField::Optional { |         CfgField::Optional { | ||||||
|  | @ -55,18 +60,19 @@ pub struct Email { | ||||||
| pub struct SimpleEmail { | pub struct SimpleEmail { | ||||||
|     base: Email, |     base: Email, | ||||||
|     from: String, |     from: String, | ||||||
|     to: String, |     to: Vec<String>, | ||||||
|     in_reply_to: Option<String>, |     in_reply_to: Option<String>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl SimpleEmail { | impl SimpleEmail { | ||||||
|     pub fn new(base: Email, from: &str, to: &str, in_reply_to: Option<String>) -> Self { |     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>> { |     pub fn send_email(&self, subject: String, body: String) -> Result<String, Box<dyn Error>> { | ||||||
|         self.base.send_email( |         self.base.send_email( | ||||||
|             self.from.clone(), |             self.from.clone(), | ||||||
|             self.to.clone(), |             &self.to.clone(), | ||||||
|             subject, |             subject, | ||||||
|             body, |             body, | ||||||
|             self.in_reply_to.clone(), |             self.in_reply_to.clone(), | ||||||
|  | @ -82,13 +88,15 @@ impl Email { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     pub fn send_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>> { |     ) -> Result<String, Box<dyn Error>> { | ||||||
|         let message_id = Uuid::new_v4().to_string() + &self.message_id_suffix; |         let message_id = Uuid::new_v4().to_string() + &self.message_id_suffix; | ||||||
|         let email = Message::builder() |         let mut email = Message::builder() | ||||||
|             .from(from.parse().unwrap()) |             .from(from.parse().unwrap()); | ||||||
|             .to(to.parse().unwrap()) |         for recipient in to { | ||||||
|             .message_id(Some(message_id.clone())); |             email = email.to(recipient.parse().unwrap()); | ||||||
|  |         }; | ||||||
|  |         email = email.message_id(Some(message_id.clone())); | ||||||
|         let email = |         let email = | ||||||
|             if in_reply_to.is_some() { email.in_reply_to(in_reply_to.unwrap()) } else { email } |             if in_reply_to.is_some() { email.in_reply_to(in_reply_to.unwrap()) } else { email } | ||||||
|                 .subject(subject) |                 .subject(subject) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 nobody
						nobody