aboutsummaryrefslogtreecommitdiff
path: root/src/mail.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mail.rs')
-rw-r--r--src/mail.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/mail.rs b/src/mail.rs
new file mode 100644
index 0000000..d86cff3
--- /dev/null
+++ b/src/mail.rs
@@ -0,0 +1,59 @@
+use lettre::sendmail::SendmailTransport;
+use lettre::{SendableEmail, Transport};
+use lettre_email::Email;
+
+use crate::model::config::Config;
+
+static FROM: &str = "contact@guyonvarch.me";
+
+pub fn send(
+ config: &Config,
+ to: Vec<(String, String)>,
+ subject: String,
+ message: String,
+) -> bool {
+ match prepare_email(to.clone(), subject.clone(), message.clone()) {
+ Ok(email) => {
+ if config.mock_mails {
+ let formatted_to = to
+ .into_iter()
+ .map(|t| t.0)
+ .collect::<Vec<String>>()
+ .join(", ");
+ info!(
+ "MOCK MAIL\nto: {}\nsubject: {}\n\n{}",
+ formatted_to, subject, message
+ );
+ true
+ } else {
+ let mut sender =
+ SendmailTransport::new_with_command(&config.sendmail_path);
+ match sender.send(email) {
+ Ok(_) => true,
+ Err(err) => {
+ error!("Error sending email: {:?}", err);
+ false
+ }
+ }
+ }
+ }
+ Err(err) => {
+ error!("Error preparing email: {:?}", err);
+ false
+ }
+ }
+}
+
+fn prepare_email(
+ to: Vec<(String, String)>,
+ subject: String,
+ message: String,
+) -> Result<SendableEmail, lettre_email::error::Error> {
+ let mut email = Email::builder().from(FROM).subject(subject).text(message);
+
+ for (address, name) in to.iter() {
+ email = email.to((address, name));
+ }
+
+ email.build().map(|e| e.into())
+}