diff --git a/config.hjson.example b/config.hjson.example index ee28509..5b31b16 100644 --- a/config.hjson.example +++ b/config.hjson.example @@ -33,8 +33,8 @@ languages: { en: { - # Email which will be sent to a guest - "email": { + # Email which will be sent to a guest which will attend + "attending_email": { "subject": "You RSVPed to our wedding!", "message": @@ -43,13 +43,34 @@

- Thanks for letting us know that you will attent to our wedding. + Thanks for letting us know that you will attend to our wedding. ... ''', # ICS File to sent with email - "attachment": ".ics", + "attachment": ".ics", + }, + + "not_attending_email": { + "subject": "We received your message :-(", + + "message": + ''' + Hi {name}! +

+ Bummer! You can't attend our wedding. +

+ In case you get a free slot in your calendar after all, you are of course still welcome to join us! If that would be the case, please fill out the contact form again. +

+ Warm regards, +

+ ... + + ''', + + # ICS File to sent with email + "attachment": "", }, # Text in form @@ -68,6 +89,9 @@ "five_guest": "5 guests", "submit": "Let us know!", "additional_info": "Is there something particular we need to know (for example, will you join us later or do you have allergies)?" + "name_error": "Please state a name", + "email_error": "Please state a valid email", + "guest_error": "Select number of guests" }, # Text on page that thanks user for e-mail @@ -82,11 +106,18 @@ "title": " & Wedding", "h1": "Oops ⁠— excuse us!", "message": "Apparently, you already sent a message less than minutes ago. Wait at least minutes before attempting to sent another message. Unfortunately, we had to add this feature to our website to protect ourselves from spam. Do you think this is not right? Please send an email to .", + }, + + # Text on page that tells user that something went wrong while sending the email + "email_issue": { + "title": " & Wedding", + "h1": "Something went wrong!", + "message": "The email could not be sent. The reason for this could be that you provided an invalid email address, that the email address couldn't be recognized as such, or because the receiving email service rejected us. Please shoot us an email at . Sorry for the inconvenience!", } }, nl: { - # Email which will be sent to a guest - "email": { + # Email which will be sent to a guest which will attend + "attending_email": { "subject": "Je hebt je aangemeld voor onze bruiloft!", "message": @@ -104,6 +135,27 @@ "attachment": ".ics", }, + "not_attending_email": { + "subject": "We hebben je bericht ontvangen :-(", + + "message": + ''' + Hoi {name}! +

+ Wat jammer dat het je/jullie niet lukt om erbij te zijn! +

+ Mocht de situatie toch nog veranderen, ben je/zijn jullie natuurlijk van harte welkom om alsnog langs te komen. Vul in dat geval het contact formulier opnieuw in om ons hiervan op de hoogte te stellen. +

+ Groetjes, +

+ Dennis en Lisanne + + ''', + + # ICS File to sent with email + "attachment": "", + }, + # Text in form "form": { "title": " & Bruiloft", @@ -120,6 +172,9 @@ "five_guest": "5 gasten", "submit": "Laat ons iets weten!", "additional_info": "Is er iets wat we moeten weten (kom je bijvoorbeeld later of heb je allergiën?" + "name_error": "Vul aub een naam in", + "email_error": "Vul aub een geldig adres in", + "guest_error": "Selecteer aantal gasten" }, # Text on page that thanks user for e-mail @@ -134,12 +189,19 @@ "title": " & Bruiloft", "h1": "Oops ⁠— sorry voor het ongemak!", "message": "Het lijkt erop dat je minder dan minuten geleden al eens een bericht verstuurd hebt via deze pagina. Wacht tenminste minuten voordat je nog een bericht verstuurd. We moesten deze bescherming helaas op de website zetten ter protectie tegen spam. Denk je dat dit niet klopt? Stuur dan even een e-mail naar .", + }, + + # Text on page that tells user that something went wrong while sending the email + "email_issue": { + "title": " & Bruiloft", + "h1": "Iets ging er mis!", + "message": "Het is niet gelukt de e-mail te versturen. Dit kan komen omdat het e-mailadres wat je opgegeven hebt niet geldig was, het niet gelukt is het te herkennen, of omdat de ontvangende e-mailservice de e-mail heeft afgewezen. We zouden het fijn vinden als je ons alsnog een berichtje stuurt via . Sorry voor het ongemakt!", } }, de: { - # Email which will be sent to a guest - "email": { + # Email which will be sent to a guest which will attend + "attending_email": { "subject": "Du hast dich angemeldet zu unserer Hochzeit!", "message": @@ -155,6 +217,27 @@ "attachment": ".ics", }, + "not_attending_email": { + "subject": "Wir haben deine Nachricht empfangen :-(", + + "message": + ''' + Hi {name}! +

+ Schade, dass du nicht kommen kannst! +

+ Wenn sich deine Situation ändern würde, und du trotzdem Zeit hättest, bist du natürlich noch immer herzlich Wilkommen. In diesem Fall würden wir dich jedoch bitten, das Kontaktformular neu auszufüllen. +

+ Liebe Grüße, +

+ ... + + ''', + + # ICS File to sent with email + "attachment": "", + }, + # Text in form "form": { "title": " & Hochzeit", @@ -171,6 +254,9 @@ "five_guest": "5 Gäste", "submit": "Formular absenden!", "additional_info": "Gibt es zusätzliche Sachen die wir wissen müssen (kommst du zum Beispiel später oder hast du Allergien)?" + "name_error": "Bitte Namen ergänzen", + "email_error": "Bitte gültige Adresse ergänzen", + "guest_error": "Bitte Anzahl Gäste angeben" }, # Text on page that thanks user for e-mail @@ -185,6 +271,13 @@ "title": " & Hochzeit", "h1": "Oops ⁠— entschuldigung für die Unannehmlichkeiten!," "message": "Es sieht danach aus, dass du in den vergangenen Minuten versucht hast, mehrere Nachrichten über diese Website zu schicken. Warte bitte wenigstens Minuten, vor dass du es nochmal versuchst. Wir mussten dies leider in die Seite mit aufnehmen, als Schutz gegen Spam. Glaubst du, dass diese Blockade unzurecht ist? Schicke uns dann eine E-Mail an .", + }, + + # Text on page that tells user that something went wrong while sending the email + "email_issue": { + "title": " & Hochzeit", + "h1": "Da ist etwas schief gelaufen!", + "message": "Leider ist es nicht gelungen die E-Mail zu verschicken. Der Grund kann sein, dass es keine gültige E-Mail-Adresse war, ich die Addresse nicht erkannt habe, oder weil der empfangende E-Mail-Service die Nachricht nicht angenommen hat. Es wäre gut, wenn du uns die Nachricht direkt an schickst. Entschuldigung für die Unannehmlichkeiten!", } } } diff --git a/src/main.rs b/src/main.rs index c6c32ff..760e59b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,7 +102,8 @@ fn validate_name(name: &String) -> bool { fn validate_email(email: &String) -> bool { // Check if the emailaddress is valid - Regex::new(r"^[a-zA-Z0-9\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z]+$").unwrap().is_match(email) + // This function is not completely correct: emailregex.com + Regex::new(r"^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]+$").unwrap().is_match(email) } fn strip_tld(url: &str) -> Option { @@ -121,7 +122,7 @@ fn strip_tld(url: &str) -> Option { fn send_email(to_name: &String, to_address: &String, subject: &String, message: &String, - attachment: Option<&String>, settings: &State) { + attachment: Option<&String>, settings: &State) -> Result<(), String> { // Load HJSON config let email_credentials = &settings.email_credentials; @@ -166,10 +167,17 @@ fn send_email(to_name: &String, to_address: &String, // Configure expected authentication mechanism .authentication_mechanism(Mechanism::Plain).transport(); - let result = mailer.send(email.into()); - assert!(result.is_ok()); + match mailer.send(email.into()) { + Ok(_x) => { + mailer.close(); + return Ok(()); + }, + Err(e) => { + mailer.close(); + return Err(e.to_string()); + } + }; - mailer.close(); } ////////////////////////// @@ -181,9 +189,12 @@ fn submit_task (host: HostHeader, user_input: Option>, client_addr: &ClientRealAddr, ip_epoch: State>>) -> Redirect { + // Create struct with all language strings let language_strings = lang_strings(host, &settings); + let result : Result<(), String>; + match user_input { Some(x) => { // Check input for anything strange or something that does not look like @@ -219,15 +230,25 @@ fn submit_task (host: HostHeader, } }; - send_email(&x.name, - &x.email, - &language_strings.email.subject, - &strfmt(&language_strings.email.message, &vars).unwrap(), - Some(&language_strings.email.attachment), - &settings); - } - else { - return Redirect::to("404"); + if x.guests > 0 { + // Send email about attendaning wedding + result = send_email(&x.name, + &x.email, + &language_strings.attending_email.subject, + &strfmt(&language_strings.attending_email.message, &vars).unwrap(), + Some(&language_strings.attending_email.attachment), + &settings); + } else { + // Send email about not attending wedding + result = send_email(&x.name, + &x.email, + &language_strings.not_attending_email.subject, + &strfmt(&language_strings.not_attending_email.message, &vars).unwrap(), + None, + &settings); + } + } else { + result = Err("Validate e-mail or name went wrong.".to_string()); } // Send a message to the sender's e-mailaddres to inform about @@ -238,9 +259,6 @@ fn submit_task (host: HostHeader, let mut rsvp_subject = String::new(); let mut rsvp_message = String::new(); - rsvp_subject.push_str(&x.name); - rsvp_subject.push_str(" just responded to your event!"); - rsvp_message.push_str("Name: "); rsvp_message.push_str(&x.name); rsvp_message.push_str("
Emailaddress: "); @@ -250,10 +268,43 @@ fn submit_task (host: HostHeader, rsvp_message.push_str("
Message:

"); rsvp_message.push_str(&x.message); - send_email(&smtp_name, &smtp_username, &rsvp_subject, &rsvp_message, None, &settings); - Redirect::to(format!("/thanks/{}", urlencoding::encode(&x.name))) + // Create subject and add error message (if necessary) + let mut error = false; + + rsvp_subject.push_str(&x.name); + match result { + Ok(_x) => { + rsvp_subject.push_str(" just responded to your event!"); + }, + Err(e) => { + rsvp_subject.push_str(" just responded to your event! (ERROR)"); + rsvp_message.push_str("
Error message:
"); + rsvp_message.push_str(&e); + error = true; + } + }; + + // Also check if e-mail to the organisation didn't go through! + match send_email(&smtp_name, + &smtp_username, + &rsvp_subject, + &rsvp_message, + None, + &settings) { + Ok(_x) => {}, + Err(_e) => { + return Redirect::to("email-issue"); + } + }; + + // Redirect to thank you page or to error page + if error { + return Redirect::to("email-issue"); + } else { + return Redirect::to(format!("/thanks/{}", urlencoding::encode(&x.name))); + } } - None => Redirect::to("404") + None => Redirect::to("email-issue") } } @@ -274,6 +325,15 @@ fn thanks(name: String, host:HostHeader, settings: State) -> Template message : language_strings.thanks.message.clone()}) } +#[get("/email-issue")] +fn email_issue(host:HostHeader, settings: State) -> Template { + // Create struct with all language strings + let language_strings = lang_strings(host, &settings).clone(); + + // Create template with appropriate strings + Template::render("message", &language_strings.email_issue) +} + #[get("/spam")] fn spam(host:HostHeader, settings: State) -> Template { // Create struct with all language strings @@ -314,6 +374,7 @@ fn main() { .mount("/", routes![index]) .mount("/", routes![thanks]) .mount("/", routes![spam]) + .mount("/", routes![email_issue]) .mount("/", routes![submit_task]) .mount("/css", StaticFiles::from(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/css"))) .mount("/js", StaticFiles::from(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/js"))) diff --git a/src/settings.rs b/src/settings.rs index 3631c21..65e360c 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -32,6 +32,9 @@ pub struct FormStrings { pub five_guest: String, pub submit: String, pub additional_info: String, + pub name_error: String, + pub email_error: String, + pub guest_error: String, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -43,10 +46,12 @@ pub struct MessageStrings { #[derive(Debug, Serialize, Deserialize)] pub struct LanguageStrings { - pub email: EmailStrings, + pub attending_email: EmailStrings, + pub not_attending_email: EmailStrings, pub form: FormStrings, pub thanks: MessageStrings, pub spam:MessageStrings, + pub email_issue:MessageStrings, } #[derive(Debug, Deserialize)] diff --git a/templates/index.html.tera b/templates/index.html.tera index 64b9d39..47cec47 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -26,12 +26,12 @@ {{ h2 }} -
+
-
+
@@ -39,7 +39,7 @@
-
+

{{ num_guests }}