88from hyper import HTTP20Connection
99from firebase_admin import initialize_app , messaging
1010import boto3
11+ from sendgrid import SendGridAPIClient
12+ from sendgrid .helpers .mail import Mail , Email , To , Personalization
1113
1214from app .coursegrab .dao .sections_dao import get_users_tracking_section
1315from app .coursegrab .dao .sessions_dao import delete_session_expired_device_tokens
2426# Initialize FCM
2527firebase_app = initialize_app ()
2628
27- # Initialize Amazon SES client
29+ # Email provider is selectable via env var so we can switch between SendGrid and
30+ # Amazon SES without a code change ("sendgrid" while SES production access is pending,
31+ # "ses" once approved). Defaults to sendgrid.
32+ EMAIL_PROVIDER = os .environ .get ("EMAIL_PROVIDER" , "sendgrid" ).lower ()
33+
34+ # Initialize the selected email client
35+ ses_client = None
36+ sendgrid_client = None
2837try :
29- ses_client = boto3 .client (
30- "ses" ,
31- region_name = os .environ ["AWS_REGION" ],
32- aws_access_key_id = os .environ ["AWS_ACCESS_KEY_ID" ],
33- aws_secret_access_key = os .environ ["AWS_SECRET_ACCESS_KEY" ],
34- )
38+ if EMAIL_PROVIDER == "ses" :
39+ ses_client = boto3 .client (
40+ "ses" ,
41+ region_name = os .environ ["AWS_REGION" ],
42+ aws_access_key_id = os .environ ["AWS_ACCESS_KEY_ID" ],
43+ aws_secret_access_key = os .environ ["AWS_SECRET_ACCESS_KEY" ],
44+ )
45+ else :
46+ sendgrid_client = SendGridAPIClient (os .environ ["SENDGRID_API_KEY" ])
3547except :
36- print ("Error initializing SES " )
48+ print (f "Error initializing email client for provider: { EMAIL_PROVIDER } " )
3749
3850# Initialize email body
3951try :
@@ -181,19 +193,37 @@ def send_emails(section, emails):
181193def send_single_email (subject_code , course_num , trimmed_section_name , email_chunk ):
182194 """Send email notification to single chunk of user emails (max 49 bcc emails)"""
183195 course_name_full = f"{ subject_code } { course_num } { trimmed_section_name } "
196+ subject = f"{ course_name_full } is Now Open"
197+ html_content = email_body .replace ("COURSE_NAME_NUM" , course_name_full )
184198
185- try :
186- ses_client .send_email (
187- Source = f"CourseGrab by AppDev <{ COURSEGRAB_FROM_EMAIL } >" ,
188- Destination = {
189- "ToAddresses" : [COURSEGRAB_TO_EMAIL ],
190- "BccAddresses" : list (email_chunk ), # Add users' emails to bcc
191- },
192- Message = {
193- "Subject" : {"Data" : f"{ course_name_full } is Now Open" },
194- "Body" : {"Html" : {"Data" : email_body .replace ("COURSE_NAME_NUM" , course_name_full )}},
195- },
199+ if EMAIL_PROVIDER == "ses" :
200+ try :
201+ ses_client .send_email (
202+ Source = f"CourseGrab by AppDev <{ COURSEGRAB_FROM_EMAIL } >" ,
203+ Destination = {
204+ "ToAddresses" : [COURSEGRAB_TO_EMAIL ],
205+ "BccAddresses" : list (email_chunk ), # Add users' emails to bcc
206+ },
207+ Message = {
208+ "Subject" : {"Data" : subject },
209+ "Body" : {"Html" : {"Data" : html_content }},
210+ },
211+ )
212+ except Exception as e :
213+ print (f"Error sending email: { e } " )
214+ else : # sendgrid
215+ message = Mail (
216+ from_email = Email (COURSEGRAB_FROM_EMAIL , "CourseGrab by AppDev" ),
217+ subject = subject ,
218+ html_content = html_content ,
196219 )
197- except Exception as e :
198- print (f"Error sending email: { e } " )
220+ personalization = Personalization ()
221+ personalization .add_to (To (COURSEGRAB_TO_EMAIL ))
222+ for bcc_email in email_chunk : # Add users' emails to bcc
223+ personalization .add_bcc (Email (bcc_email ))
224+ message .add_personalization (personalization )
225+ try :
226+ sendgrid_client .send (message )
227+ except Exception as e :
228+ print (f"Error sending email: { e } " )
199229
0 commit comments