@@ -37,6 +37,10 @@ def __init__(self, config: EmailConfig):
3737 config .reset_password_template_id
3838 or os .getenv ("SENDGRID_RESET_TEMPLATE_ID" )
3939 )
40+ self .password_changed_template_id = (
41+ config .password_changed_template_id
42+ or os .getenv ("SENDGRID_PASSWORD_CHANGED_TEMPLATE_ID" )
43+ )
4044 self .client = SendGridAPIClient (api_key = self .api_key )
4145 self .sender_name = config .sender_name
4246
@@ -208,3 +212,46 @@ async def send_password_reset_email(
208212 )
209213 logger .error (error_msg )
210214 raise RuntimeError (error_msg ) from e
215+
216+ async def send_password_changed_email (
217+ self ,
218+ to_email : str ,
219+ dynamic_template_data : Optional [dict ] = None ,
220+ * args ,
221+ ** kwargs
222+ ) -> None :
223+ try :
224+ if hasattr (self , 'password_changed_template_id' ) and self .password_changed_template_id :
225+ await self .send_email (
226+ to_email = to_email ,
227+ template_id = self .password_changed_template_id ,
228+ dynamic_template_data = dynamic_template_data ,
229+ )
230+ else :
231+ subject = "Your Password Has Been Changed"
232+ body = """
233+ Your password has been successfully changed.
234+
235+ If you did not make this change, please contact support immediately and secure your account.
236+
237+ For security reasons, you will need to log in again on all your devices.
238+ """
239+ html_body = """
240+ <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
241+ <h1>Password Changed Successfully</h1>
242+ <p>Your password has been successfully changed.</p>
243+
244+ <p>For security reasons, you will need to log in again on all your devices.</p>
245+ </div>
246+ """
247+ # Move send_email inside the else block
248+ await self .send_email (
249+ to_email = to_email ,
250+ subject = subject ,
251+ html_body = html_body ,
252+ body = body ,
253+ )
254+ except Exception as e :
255+ error_msg = f"Failed to send password change notification to { to_email } : { str (e )} "
256+ logger .error (error_msg )
257+ raise RuntimeError (error_msg ) from e
0 commit comments