@ -11,16 +11,16 @@ pub extern crate actix;
pub extern crate actix_web ;
pub extern crate bcrypt ;
pub extern crate chrono ;
pub extern crate crypto ;
pub extern crate dotenv ;
pub extern crate jsonwebtoken ;
pub extern crate lettre ;
pub extern crate lettre_email ;
pub extern crate rand ;
pub extern crate regex ;
pub extern crate serde ;
pub extern crate serde_json ;
pub extern crate strum ;
pub extern crate lettre ;
pub extern crate lettre_email ;
pub extern crate crypto ;
pub mod api ;
pub mod apub ;
@ -30,15 +30,15 @@ pub mod websocket;
use chrono ::{ DateTime , NaiveDateTime , Utc } ;
use dotenv ::dotenv ;
use regex ::Regex ;
use std ::env ;
use rand ::{ thread_rng , Rng } ;
use rand ::distributions ::Alphanumeric ;
use lettre ::{ SmtpClient , Transport } ;
use lettre_email ::{ Email } ;
use lettre ::smtp ::authentication ::{ Credentials , Mechanism } ;
use lettre ::smtp ::extension ::ClientId ;
use lettre ::smtp ::ConnectionReuseParameters ;
use lettre ::{ SmtpClient , Transport } ;
use lettre_email ::Email ;
use rand ::distributions ::Alphanumeric ;
use rand ::{ thread_rng , Rng } ;
use regex ::Regex ;
use std ::env ;
pub struct Settings {
db_url : String ,
@ -63,18 +63,18 @@ pub struct EmailConfig {
impl Settings {
fn get ( ) -> Self {
dotenv ( ) . ok ( ) ;
let email_config = if env ::var ( "SMTP_SERVER" ) . is_ok ( ) & &
! env ::var ( "SMTP_SERVER" ) . unwrap ( ) . eq ( "" ) {
Some ( EmailConfig {
smtp_server : env ::var ( "SMTP_SERVER" ) . expect ( "SMTP_SERVER must be set" ) ,
smtp_login : env ::var ( "SMTP_LOGIN" ) . expect ( "SMTP_LOGIN must be set" ) ,
smtp_password : env ::var ( "SMTP_PASSWORD" ) . expect ( "SMTP_PASSWORD must be set" ) ,
smtp_from_address : env ::var ( "SMTP_FROM_ADDRESS" ) . expect ( "SMTP_FROM_ADDRESS must be set" )
} )
} else {
None
} ;
let email_config =
if env ::var ( "SMTP_SERVER" ) . is_ok ( ) & & ! env ::var ( "SMTP_SERVER" ) . unwrap ( ) . eq ( "" ) {
Some ( EmailConfig {
smtp_server : env ::var ( "SMTP_SERVER" ) . expect ( "SMTP_SERVER must be set" ) ,
smtp_login : env ::var ( "SMTP_LOGIN" ) . expect ( "SMTP_LOGIN must be set" ) ,
smtp_password : env ::var ( "SMTP_PASSWORD" ) . expect ( "SMTP_PASSWORD must be set" ) ,
smtp_from_address : env ::var ( "SMTP_FROM_ADDRESS" ) . expect ( "SMTP_FROM_ADDRESS must be set" ) ,
} )
} else {
None
} ;
Settings {
db_url : env ::var ( "DATABASE_URL" ) . expect ( "DATABASE_URL must be set" ) ,
@ -104,7 +104,7 @@ impl Settings {
. unwrap_or ( "3600" . to_string ( ) )
. parse ( )
. unwrap ( ) ,
email_config : email_config ,
email_config : email_config ,
}
}
fn api_endpoint ( & self ) -> String {
@ -151,29 +151,35 @@ pub fn extract_usernames(test: &str) -> Vec<&str> {
}
pub fn generate_random_string ( ) -> String {
thread_rng ( )
. sample_iter ( & Alphanumeric )
. take ( 30 )
. collect ( )
thread_rng ( ) . sample_iter ( & Alphanumeric ) . take ( 30 ) . collect ( )
}
pub fn send_email ( subject : & str , to_email : & str , to_username : & str , html : & str ) -> Result < ( ) , String > {
pub fn send_email (
subject : & str ,
to_email : & str ,
to_username : & str ,
html : & str ,
) -> Result < ( ) , String > {
let email_config = Settings ::get ( ) . email_config . ok_or ( "no_email_setup" ) ? ;
let email = Email ::builder ( )
. to ( ( to_email , to_username ) )
. from ( ( email_config . smtp_login . to_owned ( ) , email_config . smtp_from_address ) )
. from ( (
email_config . smtp_login . to_owned ( ) ,
email_config . smtp_from_address ,
) )
. subject ( subject )
. html ( html )
. build ( )
. unwrap ( ) ;
let mut mailer = SmtpClient ::new_simple ( & email_config . smtp_server ) . unwrap ( )
let mut mailer = SmtpClient ::new_simple ( & email_config . smtp_server )
. unwrap ( )
. hello_name ( ClientId ::Domain ( "localhost" . to_string ( ) ) )
. credentials ( Credentials ::new (
email_config . smtp_login . to_owned ( ) ,
email_config . smtp_password . to_owned ( ) ) )
email_config . smtp_login . to_owned ( ) ,
email_config . smtp_password . to_owned ( ) ,
) )
. smtp_utf8 ( true )
. authentication_mechanism ( Mechanism ::Plain )
. connection_reuse ( ConnectionReuseParameters ::ReuseUnlimited )