Added AWS SES email

This commit is contained in:
2026-04-25 16:24:26 -04:00
parent 98c1b1fa75
commit 58e70cb655
5 changed files with 132 additions and 0 deletions
View File
+39
View File
@@ -0,0 +1,39 @@
import boto3
from botocore.exceptions import ClientError
from app.utils.logger import logger
# @staticmethod
def send_email_ses(to_email, subject, html_content, text_content=None):
AWS_REGION = "us-east-1"
FROM_EMAIL = "support@mermsemr.com" # Must be verified in SES
client = boto3.client(
"ses",
region_name=AWS_REGION,
# Credentials pulled from env vars: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
# or from IAM role if running on EC2/ECS
)
body = {"Html": {"Charset": "UTF-8", "Data": html_content}}
if text_content:
body["Text"] = {"Charset": "UTF-8", "Data": text_content}
try:
response = client.send_email(
Source=FROM_EMAIL,
Destination={"ToAddresses": [to_email]},
Message={
"Subject": {"Charset": "UTF-8", "Data": subject},
"Body": body,
},
)
logger.info(f"SES email sent. MessageId: {response['MessageId']}")
return response
except ClientError as e:
logger.error(f"SES ClientError: {e.response['Error']['Message']}")
raise
except Exception as e:
logger.error(f"SES send failed: {e}")
raise
View File
@@ -0,0 +1,37 @@
from app.utils.logger import logger
from marshmallow import ValidationError
from flask_mail import Mail, Message
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
@staticmethod
def test_new_mailer():
# Replace these with your actual details
SENDGRID_API_KEY = "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"
FROM_EMAIL = 'support@mermsemr.com' # Must be a verified sender in SendGrid
TO_EMAIL = 'works@chiefsoft.com'
message = Mail(
from_email=FROM_EMAIL,
to_emails=TO_EMAIL,
subject='Sending with Twilio SendGrid is Fun',
html_content='<strong>and easy to do anywhere, even with Python</strong>'
)
try:
# Initialize the SendGrid client
sg = SendGridAPIClient(SENDGRID_API_KEY)
# Send the email
response = sg.send(message)
# Print status codes: 202 means the request was accepted for delivery
print(f"Status Code: {response.status_code}")
print(f"Body: {response.body}")
print(f"Headers: {response.headers}")
except Exception as e:
logger.error(f"An unrelated error occurred: {e}")