Skip to content

Commit cade43a

Browse files
committed
sendgrid temporary migration
1 parent c761bd7 commit cade43a

3 files changed

Lines changed: 54 additions & 21 deletions

File tree

envrc.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ export PORT=5000
1111
export AWS_ACCESS_KEY_ID=FILL_IN
1212
export AWS_SECRET_ACCESS_KEY=FILL_IN
1313
export AWS_REGION=us-east-2
14+
export SENDGRID_API_KEY=FILL_IN
15+
export EMAIL_PROVIDER=sendgrid

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ requests==2.22.0
6969
requests-oauthlib==1.3.0
7070
rsa==4.0
7171
boto3==1.37.38
72+
sendgrid==6.1.2
7273
six==1.13.0
7374
soupsieve==1.9.5
7475
SQLAlchemy==1.3.11

src/app/coursegrab/notifications/push_notifications.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from hyper import HTTP20Connection
99
from firebase_admin import initialize_app, messaging
1010
import boto3
11+
from sendgrid import SendGridAPIClient
12+
from sendgrid.helpers.mail import Mail, Email, To, Personalization
1113

1214
from app.coursegrab.dao.sections_dao import get_users_tracking_section
1315
from app.coursegrab.dao.sessions_dao import delete_session_expired_device_tokens
@@ -24,16 +26,26 @@
2426
# Initialize FCM
2527
firebase_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
2837
try:
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"])
3547
except:
36-
print("Error initializing SES")
48+
print(f"Error initializing email client for provider: {EMAIL_PROVIDER}")
3749

3850
# Initialize email body
3951
try:
@@ -181,19 +193,37 @@ def send_emails(section, emails):
181193
def 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

Comments
 (0)