无法使用 flask-mail 发送电子邮件
cannot send email with flask-mail
我正在尝试使用 Flask-Mail 发送电子邮件,
这是教程的link: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 终端中),但它不起作用,即无法回显某些内容。
如何解决这个问题?
邮件发送代码如下:
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 本身。
我正在尝试使用 Flask-Mail 发送电子邮件,
这是教程的link: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 终端中),但它不起作用,即无法回显某些内容。
如何解决这个问题?
邮件发送代码如下:
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 本身。