1

我有一个 Python Flask 应用程序设置并在 CherryPy 上运行(托管在Digital Ocean 上,操作系统:Debian GNU/Linux 7.0)。我正在使用Flask Sendmail发送邮件,运行应用程序并尝试发送电子邮件,它没有显示任何错误并且可以正常执行。但是没有收到电子邮件(检查垃圾邮件和所有其他文件夹)。

有什么帮助吗?我在下面添加了代码。

Flask 应用程序的配置:

app.config.update(
    DEBUG=True,
    MAIL_DEBUG=True,
    MAIL_FAIL_SILENTLY=False,
    MAIL_SUPPRESS_SEND=False,
    DEFAULT_MAIL_SENDER='Tester',
    TESTING=False
)

邮件发送部分:

mail_handler = Mail()
mail_handler.init_app(app)

try:
    msg = Message("Hello World",
                  recipients='jane@doe.com')
    msg.html += '<b>HTML content for email</b>'

    if mail_handler!=None:
        mail_handler.send(msg)
        print "email sent"

    return {"status": "success", "message": "Please check your email"}

except Exception as e:
    return {"status": "failed", "message": "Failed"}
4

1 回答 1

1

最近我也用这个东西度过了整个晚上。我最终得到的工作邮件模块如下:

from flask_mail import Mail, Message

mail = None

def configure_mail(app):
    # EMAIL SETTINGS
    global mail
    app.config.update(
        MAIL_SERVER = 'smtp.gmail.com',
        MAIL_PORT = 465,
        MAIL_USE_SSL = True,
        MAIL_USERNAME = 'blabla@gmail.com',
        MAIL_PASSWORD = 'mega_password',
        DEFAULT_MAIL_SENDER = 'blabla@gmail.com',
        SECRET_KEY = 'abcdefd_thats_a_charming_secret_key',
    )
    mail=Mail(app)

def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender = sender, recipients = recipients)
    msg.body = text_body
    msg.html = html_body
    mail.send(msg)

然后我只是从适当的地方调用实现的方法:

from emails import send_email # 'emails' is a name of the module provided above
send_email('messageTopic', 'blabla@gmail.com', ['blabla@gmail.com'], 'composedMsg', None)

并且不要忘记在发送电子邮件之前调用配置代码。例如:

from emails import configure_mail # 'emails' is a name of the module provided above
app = Flask(__name__)
app.debug = True
configure_mail(app)

希望这可以帮助。

于 2014-01-16T10:19:02.457 回答