我有一个Python Flask应用程序设置并在CherryPy上运行(托管在Digital Ocean,操作系统:Debian GNU / Linux 7.0)。我正在使用Flask Sendmail发送邮件,运行应用程序并尝试发送电子邮件,它不显示任何错误并正确执行。但是没有收到电子邮件(已检查垃圾邮件和其他所有文件夹)。
有任何帮助吗?我已添加以下代码。
Flask app的配置:
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"}
答案 0 :(得分:0)
最近我整个晚上都用这个东西。我最终得到的工作邮件模块如下:
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)
不要忘记在发送电子邮件之前调用配置代码。 E.g:
from emails import configure_mail # 'emails' is a name of the module provided above
app = Flask(__name__)
app.debug = True
configure_mail(app)
希望这有帮助。