2

I am trying to send Email with Flask-Mail,

This is the link of tutorial: https://www.twilio.com/blog/2018/03/send-email-programmatically-with-gmail-python-and-flask.html

EMAIL_USER and EMAIL_PASSWORD are environment variables, so I try to modify ~/.bashrc file(sudo vim ~/.bashrc) and add relevant information into them (in PyCharm Terminal), but it doesn't work, that is, it cannot echo something.

How to solve this problem?

enter image description here enter image description here

Here is the email sending code:

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)
HungryBird
  • 1,077
  • 1
  • 11
  • 28

1 Answers1

0

After adding the variable to the .bashrc file you may need to restart your terminal to see it echo out. Try closing a reopening your pycharm terminal or pycharm itself.

shmuels
  • 1,039
  • 1
  • 9
  • 22