From cbac8029780525253d988505dd142913d27c8c92 Mon Sep 17 00:00:00 2001
From: Paul Makles <paulmakles@gmail.com>
Date: Sun, 30 Aug 2020 17:16:53 +0100
Subject: [PATCH] Re-write email backend, use SMTP directly.

---
 .dockerignore                      |  2 +
 src/database/channel.rs            | 38 ++++++------
 src/database/guild.rs              | 30 +++++-----
 src/database/migrations/init.rs    | 28 +++++----
 src/database/migrations/mod.rs     |  7 +--
 src/database/migrations/scripts.rs | 95 ++++++++++++++++-------------
 src/database/mutual.rs             |  8 +--
 src/database/permissions.rs        |  9 ++-
 src/database/user.rs               | 94 ++++++++++++-----------------
 src/email.rs                       | 61 -------------------
 src/main.rs                        |  4 +-
 src/notifications/ws.rs            |  8 ++-
 src/routes/account.rs              | 34 ++++-------
 src/routes/channel.rs              | 14 ++---
 src/routes/guild.rs                | 79 ++++++++++++------------
 src/routes/root.rs                 |  5 +-
 src/routes/user.rs                 | 12 ++--
 src/util/captcha.rs                |  4 +-
 src/util/email.rs                  | 96 ++++++++++++++++++++++++++++++
 src/util/mod.rs                    |  2 +
 src/util/variables.rs              | 27 +++++++++
 21 files changed, 354 insertions(+), 303 deletions(-)
 create mode 100644 .dockerignore
 delete mode 100644 src/email.rs
 create mode 100644 src/util/email.rs
 create mode 100644 src/util/variables.rs

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..c9adf6b
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,2 @@
+assets
+target
\ No newline at end of file
diff --git a/src/database/channel.rs b/src/database/channel.rs
index c4c23e1..4f5695b 100644
--- a/src/database/channel.rs
+++ b/src/database/channel.rs
@@ -1,12 +1,12 @@
 use super::get_collection;
 
 use lru::LruCache;
-use std::sync::{Arc, Mutex};
 use mongodb::bson::{doc, from_bson, Bson};
 use rocket::http::RawStr;
 use rocket::request::FromParam;
 use rocket_contrib::json::JsonValue;
 use serde::{Deserialize, Serialize};
+use std::sync::{Arc, Mutex};
 
 #[derive(Serialize, Deserialize, Debug, Clone)]
 pub struct LastMessage {
@@ -50,26 +50,22 @@ impl Channel {
                 "last_message": self.last_message,
                 "recipients": self.recipients,
             }),
-            1 => {
-                json!({
-                    "id": self.id,
-                    "type": self.channel_type,
-                    "last_message": self.last_message,
-                    "recipients": self.recipients,
-                    "name": self.name,
-                    "owner": self.owner,
-                    "description": self.description,
-                })
-            }
-            2 => {
-                json!({
-                    "id": self.id,
-                    "type": self.channel_type,
-                    "guild": self.guild,
-                    "name": self.name,
-                    "description": self.description,
-                })
-            }
+            1 => json!({
+                "id": self.id,
+                "type": self.channel_type,
+                "last_message": self.last_message,
+                "recipients": self.recipients,
+                "name": self.name,
+                "owner": self.owner,
+                "description": self.description,
+            }),
+            2 => json!({
+                "id": self.id,
+                "type": self.channel_type,
+                "guild": self.guild,
+                "name": self.name,
+                "description": self.description,
+            }),
             _ => unreachable!(),
         }
     }
diff --git a/src/database/guild.rs b/src/database/guild.rs
index 9b9801d..a7a90db 100644
--- a/src/database/guild.rs
+++ b/src/database/guild.rs
@@ -1,5 +1,5 @@
-use super::get_collection;
 use super::channel::fetch_channels;
+use super::get_collection;
 
 use lru::LruCache;
 use mongodb::bson::{doc, from_bson, Bson};
@@ -66,13 +66,17 @@ impl Guild {
     }
 
     pub fn seralise_with_channels(self) -> Result<JsonValue, String> {
-        let channels = self.fetch_channels()?
+        let channels = self
+            .fetch_channels()?
             .into_iter()
             .map(|x| x.serialise())
             .collect();
 
         let mut value = self.serialise();
-        value.as_object_mut().unwrap().insert("channels".to_string(), channels);
+        value
+            .as_object_mut()
+            .unwrap()
+            .insert("channels".to_string(), channels);
         Ok(value)
     }
 }
