diff --git a/src/main.rs b/src/main.rs index 760e59b..49780d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,7 +122,9 @@ fn strip_tld(url: &str) -> Option { fn send_email(to_name: &String, to_address: &String, subject: &String, message: &String, - attachment: Option<&String>, settings: &State) -> Result<(), String> { + attachment: Option<&String>, + html: bool, + settings: &State) -> Result<(), String> { // Load HJSON config let email_credentials = &settings.email_credentials; @@ -132,29 +134,32 @@ fn send_email(to_name: &String, to_address: &String, let smtp_username = email_credentials.smtp_username.to_string(); let smtp_password = email_credentials.smtp_password.to_string(); + // Create base object for email + let mut email = Email::builder() + .to((to_address, to_name)) + .from((&smtp_username, &smtp_name)) + .subject(subject); + + // Check if HTML bool is set + if html { + email = email.html(message) + } else { + email = email.text(message) + } + // If an attachment is defined, send it. let email = match attachment { Some(x) => { - Email::builder() - .to((to_address, to_name)) - .from((&smtp_username, &smtp_name)) - .subject(subject) - .html(message) - .attachment_from_file(Path::new(x), None, &TEXT_PLAIN) - .unwrap() - .build() + email.attachment_from_file(Path::new(x), None, &TEXT_PLAIN) .unwrap() }, None => { - Email::builder() - .to((to_address, to_name)) - .from((&smtp_username, &smtp_name)) - .subject(subject) - .html(message) - .build() - .unwrap() + email } }; + + // Build email + let email = email.build().unwrap(); // Connect to a remote server on a custom port let mut mailer = SmtpClient::new_simple(&smtp_server).unwrap() @@ -236,7 +241,8 @@ fn submit_task (host: HostHeader, &x.email, &language_strings.attending_email.subject, &strfmt(&language_strings.attending_email.message, &vars).unwrap(), - Some(&language_strings.attending_email.attachment), + Some(&language_strings.attending_email.attachment), //attachment + true, // HTML email &settings); } else { // Send email about not attending wedding @@ -244,7 +250,8 @@ fn submit_task (host: HostHeader, &x.email, &language_strings.not_attending_email.subject, &strfmt(&language_strings.not_attending_email.message, &vars).unwrap(), - None, + None, // No attachment + true, // HTML email &settings); } } else { @@ -261,11 +268,11 @@ fn submit_task (host: HostHeader, rsvp_message.push_str("Name: "); rsvp_message.push_str(&x.name); - rsvp_message.push_str("
Emailaddress: "); + rsvp_message.push_str("\nEmailaddress: "); rsvp_message.push_str(&x.email); - rsvp_message.push_str("
Guests: "); + rsvp_message.push_str("\nGuests: "); rsvp_message.push_str(&x.guests.to_string()); - rsvp_message.push_str("
Message:

"); + rsvp_message.push_str("\nMessage:\n\n"); rsvp_message.push_str(&x.message); // Create subject and add error message (if necessary) @@ -278,7 +285,7 @@ fn submit_task (host: HostHeader, }, Err(e) => { rsvp_subject.push_str(" just responded to your event! (ERROR)"); - rsvp_message.push_str("
Error message:
"); + rsvp_message.push_str("\n\nError message:\n"); rsvp_message.push_str(&e); error = true; } @@ -289,7 +296,8 @@ fn submit_task (host: HostHeader, &smtp_username, &rsvp_subject, &rsvp_message, - None, + None, // no attachment + false, // no HTML &settings) { Ok(_x) => {}, Err(_e) => {