You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Flask-Security-Too配置AWS SES发送邮件?(ValueError报错)

问题:Flask-Security-Too切换AWS SES发送邮件时出现"No email extension configured"错误

我正在尝试将使用Flask-Security-Too做用户认证和邮件管理的Flask应用,从SMTP切换为AWS SES发送邮件。已经完成了以下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类实现
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}")

运行代码时触发如下错误:

ValueError: No email extension configured


解决方法

问题根源在于MailUtil父类的__init__方法会强制检查Flask-Mail扩展是否已配置,但我们自定义的SESMailUtil直接调用AWS SES API,不需要依赖Flask-Mail。

修改SESMailUtil类,跳过父类的初始化逻辑即可:

from app_utils import get_ses_client
from flask_security.mail_util import MailUtil

class SESMailUtil(MailUtil):
    def __init__(self, app=None):
        # 跳过父类初始化,避免检查Flask-Mail扩展
        self.app = 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"邮件发送成功!消息ID: {response['MessageId']}")
        except Exception as e:
            print(f"发送邮件出错: {e}")

另外,如果你的应用之前配置过Flask-Mail相关代码,现在可以直接移除,因为我们已经通过自定义工具类绕开了它。


内容的提问来源于stack exchange,提问作者Alphonsa John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 22:33:10