@@ -167,10 +171,7 @@ pub fn fetch_guilds(ids: &Vec<String>) -> Result<Vec<Guild>, String> {
 
 pub fn serialise_guilds_with_channels(ids: &Vec<String>) -> Result<Vec<JsonValue>, String> {
     let guilds = fetch_guilds(&ids)?;
-    let cids: Vec<String> = guilds
-        .iter()
-        .flat_map(|x| x.channels.clone())
-        .collect();
+    let cids: Vec<String> = guilds.iter().flat_map(|x| x.channels.clone()).collect();
 
     let channels = fetch_channels(&cids)?;
     Ok(guilds
@@ -180,10 +181,11 @@ pub fn serialise_guilds_with_channels(ids: &Vec<String>) -> Result<Vec<JsonValue
             let mut obj = x.serialise();
             obj.as_object_mut().unwrap().insert(
                 "channels".to_string(),
-                channels.iter()
+                channels
+                    .iter()
                     .filter(|x| x.guild.is_some() && x.guild.as_ref().unwrap() == &id)
                     .map(|x| x.clone().serialise())
-                    .collect()
+                    .collect(),
             );
             obj
         })
@@ -315,19 +317,19 @@ pub fn process_event(event: &Notification) {
         Notification::guild_user_join(ev) => {
             let mut cache = MEMBER_CACHE.lock().unwrap();
             cache.put(
-                MemberKey ( ev.id.clone(), ev.user.clone() ),
+                MemberKey(ev.id.clone(), ev.user.clone()),
                 Member {
                     id: MemberRef {
                         guild: ev.id.clone(),
-                        user: ev.user.clone()
+                        user: ev.user.clone(),
                     },
-                    nickname: None
-                }
+                    nickname: None,
+                },
             );
         }
         Notification::guild_user_leave(ev) => {
             let mut cache = MEMBER_CACHE.lock().unwrap();
-            cache.pop(&MemberKey ( ev.id.clone(), ev.user.clone() ));
+            cache.pop(&MemberKey(ev.id.clone(), ev.user.clone()));
         }
         _ => {}
     }
diff --git a/src/database/migrations/init.rs b/src/database/migrations/init.rs
index 4616760..35170fb 100644
--- a/src/database/migrations/init.rs
+++ b/src/database/migrations/init.rs
@@ -1,27 +1,33 @@
 use super::super::get_db;
 use super::scripts::LATEST_REVISION;
 
-use mongodb::options::CreateCollectionOptions;
-use mongodb::bson::doc;
 use log::info;
+use mongodb::bson::doc;
+use mongodb::options::CreateCollectionOptions;
 
 pub fn create_database() {
     info!("Creating database.");
     let db = get_db();
 
-    db.create_collection("users", None).expect("Failed to create users collection.");
-    db.create_collection("channels", None).expect("Failed to create channels collection.");
-    db.create_collection("guilds", None).expect("Failed to create guilds collection.");
-    db.create_collection("members", None).expect("Failed to create members collection.");
-    db.create_collection("messages", None).expect("Failed to create messages collection.");
-    db.create_collection("migrations", None).expect("Failed to create migrations collection.");
+    db.create_collection("users", None)
+        .expect("Failed to create users collection.");
+    db.create_collection("channels", None)
+        .expect("Failed to create channels collection.");
+    db.create_collection("guilds", None)
+        .expect("Failed to create guilds collection.");
+    db.create_collection("members", None)
+        .expect("Failed to create members collection.");
+    db.create_collection("messages", None)
+        .expect("Failed to create messages collection.");
+    db.create_collection("migrations", None)
+        .expect("Failed to create migrations collection.");
 
     db.create_collection(
         "pubsub",
         CreateCollectionOptions::builder()
             .capped(true)
             .size(1_000_000)
-            .build()
+            .build(),
     )
     .expect("Failed to create pubsub collection.");
 
@@ -31,9 +37,9 @@ pub fn create_database() {
                 "_id": 0,
                 "revision": LATEST_REVISION
             },
-            None
+            None,
         )
         .expect("Failed to save migration info.");
-    
+
     info!("Created database.");
 }
