from common.config.app_settings import app_settings from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail from common.log.module_logger import ModuleLogger class EmailHandler: def __init__(self) -> None: pass async def send_email(self, message: dict): receiver_id = message["receiver_id"] content_text = message["properties"]["content_text"] content_subject = message["properties"]["content_subject"] receiver_type = message["properties"]["receiver_type"] module_logger = ModuleLogger(sender_id="EmailHandler") if receiver_type == "email": receiver_email = receiver_id else: receiver_email = None module_logger.log_info( "unsupported receiver_type: '{}'".format(receiver_type) ) return mail = Mail( from_email=app_settings.EMAIL_FROM, to_emails=receiver_email, subject=content_subject, html_content=content_text, ) try: sg = SendGridAPIClient(app_settings.SENDGRID_API_KEY) response = sg.send(mail) await module_logger.log_info( info=f"SendGridAPIClient:response:status_code:{response.status_code} | body:{response.body}", properties=message["properties"], ) except Exception as e: await module_logger.log_exception(e)