E-mails to organisation are now non-HTML. Fixes #2

This commit is contained in:
Dennis Potter 2019-09-05 00:33:47 +02:00
parent 29666541bc
commit 75c9473ca9
Signed by: Dennis
GPG Key ID: 186A8AD440942BAF

View File

@ -122,7 +122,9 @@ fn strip_tld(url: &str) -> Option<String> {
fn send_email(to_name: &String, to_address: &String, fn send_email(to_name: &String, to_address: &String,
subject: &String, message: &String, subject: &String, message: &String,
attachment: Option<&String>, settings: &State<Settings>) -> Result<(), String> { attachment: Option<&String>,
html: bool,
settings: &State<Settings>) -> Result<(), String> {
// Load HJSON config // Load HJSON config
let email_credentials = &settings.email_credentials; let email_credentials = &settings.email_credentials;
@ -132,30 +134,33 @@ fn send_email(to_name: &String, to_address: &String,
let smtp_username = email_credentials.smtp_username.to_string(); let smtp_username = email_credentials.smtp_username.to_string();
let smtp_password = email_credentials.smtp_password.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. // If an attachment is defined, send it.
let email = match attachment { let email = match attachment {
Some(x) => { Some(x) => {
Email::builder() email.attachment_from_file(Path::new(x), None, &TEXT_PLAIN)
.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()
.unwrap() .unwrap()
}, },
None => { None => {
Email::builder() email
.to((to_address, to_name))
.from((&smtp_username, &smtp_name))
.subject(subject)
.html(message)
.build()
.unwrap()
} }
}; };
// Build email
let email = email.build().unwrap();
// Connect to a remote server on a custom port // Connect to a remote server on a custom port
let mut mailer = SmtpClient::new_simple(&smtp_server).unwrap() let mut mailer = SmtpClient::new_simple(&smtp_server).unwrap()
// Set the name sent during EHLO/HELO, default is `localhost` // Set the name sent during EHLO/HELO, default is `localhost`
@ -236,7 +241,8 @@ fn submit_task (host: HostHeader,
&x.email, &x.email,
&language_strings.attending_email.subject, &language_strings.attending_email.subject,
&strfmt(&language_strings.attending_email.message, &vars).unwrap(), &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); &settings);
} else { } else {
// Send email about not attending wedding // Send email about not attending wedding
@ -244,7 +250,8 @@ fn submit_task (host: HostHeader,
&x.email, &x.email,
&language_strings.not_attending_email.subject, &language_strings.not_attending_email.subject,
&strfmt(&language_strings.not_attending_email.message, &vars).unwrap(), &strfmt(&language_strings.not_attending_email.message, &vars).unwrap(),
None, None, // No attachment
true, // HTML email
&settings); &settings);
} }
} else { } else {
@ -261,11 +268,11 @@ fn submit_task (host: HostHeader,
rsvp_message.push_str("Name: "); rsvp_message.push_str("Name: ");
rsvp_message.push_str(&x.name); rsvp_message.push_str(&x.name);
rsvp_message.push_str("<br />Emailaddress: "); rsvp_message.push_str("\nEmailaddress: ");
rsvp_message.push_str(&x.email); rsvp_message.push_str(&x.email);
rsvp_message.push_str("<br />Guests: "); rsvp_message.push_str("\nGuests: ");
rsvp_message.push_str(&x.guests.to_string()); rsvp_message.push_str(&x.guests.to_string());
rsvp_message.push_str("<br />Message:<br /><br />"); rsvp_message.push_str("\nMessage:\n\n");
rsvp_message.push_str(&x.message); rsvp_message.push_str(&x.message);
// Create subject and add error message (if necessary) // Create subject and add error message (if necessary)
@ -278,7 +285,7 @@ fn submit_task (host: HostHeader,
}, },
Err(e) => { Err(e) => {
rsvp_subject.push_str(" just responded to your event! (ERROR)"); rsvp_subject.push_str(" just responded to your event! (ERROR)");
rsvp_message.push_str("<br />Error message:<br />"); rsvp_message.push_str("\n\nError message:\n");
rsvp_message.push_str(&e); rsvp_message.push_str(&e);
error = true; error = true;
} }
@ -289,7 +296,8 @@ fn submit_task (host: HostHeader,
&smtp_username, &smtp_username,
&rsvp_subject, &rsvp_subject,
&rsvp_message, &rsvp_message,
None, None, // no attachment
false, // no HTML
&settings) { &settings) {
Ok(_x) => {}, Ok(_x) => {},
Err(_e) => { Err(_e) => {