1- import os
21import json
32import logging
4- from typing import Dict , Any
3+ import os
4+ from typing import Any , Dict
55
66import boto3
77from botocore .exceptions import ClientError
@@ -16,7 +16,7 @@ def __init__(self):
1616 self .aws_access_key = os .getenv ("AWS_ACCESS_KEY" )
1717 self .aws_secret_key = os .getenv ("AWS_SECRET_KEY" )
1818 self .source_email = os .getenv ("SES_SOURCE_EMAIL" )
19-
19+
2020 if not all ([self .aws_region , self .aws_access_key , self .aws_secret_key , self .source_email ]):
2121 self .logger .warning ("SES credentials not fully configured. Email sending will be disabled." )
2222 self .ses_client = None
@@ -39,7 +39,7 @@ def verify_email_address(self, email: str) -> bool:
3939 """
4040 if not self .ses_client :
4141 return False
42-
42+
4343 try :
4444 self .ses_client .verify_email_identity (EmailAddress = email )
4545 self .logger .info (f"Email verification request sent to { email } " )
@@ -68,22 +68,22 @@ def send_templated_email(self, to_email: str, template_name: str, template_data:
6868 if not self .ses_client :
6969 self .logger .error ("SES client not available. Cannot send email." )
7070 return False
71-
71+
7272 try :
7373 response = self .ses_client .send_templated_email (
7474 Source = self .source_email ,
7575 Destination = {'ToAddresses' : [to_email ]},
7676 Template = template_name ,
7777 TemplateData = json .dumps (template_data )
7878 )
79-
79+
8080 self .logger .info (f"Email sent successfully to { to_email } . MessageId: { response ['MessageId' ]} " )
8181 return True
82-
82+
8383 except ClientError as e :
8484 error_code = e .response ['Error' ]['Code' ]
8585 error_message = e .response ['Error' ]['Message' ]
86-
86+
8787 if error_code == 'MessageRejected' and 'not verified' in error_message :
8888 # Try to verify the email address automatically
8989 self .logger .info (f"Email { to_email } not verified. Attempting to verify..." )
@@ -113,7 +113,7 @@ def send_verification_email(self, to_email: str, verification_link: str) -> bool
113113 template_data = {
114114 "verification_link" : verification_link
115115 }
116-
116+
117117 return self .send_templated_email (to_email , "EmailVerification" , template_data )
118118
119119 def send_password_reset_email (self , to_email : str , reset_link : str ) -> bool :
@@ -130,5 +130,5 @@ def send_password_reset_email(self, to_email: str, reset_link: str) -> bool:
130130 template_data = {
131131 "reset_link" : reset_link
132132 }
133-
134- return self .send_templated_email (to_email , "PasswordReset" , template_data )
133+
134+ return self .send_templated_email (to_email , "PasswordReset" , template_data )
0 commit comments