Sign up form and other stuff
[recipes.git] / backend / src / email.rs
diff --git a/backend/src/email.rs b/backend/src/email.rs
new file mode 100644 (file)
index 0000000..a0043f7
--- /dev/null
@@ -0,0 +1,22 @@
+use lettre::{transport::smtp::{authentication::Credentials}, Message, SmtpTransport, Transport};
+
+///
+pub fn send_validation(site_url: &str, email: &str, token: &str, smtp_login: &str, smtp_password: &str) -> Result<(), Box<dyn std::error::Error>> {
+
+    let email = Message::builder()
+        .message_id(None)
+        .from("recipes@gburri.org".parse()?)
+        .to(email.parse()?)
+        .subject("Recipes.gburri.org account validation")
+        .body(format!("Follow this link to confirm your inscription: {}/validation?token={}", site_url, token))?;
+
+    let credentials = Credentials::new(smtp_login.to_string(), smtp_password.to_string());
+
+    let mailer = SmtpTransport::relay("mail.gandi.net")?.credentials(credentials).build();
+
+    if let Err(error) = mailer.send(&email) {
+        println!("Error when sending E-mail:\n{:?}", &error);
+    }
+
+    Ok(())
+}