gpt4 book ai didi

python - 无法通过 flask 邮件发送电子邮件

转载 作者:行者123 更新时间:2023-12-03 16:58:06 25 4
gpt4 key购买 nike

我正在尝试通过Flask-Mail发送电子邮件,

这是教程的链接:https://www.twilio.com/blog/2018/03/send-email-programmatically-with-gmail-python-and-flask.html

EMAIL_USER和EMAIL_PASSWORD是环境变量,因此我尝试修改〜/ .bashrc文件(sudo vim〜/ .bashrc)并向其中添加相关信息(在PyCharm Terminal中),但是它不起作用,即无法回显的东西。

如何解决这个问题呢?

enter image description here
enter image description here

这是电子邮件发送代码:

from flask import Flask
from flask_mail import Mail, Message
import os

app = Flask(__name__)

mail_settings = {
"MAIL_SERVER": 'smtp.gmail.com',
"MAIL_PORT": 465,
"MAIL_USE_TLS": False,
"MAIL_USE_SSL": True,
"MAIL_USERNAME": os.environ['EMAIL_USER'],
"MAIL_PASSWORD": os.environ['EMAIL_PASSWORD']
}

app.config.update(mail_settings)
mail = Mail(app)

if __name__ == '__main__':
with app.app_context():
msg = Message(subject="Hello",
sender=app.config.get("MAIL_USERNAME"),
recipients=["<example@gmail.com>"], # replace with your email for testing
body="This is a test email I sent with Gmail and Python!")
mail.send(msg)

最佳答案

将变量添加到.bashrc文件后,您可能需要重新启动终端才能看到它echo。尝试关闭重新打开pycharm终端或pycharm本身。

关于python - 无法通过 flask 邮件发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50573718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com