@@ -28,7 +28,9 @@ use diesel::{
2828 result:: Error :: NotFound ,
2929 PgConnection ,
3030} ;
31+ #[ cfg( not( test) ) ]
3132use lettre:: message:: header:: ContentType ;
33+ #[ cfg( not( test) ) ]
3234use lettre:: { Message , Transport } ;
3335use rand:: Rng ;
3436
@@ -159,21 +161,35 @@ pub async fn validate_auth_token(
159161}
160162
161163pub fn send_email ( email : & str , subject : & str , body : String , state : & web:: Data < AppState > ) {
162- let email = Message :: builder ( )
163- . from (
164- format ! ( "{} <{}>" , state. sender_name, state. sender_email)
165- . parse ( )
166- . unwrap ( ) ,
167- )
168- . to ( email. parse ( ) . unwrap ( ) )
169- . subject ( subject)
170- . header ( ContentType :: TEXT_HTML )
171- . body ( body)
172- . unwrap ( ) ;
173-
174- match state. mailer . send ( & email) {
175- Ok ( _) => println ! ( "Email sent successfully!" ) ,
176- Err ( e) => panic ! ( "Could not send email: {e:?}" ) ,
164+ #[ cfg( not( test) ) ]
165+ {
166+ if let Some ( ref mailer) = state. mailer {
167+ let email = Message :: builder ( )
168+ . from (
169+ format ! ( "{} <{}>" , state. sender_name, state. sender_email)
170+ . parse ( )
171+ . unwrap ( ) ,
172+ )
173+ . to ( email. parse ( ) . unwrap ( ) )
174+ . subject ( subject)
175+ . header ( ContentType :: TEXT_HTML )
176+ . body ( body)
177+ . unwrap ( ) ;
178+
179+ match mailer. send ( & email) {
180+ Ok ( _) => println ! ( "Email sent successfully!" ) ,
181+ Err ( e) => panic ! ( "Could not send email: {e:?}" ) ,
182+ }
183+ } else {
184+ println ! ( "No mailer configured, email not sent" ) ;
185+ }
186+ }
187+
188+ #[ cfg( test) ]
189+ {
190+ let _ = body;
191+ let _ = state;
192+ println ! ( "Test mode: Email would be sent to {} with subject '{}'" , email, subject) ;
177193 }
178194}
179195
0 commit comments