diff --git a/src/database/migrations/mod.rs b/src/database/migrations/mod.rs
index 6e188b0..33cbdf5 100644
--- a/src/database/migrations/mod.rs
+++ b/src/database/migrations/mod.rs
@@ -6,10 +6,9 @@ pub mod scripts;
 pub fn run_migrations() {
     let client = get_connection();
 
-    let list = client.list_database_names(
-        None,
-        None
-    ).expect("Failed to fetch database names.");
+    let list = client
+        .list_database_names(None, None)
+        .expect("Failed to fetch database names.");
 
     if list.iter().position(|x| x == "revolt").is_none() {
         init::create_database();
diff --git a/src/database/migrations/scripts.rs b/src/database/migrations/scripts.rs
index c2041ea..81fdf4e 100644
--- a/src/database/migrations/scripts.rs
+++ b/src/database/migrations/scripts.rs
@@ -1,40 +1,43 @@
 use super::super::get_collection;
 
-use serde::{Serialize, Deserialize};
-use mongodb::bson::{Bson, from_bson, doc};
-use mongodb::options::FindOptions;
 use log::info;
+use mongodb::bson::{doc, from_bson, Bson};
+use mongodb::options::FindOptions;
+use serde::{Deserialize, Serialize};
 
 #[derive(Serialize, Deserialize)]
 struct MigrationInfo {
     _id: i32,
-    revision: i32
+    revision: i32,
 }
 
 pub const LATEST_REVISION: i32 = 2;
 
 pub fn migrate_database() {
     let migrations = get_collection("migrations");
-    let data = migrations.find_one(None, None)
+    let data = migrations
+        .find_one(None, None)
         .expect("Failed to fetch migration data.");
-    
+
     if let Some(doc) = data {
-        let info: MigrationInfo = from_bson(Bson::Document(doc))
-            .expect("Failed to read migration information.");
-        
+        let info: MigrationInfo =
+            from_bson(Bson::Document(doc)).expect("Failed to read migration information.");
+
         let revision = run_migrations(info.revision);
 
-        migrations.update_one(
-            doc! {
-                "_id": info._id
-            },
-            doc! {
-                "$set": {
-                    "revision": revision
-                }
-            },
-            None
-        ).expect("Failed to commit migration information.");
+        migrations
+            .update_one(
+                doc! {
+                    "_id": info._id
+                },
+                doc! {
+                    "$set": {
+                        "revision": revision
+                    }
+                },
+                None,
+            )
+            .expect("Failed to commit migration information.");
 
         info!("Migration complete. Currently at revision {}.", revision);
     } else {
@@ -53,32 +56,39 @@ pub fn run_migrations(revision: i32) -> i32 {
         info!("Running migration [revision 1]: Add channels to guild object.");
 
         let col = get_collection("guilds");
-        let guilds = col.find(
-            None,
-            FindOptions::builder()
-                .projection(doc! { "_id": 1 })
-                .build()
-        )
+        let guilds = col
+            .find(
+                None,
+                FindOptions::builder().projection(doc! { "_id": 1 }).build(),
+            )
             .expect("Failed to fetch guilds.");
-        
-        let result = get_collection("channels").find(
-            doc! {
-                "type": 2
-            },
-            FindOptions::builder()
-                .projection(doc! { "_id": 1, "guild": 1 })
-                .build()
-        ).expect("Failed to fetch channels.");
+
+        let result = get_collection("channels")
+            .find(
+                doc! {
+                    "type": 2
+                },
+                FindOptions::builder()
+                    .projection(doc! { "_id": 1, "guild": 1 })
+                    .build(),
+            )
+            .expect("Failed to fetch channels.");
 
         let mut channels = vec![];
         for doc in result {
             let channel = doc.expect("Failed to fetch channel.");
-            let id  = channel.get_str("_id").expect("Failed to get channel id.").to_string();
-            let gid = channel.get_str("guild").expect("Failed to get guild id.").to_string();
+            let id = channel
+                .get_str("_id")
+                .expect("Failed to get channel id.")
+                .to_string();
+            let gid = channel
+                .get_str("guild")
+                .expect("Failed to get guild id.")
+                .to_string();
 
-            channels.push(( id, gid ));
+            channels.push((id, gid));
         }
-        
+
         for doc in guilds {
             let guild = doc.expect("Failed to fetch guild.");
             let id = guild.get_str("_id").expect("Failed to get guild id.");
@@ -88,7 +98,7 @@ pub fn run_migrations(revision: i32) -> i32 {
                 .filter(|x| x.1 == id)
                 .map(|x| x.0.clone())
                 .collect();
-            
+
             col.update_one(
                 doc! {
                     "_id": id
@@ -98,8 +108,9 @@ pub fn run_migrations(revision: i32) -> i32 {
                         "channels": list
                     }
                 },
-                None
-            ).expect("Failed to update guild.");
+                None,
+            )
+            .expect("Failed to update guild.");
         }
     }
 
diff --git a/src/database/mutual.rs b/src/database/mutual.rs
index 4de4e58..5d0a5fe 100644
--- a/src/database/mutual.rs
+++ b/src/database/mutual.rs
@@ -118,12 +118,10 @@ pub fn has_mutual_connection(user_id: &str, target_id: &str, with_permission: bo
             }
 
             false
+        } else if result.count() > 0 {
+            true
         } else {
-            if result.count() > 0 {
-                true
-            } else {
-                false
-            }
+            false
         }
     } else {
         false
diff --git a/src/database/permissions.rs b/src/database/permissions.rs
index 19bdb89..596419c 100644
--- a/src/database/permissions.rs
+++ b/src/database/permissions.rs
@@ -173,9 +173,12 @@ impl PermissionCalculator {
                         }
 
                         if let Some(other) = other_user {
-                            let relationship =
-                                get_relationship_internal(&self.user.id, &other, &self.user.relations);
-    
+                            let relationship = get_relationship_internal(
+                                &self.user.id,
+                                &other,
+                                &self.user.relations,
+                            );
+
                             if relationship == Relationship::Friend {
                                 permissions = 1024 + 128 + 32 + 16 + 1;
                             } else if relationship == Relationship::Blocked
diff --git a/src/database/user.rs b/src/database/user.rs
index 3b5de3a..26b938c 100644
--- a/src/database/user.rs
+++ b/src/database/user.rs
@@ -1,16 +1,16 @@
+use super::channel::fetch_channels;
 use super::get_collection;
-use super::guild::{serialise_guilds_with_channels};
-use super::channel::{fetch_channels};
+use super::guild::serialise_guilds_with_channels;
 
 use lru::LruCache;
 use mongodb::bson::{doc, from_bson, Bson, DateTime};
 use mongodb::options::FindOptions;
 use rocket::http::{RawStr, Status};
 use rocket::request::{self, FromParam, FromRequest, Request};
-use rocket_contrib::json::JsonValue;
 use rocket::Outcome;
-use std::sync::{Arc, Mutex};
+use rocket_contrib::json::JsonValue;
 use serde::{Deserialize, Serialize};
+use std::sync::{Arc, Mutex};
 
 #[derive(Serialize, Deserialize, Debug, Clone)]
 pub struct UserEmailVerification {
@@ -66,23 +66,21 @@ impl User {
                 doc! {
                     "_id.user": &self.id
                 },
-                None
-            ).map_err(|_| "Failed to fetch members.")?;
-        
-        Ok(members.into_iter()
+                None,
+            )
+            .map_err(|_| "Failed to fetch members.")?;
+
+        Ok(members
+            .into_iter()
             .filter_map(|x| match x {
-                Ok(doc) => {
-                    match doc.get_document("_id") {
-                        Ok(id) => {
-                            match id.get_str("guild") {
-                                Ok(value) => Some(value.to_string()),
-                                Err(_) => None
-                            }
-                        }
-                        Err(_) => None
-                    }
-                }
-                Err(_) => None
+                Ok(doc) => match doc.get_document("_id") {
+                    Ok(id) => match id.get_str("guild") {
+                        Ok(value) => Some(value.to_string()),
+                        Err(_) => None,
+                    },
+                    Err(_) => None,
+                },
+                Err(_) => None,
             })
             .collect())
     }
@@ -93,18 +91,16 @@ impl User {
                 doc! {
                     "recipients": &self.id
                 },
-                FindOptions::builder()
-                    .projection(doc! { "_id": 1 })
-                    .build()
-            ).map_err(|_| "Failed to fetch channel ids.")?;
-        
-        Ok(channels.into_iter()
+                FindOptions::builder().projection(doc! { "_id": 1 }).build(),
+            )
+            .map_err(|_| "Failed to fetch channel ids.")?;
+
+        Ok(channels
+            .into_iter()
             .filter_map(|x| x.ok())
-            .filter_map(|x| {
-                match x.get_str("_id") {
-                    Ok(value) => Some(value.to_string()),
-                    Err(_) => None
-                }
+            .filter_map(|x| match x.get_str("_id") {
+                Ok(value) => Some(value.to_string()),
+                Err(_) => None,
             })
             .collect())
     }
@@ -112,22 +108,12 @@ impl User {
     pub fn create_payload(self) -> Result<JsonValue, String> {
         let v = vec![];
         let relations = self.relations.as_ref().unwrap_or(&v);
-        
-        let users: Vec<JsonValue> = fetch_users(
-            &relations
-                .iter()
-                .map(|x| x.id.clone())
-                .collect()
-        )?
+
+        let users: Vec<JsonValue> = fetch_users(&relations.iter().map(|x| x.id.clone()).collect())?
             .into_iter()
             .map(|x| {
                 let id = x.id.clone();
-                x.serialise(
-                    relations.iter()
-                        .find(|y| y.id == id)
-                        .unwrap()
-                        .status as i32
-                )
+                x.serialise(relations.iter().find(|y| y.id == id).unwrap().status as i32)
             })
             .collect();
 
@@ -135,7 +121,7 @@ impl User {
             .into_iter()
             .map(|x| x.serialise())
             .collect();
-        
+
         Ok(json!({
             "users": users,
             "channels": channels,
@@ -239,7 +225,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
     type Error = AuthError;
 
     fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
-        let u = request.headers().get("x-user").next(); 
+        let u = request.headers().get("x-user").next();
         let t = request.headers().get("x-auth-token").next();
 
         if let Some(uid) = u {
@@ -298,17 +284,13 @@ pub fn process_event(event: &Notification) {
                         if let Some(pos) = relations.iter().position(|x| x.id == ev.user) {
                             relations.remove(pos);
                         }
+                    } else if let Some(entry) = relations.iter_mut().find(|x| x.id == ev.user) {
+                        entry.status = ev.status as u8;
                     } else {
-                        if let Some(entry) = relations.iter_mut().find(|x| x.id == ev.user) {
-                            entry.status = ev.status as u8;
-                        } else {
-                            relations.push(
-                                UserRelationship {
-                                    id: ev.id.clone(),
-                                    status: ev.status as u8
-                                }
-                            );
-                        }
+                        relations.push(UserRelationship {
+                            id: ev.id.clone(),
+                            status: ev.status as u8,
+                        });
                     }
                 }
             }
diff --git a/src/email.rs b/src/email.rs
deleted file mode 100644
index 1fd47ab..0000000
--- a/src/email.rs
+++ /dev/null
@@ -1,61 +0,0 @@
-use reqwest::blocking::Client;
-use std::collections::HashMap;
-use std::env;
-
-fn public_uri() -> String {
-    env::var("PUBLIC_URI").expect("PUBLIC_URI not in environment variables!")
-}
-
-fn portal() -> String {
-    env::var("PORTAL_URL").expect("PORTAL_URL not in environment variables!")
-}
-
-pub fn send_email(target: String, subject: String, body: String, html: String) -> Result<(), ()> {
-    let mut map = HashMap::new();
-    map.insert("target", target.clone());
-    map.insert("subject", subject);
-    map.insert("body", body);
-    map.insert("html", html);
-
-    let client = Client::new();
-    match client.post(&portal()).json(&map).send() {
-        Ok(_) => Ok(()),
-        Err(_) => Err(()),
-    }
-}
-
-pub fn send_verification_email(email: String, code: String) -> bool {
-    let url = format!("{}/api/account/verify/{}", public_uri(), code);
-    send_email(
-        email,
-        "Verify your email!".to_string(),
-        format!("Verify your email here: {}", url),
-        format!("<a href=\"{}\">Click to verify your email!</a>", url),
-    )
-    .is_ok()
-}
-
-pub fn send_password_reset(email: String, code: String) -> bool {
-    let url = format!("{}/api/account/reset/{}", public_uri(), code);
-    send_email(
-        email,
-        "Reset your password.".to_string(),
-        format!("Reset your password here: {}", url),
-        format!("<a href=\"{}\">Click to reset your password!</a>", url),
-    )
-    .is_ok()
-}
-
-pub fn send_welcome_email(email: String, username: String) -> bool {
-    send_email(
-        email,
-        "Welcome to REVOLT!".to_string(),
-        format!("Welcome, {}! You can now use REVOLT.", username.clone()),
-        format!(
-            "<b>Welcome, {}!</b><br/>You can now use REVOLT.<br/><a href=\"{}\">Go to REVOLT</a>",
-            username.clone(),
-            public_uri()
-        ),
-    )
-    .is_ok()
-}
diff --git a/src/main.rs b/src/main.rs
index 72161bc..b10d6de 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,13 +9,11 @@ extern crate bitfield;
 #[macro_use]
 extern crate lazy_static;
 
-pub mod notifications;
 pub mod database;
+pub mod notifications;
 pub mod routes;
-pub mod email;
 pub mod util;
 
-use dotenv;
 use rocket_cors::AllowedOrigins;
 use std::thread;
 
diff --git a/src/notifications/ws.rs b/src/notifications/ws.rs
index b3dd802..af00dbf 100644
--- a/src/notifications/ws.rs
+++ b/src/notifications/ws.rs
@@ -36,9 +36,11 @@ impl Handler for Server {
 
                             match state.try_authenticate(self.id.clone(), token.to_string()) {
                                 StateResult::Success(user_id) => {
-                                    let user = crate::database::user::fetch_user(&user_id).unwrap().unwrap();
+                                    let user = crate::database::user::fetch_user(&user_id)
+                                        .unwrap()
+                                        .unwrap();
                                     self.user_id = Some(user_id);
-                                    
+
                                     self.sender.send(
                                         json!({
                                             "type": "authenticate",
@@ -46,7 +48,7 @@ impl Handler for Server {
                                         })
                                         .to_string(),
                                     )?;
-                                    
+
                                     if let Ok(payload) = user.create_payload() {
                                         self.sender.send(
                                             json!({
diff --git a/src/routes/account.rs b/src/routes/account.rs
index ed53e38..2369dfa 100644
--- a/src/routes/account.rs
+++ b/src/routes/account.rs
@@ -1,12 +1,11 @@
 use super::Response;
 use crate::database;
-use crate::email;
-use crate::util::gen_token;
-use crate::util::captcha;
+use crate::util::{captcha, email, gen_token};
 
 use bcrypt::{hash, verify};
 use chrono::prelude::*;
 use database::user::User;
+use log::error;
 use mongodb::bson::{doc, from_bson, Bson};
 use rocket_contrib::json::Json;
 use serde::{Deserialize, Serialize};
@@ -31,15 +30,11 @@ pub struct Create {
 #[post("/create", data = "<info>")]
 pub fn create(info: Json<Create>) -> Response {
     if let Err(error) = captcha::verify(&info.captcha) {
-        return Response::BadRequest(
-            json!({ "error": error })
-        );
+        return Response::BadRequest(json!({ "error": error }));
     }
 
     if true {
-        return Response::BadRequest(
-            json!({ "error": "Registration disabled." })
-        );
+        return Response::BadRequest(json!({ "error": "Registration disabled." }));
     }
 
     let col = database::get_collection("users");
@@ -152,7 +147,9 @@ pub fn verify_email(code: String) -> Response {
             )
             .expect("Failed to update user!");
 
-            email::send_welcome_email(target.to_string(), user.username);
+            if let Err(err) = email::send_welcome_email(target.to_string(), user.username) {
+                error!("Failed to send welcome email! {}", err);
+            }
 
             Response::Redirect(super::Redirect::to("https://app.revolt.chat"))
         }
@@ -174,9 +171,7 @@ pub struct Resend {
 #[post("/resend", data = "<info>")]
 pub fn resend_email(info: Json<Resend>) -> Response {
     if let Err(error) = captcha::verify(&info.captcha) {
-        return Response::BadRequest(
-            json!({ "error": error })
-        );
+        return Response::BadRequest(json!({ "error": error }));
     }
 
     let col = database::get_collection("users");
@@ -223,12 +218,11 @@ pub fn resend_email(info: Json<Resend>) -> Response {
 					None,
 				).expect("Failed to update user!");
 
-            match email::send_verification_email(info.email.to_string(), code) {
-                true => Response::Result(super::Status::Ok),
-                false => Response::InternalServerError(
-                    json!({ "error": "Failed to send email! Likely an issue with the backend API." }),
-                ),
+            if let Err(err) = email::send_verification_email(info.email.clone(), code) {
+                return Response::InternalServerError(json!({ "error": err }));
             }
+
+            Response::Result(super::Status::Ok)
         }
     } else {
         Response::NotFound(json!({ "error": "Email not found or pending verification!" }))
@@ -249,9 +243,7 @@ pub struct Login {
 #[post("/login", data = "<info>")]
 pub fn login(info: Json<Login>) -> Response {
     if let Err(error) = captcha::verify(&info.captcha) {
-        return Response::BadRequest(
-            json!({ "error": error })
-        );
+        return Response::BadRequest(json!({ "error": error }));
     }
 
     let col = database::get_collection("users");
diff --git a/src/routes/channel.rs b/src/routes/channel.rs
index 5734339..f4a1707 100644
--- a/src/routes/channel.rs
+++ b/src/routes/channel.rs
@@ -1,7 +1,7 @@
 use super::Response;
 use crate::database::{
     self, channel::Channel, get_relationship, get_relationship_internal, message::Message,
-    Permission, PermissionCalculator, Relationship, user::User
+    user::User, Permission, PermissionCalculator, Relationship,
 };
 use crate::notifications::{
     self,
@@ -506,11 +506,7 @@ pub struct SendMessage {
 
 /// send a message to a channel
 #[post("/<target>/messages", data = "<message>")]
-pub fn send_message(
-    user: User,
-    target: Channel,
-    message: Json<SendMessage>,
-) -> Option<Response> {
+pub fn send_message(user: User, target: Channel, message: Json<SendMessage>) -> Option<Response> {
     let permissions = with_permissions!(user, target);
 
     if !permissions.get_send_messages() {
@@ -657,10 +653,8 @@ pub fn edit_message(
 pub fn delete_message(user: User, target: Channel, message: Message) -> Option<Response> {
     let permissions = with_permissions!(user, target);
 
-    if !permissions.get_manage_messages() {
-        if message.author != user.id {
-            return Some(Response::LackingPermission(Permission::ManageMessages));
-        }
+    if !permissions.get_manage_messages() && message.author != user.id {
+        return Some(Response::LackingPermission(Permission::ManageMessages));
     }
 
     let col = database::get_collection("messages");
diff --git a/src/routes/guild.rs b/src/routes/guild.rs
index 0fc0752..a7b3737 100644
--- a/src/routes/guild.rs
+++ b/src/routes/guild.rs
@@ -2,7 +2,8 @@ use super::channel::ChannelType;
 use super::Response;
 use crate::database::guild::{fetch_member as get_member, get_invite, Guild, MemberKey};
 use crate::database::{
-    self, channel::fetch_channel, guild::serialise_guilds_with_channels, channel::Channel, Permission, PermissionCalculator, user::User
+    self, channel::fetch_channel, channel::Channel, guild::serialise_guilds_with_channels,
+    user::User, Permission, PermissionCalculator,
 };
 use crate::notifications::{
     self,
@@ -51,11 +52,13 @@ pub fn my_guilds(user: User) -> Response {
 #[get("/<target>")]
 pub fn guild(user: User, target: Guild) -> Option<Response> {
     with_permissions!(user, target);
-    
+
     if let Ok(result) = target.seralise_with_channels() {
         Some(Response::Success(result))
     } else {
-        Some(Response::InternalServerError(json!({ "error": "Failed to fetch channels!" })))
+        Some(Response::InternalServerError(
+            json!({ "error": "Failed to fetch channels!" }),
+        ))
     }
 }
 
@@ -153,33 +156,31 @@ pub fn remove_guild(user: User, target: Guild) -> Option<Response> {
                 json!({ "error": "Could not fetch channels." }),
             ))
         }
-    } else {
-        if database::get_collection("members")
-            .delete_one(
-                doc! {
-                    "_id.guild": &target.id,
-                    "_id.user": &user.id,
-                },
-                None,
-            )
-            .is_ok()
-        {
-            notifications::send_message_threaded(
-                None,
-                target.id.clone(),
-                Notification::guild_user_leave(UserLeave {
-                    id: target.id.clone(),
-                    user: user.id.clone(),
-                    banned: false,
-                }),
-            );
+    } else if database::get_collection("members")
+        .delete_one(
+            doc! {
+                "_id.guild": &target.id,
+                "_id.user": &user.id,
+            },
+            None,
+        )
+        .is_ok()
+    {
+        notifications::send_message_threaded(
+            None,
+            target.id.clone(),
+            Notification::guild_user_leave(UserLeave {
+                id: target.id.clone(),
+                user: user.id.clone(),
+                banned: false,
+            }),
+        );
 
-            Some(Response::Result(super::Status::Ok))
-        } else {
-            Some(Response::InternalServerError(
-                json!({ "error": "Failed to remove you from the guild." }),
-            ))
-        }
+        Some(Response::Result(super::Status::Ok))
+    } else {
+        Some(Response::InternalServerError(
+            json!({ "error": "Failed to remove you from the guild." }),
+        ))
     }
 }
 
@@ -243,7 +244,7 @@ pub fn create_channel(user: User, target: Guild, info: Json<CreateChannel>) -> O
                             "channels": &id
                         }
                     },
-                    None
+                    None,
                 )
                 .is_ok()
             {
@@ -326,10 +327,8 @@ pub fn remove_invite(user: User, target: Guild, code: String) -> Option<Response
     let (permissions, _) = with_permissions!(user, target);
 
     if let Some((guild_id, _, invite)) = get_invite(&code, None) {
-        if invite.creator != user.id {
-            if !permissions.get_manage_server() {
-                return Some(Response::LackingPermission(Permission::ManageServer));
-            }
+        if invite.creator != user.id && !permissions.get_manage_server() {
+            return Some(Response::LackingPermission(Permission::ManageServer));
         }
 
         if database::get_collection("guilds")
@@ -621,14 +620,16 @@ pub fn kick_member(user: User, target: Guild, other: String) -> Option<Response>
         return Some(Response::LackingPermission(Permission::KickMembers));
     }
 
-    if let Ok(result) = get_member(MemberKey( target.id.clone(), other.clone() )) {
+    if let Ok(result) = get_member(MemberKey(target.id.clone(), other.clone())) {
         if result.is_none() {
             return Some(Response::BadRequest(
                 json!({ "error": "User not part of guild." }),
             ));
         }
     } else {
-        return Some(Response::InternalServerError(json!({ "error": "Failed to fetch member." })))
+        return Some(Response::InternalServerError(
+            json!({ "error": "Failed to fetch member." }),
+        ));
     }
 
     if database::get_collection("members")
@@ -691,14 +692,16 @@ pub fn ban_member(
         return Some(Response::LackingPermission(Permission::BanMembers));
     }
 
-    if let Ok(result) = get_member(MemberKey( target.id.clone(), other.clone() )) {
+    if let Ok(result) = get_member(MemberKey(target.id.clone(), other.clone())) {
         if result.is_none() {
             return Some(Response::BadRequest(
                 json!({ "error": "User not part of guild." }),
             ));
         }
     } else {
-        return Some(Response::InternalServerError(json!({ "error": "Failed to fetch member." })))
+        return Some(Response::InternalServerError(
+            json!({ "error": "Failed to fetch member." }),
+        ));
     }
 
     if database::get_collection("guilds")
diff --git a/src/routes/root.rs b/src/routes/root.rs
index 29715ac..124f095 100644
--- a/src/routes/root.rs
+++ b/src/routes/root.rs
@@ -1,7 +1,7 @@
 use super::Response;
+use crate::util::variables::{USE_EMAIL_VERIFICATION, USE_HCAPTCHA};
 
 use mongodb::bson::doc;
-use std::env;
 
 /// root
 #[get("/")]
@@ -14,7 +14,8 @@ pub fn root() -> Response {
             "patch": 9
         },
         "features": {
-            "captcha": env::var("HCAPTCHA_KEY").is_ok()
+            "email_verification": USE_EMAIL_VERIFICATION.clone(),
+            "captcha": USE_HCAPTCHA.clone(),
         }
     }))
 }
diff --git a/src/routes/user.rs b/src/routes/user.rs
index 145a61b..19f2719 100644
--- a/src/routes/user.rs
+++ b/src/routes/user.rs
@@ -1,5 +1,7 @@
 use super::Response;
-use crate::database::{self, get_relationship, get_relationship_internal, mutual, Relationship, user::User};
+use crate::database::{
+    self, get_relationship, get_relationship_internal, user::User, Relationship,
+};
 use crate::notifications::{
     self,
     events::{users::*, Notification},
@@ -15,18 +17,14 @@ use ulid::Ulid;
 /// retrieve your user information
 #[get("/@me")]
 pub fn me(user: User) -> Response {
-    Response::Success(
-        user.serialise(Relationship::SELF as i32)
-    )
+    Response::Success(user.serialise(Relationship::SELF as i32))
 }
 
 /// retrieve another user's information
 #[get("/<target>")]
 pub fn user(user: User, target: User) -> Response {
     let relationship = get_relationship(&user, &target) as i32;
-    Response::Success(
-        user.serialise(relationship)
-    )
+    Response::Success(user.serialise(relationship))
 }
 
 #[derive(Serialize, Deserialize)]
diff --git a/src/util/captcha.rs b/src/util/captcha.rs
index caa672b..d2dda33 100644
--- a/src/util/captcha.rs
+++ b/src/util/captcha.rs
@@ -1,11 +1,11 @@
-use serde::{Serialize, Deserialize};
 use reqwest::blocking::Client;
+use serde::{Deserialize, Serialize};
 use std::collections::HashMap;
 use std::env;
 
 #[derive(Serialize, Deserialize)]
 struct CaptchaResponse {
-    success: bool
+    success: bool,
 }
 
 pub fn verify(user_token: &Option<String>) -> Result<(), String> {
diff --git a/src/util/email.rs b/src/util/email.rs
new file mode 100644
index 0000000..c215999
--- /dev/null
+++ b/src/util/email.rs
@@ -0,0 +1,96 @@
+use lettre::message::{header, MultiPart, SinglePart};
+use lettre::transport::smtp::authentication::Credentials;
+use lettre::{Message, SmtpTransport, Transport};
+
+use super::variables::{PUBLIC_URL, SMTP_FROM, SMTP_HOST, SMTP_PASSWORD, SMTP_USERNAME};
+
+lazy_static! {
+    static ref MAILER: lettre::transport::smtp::SmtpTransport =
+        SmtpTransport::relay(SMTP_HOST.as_ref())
+            .unwrap()
+            .credentials(Credentials::new(
+                SMTP_USERNAME.to_string(),
+                SMTP_PASSWORD.to_string()
+            ))
+            .build();
+}
+
+fn send(message: Message) -> Result<(), String> {
+    MAILER
+        .send(&message)
+        .map_err(|err| format!("Failed to send email! {}", err.to_string()))?;
+
+    Ok(())
+}
+
+fn generate_multipart(text: &str, html: &str) -> MultiPart {
+    MultiPart::mixed().multipart(
+        MultiPart::alternative()
+            .singlepart(
+                SinglePart::quoted_printable()
+                    .header(header::ContentType(
+                        "text/plain; charset=utf8".parse().unwrap(),
+                    ))
+                    .body(text),
+            )
+            .multipart(
+                MultiPart::related().singlepart(
+                    SinglePart::eight_bit()
+                        .header(header::ContentType(
+                            "text/html; charset=utf8".parse().unwrap(),
+                        ))
+                        .body(html),
+                ),
+            ),
+    )
+}
+
+pub fn send_verification_email(email: String, code: String) -> Result<(), String> {
+    let url = format!("{}/api/account/verify/{}", PUBLIC_URL.to_string(), code);
+    let email = Message::builder()
+        .from(SMTP_FROM.to_string().parse().unwrap())
+        .to(email.parse().unwrap())
+        .subject("Verify your email!")
+        .multipart(generate_multipart(
+            &format!("Verify your email here: {}", url),
+            &format!("<a href=\"{}\">Click to verify your email!</a>", url),
+        ))
+        .unwrap();
+
+    send(email)
+}
+
+pub fn send_password_reset(email: String, code: String) -> Result<(), String> {
+    let url = format!("{}/api/account/reset/{}", PUBLIC_URL.to_string(), code);
+    let email = Message::builder()
+        .from(SMTP_FROM.to_string().parse().unwrap())
+        .to(email.parse().unwrap())
+        .subject("Reset your password.")
+        .multipart(generate_multipart(
+            &format!("Reset your password here: {}", url),
+            &format!("<a href=\"{}\">Click to reset your password!</a>", url),
+        ))
+        .unwrap();
+
+    send(email)
+}
+
+pub fn send_welcome_email(email: String, username: String) -> Result<(), String> {
+    let email = Message::builder()
+        .from(SMTP_FROM.to_string().parse().unwrap())
+        .to(email.parse().unwrap())
+        .subject("Welcome to REVOLT!")
+        .multipart(
+        generate_multipart(
+            &format!("Welcome, {}! You can now use REVOLT.", username),
+            &format!(
+                    "<b>Welcome, {}!</b><br/>You can now use REVOLT.<br/><a href=\"{}\">Go to REVOLT</a>",
+                    username,
+                    PUBLIC_URL.to_string()
+                )
+            )
+        )
+        .unwrap();
+
+    send(email)
+}
diff --git a/src/util/mod.rs b/src/util/mod.rs
index c88ae63..abd1031 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -3,6 +3,8 @@ use rand::{distributions::Alphanumeric, Rng};
 use std::iter::FromIterator;
 
 pub mod captcha;
+pub mod email;
+pub mod variables;
 
 pub fn vec_to_set<T: Clone + Eq + std::hash::Hash>(data: &[T]) -> HashSet<T> {
     HashSet::from_iter(data.iter().cloned())
diff --git a/src/util/variables.rs b/src/util/variables.rs
new file mode 100644
index 0000000..2965d9d
--- /dev/null
+++ b/src/util/variables.rs
@@ -0,0 +1,27 @@
+use std::env;
+
+lazy_static! {
+    pub static ref MONGO_URI: String =
+        env::var("REVOLT_MONGO_URI").expect("Missing REVOLT_MONGO_URI environment variable.");
+    pub static ref PUBLIC_URL: String =
+        env::var("REVOLT_PUBLIC_URL").expect("Missing REVOLT_PUBLIC_URL environment variable.");
+    pub static ref USE_EMAIL_VERIFICATION: bool = env::var("REVOLT_USE_EMAIL_VERIFICATION").map_or(
+        env::var("REVOLT_SMTP_HOST").is_ok()
+            && env::var("REVOLT_SMTP_USERNAME").is_ok()
+            && env::var("REVOLT_SMTP_PASSWORD").is_ok()
+            && env::var("REVOLT_SMTP_FROM").is_ok(),
+        |v| v == *"1"
+    );
+    pub static ref USE_HCAPTCHA: bool = env::var("REVOLT_HCAPTCHA_KEY").is_ok();
+    pub static ref SMTP_HOST: String =
+        env::var("REVOLT_SMTP_HOST").unwrap_or_else(|_| "".to_string());
+    pub static ref SMTP_USERNAME: String =
+        env::var("SMTP_USERNAME").unwrap_or_else(|_| "".to_string());
+    pub static ref SMTP_PASSWORD: String =
+        env::var("SMTP_PASSWORD").unwrap_or_else(|_| "".to_string());
+    pub static ref SMTP_FROM: String = env::var("SMTP_FROM").unwrap_or_else(|_| "".to_string());
+    pub static ref HCAPTCHA_KEY: String =
+        env::var("REVOLT_HCAPTCHA_KEY").unwrap_or_else(|_| "".to_string());
+    pub static ref WS_HOST: String =
+        env::var("REVOLT_WS_HOST").unwrap_or_else(|_| "0.0.0.0:9999".to_string());
+}
-- 
GitLab