|
3 | 3 | """ |
4 | 4 | import time |
5 | 5 | from datetime import date, datetime, timedelta |
| 6 | + |
| 7 | +import boto3 |
| 8 | +from edx_ace.channel.django_email import DjangoEmailChannel |
| 9 | +from edx_ace.channel.mixins import EmailChannelMixin |
6 | 10 | from eventtracking import tracker |
7 | 11 | import logging |
8 | 12 | import uuid |
9 | 13 |
|
10 | 14 | from django.conf import settings |
11 | 15 | from django.contrib.sites.models import Site |
12 | 16 | from django.core.management.base import BaseCommand |
13 | | -from edx_ace import ace |
| 17 | +from edx_ace import ace, presentation |
14 | 18 | from edx_ace.message import Message |
15 | 19 | from edx_ace.recipient import Recipient |
16 | | - |
| 20 | +from edx_ace.utils.signals import send_ace_message_sent_signal |
17 | 21 | from common.djangoapps.student.models import CourseEnrollment |
18 | 22 | from lms.djangoapps.certificates.api import get_certificate_for_user_id |
19 | 23 | from lms.djangoapps.certificates.data import CertificateStatuses |
@@ -121,7 +125,11 @@ def send_ace_message(goal, session_id): |
121 | 125 | with emulate_http_request(site, user): |
122 | 126 | try: |
123 | 127 | start_time = time.perf_counter() |
124 | | - ace.send(msg) |
| 128 | + if is_ses_enabled: |
| 129 | + # experimental implementation to log errors with ses |
| 130 | + send_email_using_ses(user, msg) |
| 131 | + else: |
| 132 | + ace.send(msg) |
125 | 133 | end_time = time.perf_counter() |
126 | 134 | log.info(f"Goal Reminder for {user.id} for course {goal.course_key} sent in {end_time - start_time} " |
127 | 135 | f"using {'SES' if is_ses_enabled else 'others'}") |
@@ -297,3 +305,46 @@ def handle_goal(goal, today, sunday_date, monday_date, session_id): |
297 | 305 | return True |
298 | 306 |
|
299 | 307 | return False |
| 308 | + |
| 309 | + |
| 310 | +def send_email_using_ses(user, msg): |
| 311 | + """ |
| 312 | + Send email using AWS SES |
| 313 | + """ |
| 314 | + msg = presentation.render(DjangoEmailChannel, msg) |
| 315 | + # send rendered email using SES |
| 316 | + sender = EmailChannelMixin.get_from_address(msg) |
| 317 | + recipient = user.email |
| 318 | + subject = EmailChannelMixin.get_subject(msg) |
| 319 | + body_text = msg.body |
| 320 | + body_html = msg.body_html |
| 321 | + |
| 322 | + try: |
| 323 | + # Send email |
| 324 | + response = boto3.client('ses', settings.AWS_SES_REGION_NAME).send_email( |
| 325 | + Source=sender, |
| 326 | + Destination={ |
| 327 | + 'ToAddresses': [recipient], |
| 328 | + }, |
| 329 | + Message={ |
| 330 | + 'Subject': { |
| 331 | + 'Data': subject, |
| 332 | + 'Charset': 'UTF-8' |
| 333 | + }, |
| 334 | + 'Body': { |
| 335 | + 'Text': { |
| 336 | + 'Data': body_text, |
| 337 | + 'Charset': 'UTF-8' |
| 338 | + }, |
| 339 | + 'Html': { |
| 340 | + 'Data': body_html, |
| 341 | + 'Charset': 'UTF-8' |
| 342 | + } |
| 343 | + } |
| 344 | + } |
| 345 | + ) |
| 346 | + |
| 347 | + log.info(f"Goal Reminder Email: email sent using SES with message ID {response['MessageId']}") |
| 348 | + send_ace_message_sent_signal(DjangoEmailChannel, msg) |
| 349 | + except Exception as e: |
| 350 | + log.error(f"Goal Reminder Email: Error sending email using SES: {e}") |
0 commit comments