I am attempting to switch from SMTP to AWS SES for sending emails in my Flask application, which uses Flask-Security-Too for user authentication and email management.
I have implemented the following configuration changes to integrate AWS SES:
app.config['SECURITY_MAIL_UTIL'] = SESMailUtil(app)
app.config['SECURITY_EMAIL_SENDER'] = read_config("EMAIL", 'notifications').get("sender_name")
app.config['SECURITY_EMAIL_SUBJECT_REGISTER'] = 'Welcome! Confirm your email'
SESMailUtil class:
from app_utils import get_ses_client
from flask_security.mail_util import MailUtil
class SESMailUtil(MailUtil):
def __init__(self, app=None):
super().__init__(app)
def send_mail(self, template, subject, recipient, sender, body, html, user, **kwargs):
ses_client = get_ses_client()
try:
response = ses_client.send_email(
Source=sender,
Destination={
'ToAddresses': [recipient]
},
Message={
'Subject': {'Data': subject},
'Body': {
'Text': {'Data': body},
'Html': {'Data': html}
}
}
)
print(f"Email sent! Message ID: {response['MessageId']}")
except Exception as e:
print(f"Error sending email: {e}")
However, when I run the code, I encounter the following error: raise ValueError("No email extension configured") ValueError: No email extension configured
The correct config variable is SECURITY_MAIL_UTIL_CLS - pass in the class name and it will be instantiated as part of flask-security init_app(). You can also pass the class name in as part of the constructor.