33import boto3
44from botocore .exceptions import ClientError
55
6- from app .interfaces .email_service import EmailContent , EmailTemplate
76from app .interfaces .email_service_provider import IEmailServiceProvider
7+ from app .schemas .email_template import EmailContent , EmailTemplateType
88
99
1010class AmazonSESEmailProvider (IEmailServiceProvider ):
11+ """Amazon SES Email Provider.
12+
13+ This class is responsible for sending emails using Amazon SES.
14+ """
15+
16+ """
17+ Args:
18+ aws_access_key (str): AWS Access Key ID
19+ aws_secret_key (str): AWS Secret Access Key
20+ region (str): AWS region where SES is configured
21+ source_email (str): Email address from which the email will be sent
22+ is_sandbox (bool): If True, the amazon provider will only be able to send emails
23+ to previously verified email addresses and domains
24+ """
25+
1126 def __init__ (
1227 self ,
1328 aws_access_key : str ,
@@ -30,28 +45,28 @@ def __init__(
3045 response = self .ses_client .list_verified_email_addresses ()
3146 self .verified_emails = response .get ("VerifiedEmailAddresses" , [])
3247
33- def _verify_email (self , email : str ):
34- if not self .is_sandbox :
35- return
48+ def _verify_email (self , email : str , templateType : EmailTemplateType ) -> None :
3649 try :
37- if email not in self .verified_emails :
50+ if self . is_sandbox and email not in self .verified_emails :
3851 self .ses_client .verify_email_identity (EmailAddress = email )
3952 print (f"Verification email sent to { email } ." )
53+ if self .ses_client .get_template (TemplateName = templateType .value ):
54+ print (f"Template { templateType .value } exists." )
4055 except Exception as e :
4156 print (f"Failed to verify email: { e } " )
4257
43- def send_email (self , template : EmailTemplate , content : EmailContent ) -> dict :
58+ def send_email (
59+ self , templateType : EmailTemplateType , content : EmailContent
60+ ) -> dict :
4461 try :
45- self ._verify_email (content .recipient )
46-
47- self .ses_client .get_template (TemplateName = template .value )
62+ self ._verify_email (content .recipient , templateType )
4863
4964 template_data = content .data .get_formatted_string ()
5065
5166 response = self .ses_client .send_templated_email (
5267 Source = self .source_email ,
5368 Destination = {"ToAddresses" : [content .recipient ]},
54- Template = template .value ,
69+ Template = templateType .value ,
5570 TemplateData = template_data ,
5671 )
5772
0 commit comments