I have two versions of codes for sending emails
Version 1 (W/O external config file)
# app.py
from flask import Flask
from flask_mail import Mail, Message
mail = Mail()
app =Flask(__name__)
app.config['MAIL_SERVER']='smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = 'test@gmail.com'
app.config['MAIL_PASSWORD'] = 'test'
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
print(app.config)
mail.init_app(app)
@app.route("/")
def index():
msg = Message('Hi', sender = 'test@gmail.com', recipients = ['test@gmail.com'])
msg.body = "Hello"
mail.send(msg)
return "Sent"
if __name__ == '__main__':
app.run(debug = True)
This version sends emails no problems
Version 2 (Config using from_object())
# app.py
from flask import Flask
from flask_mail import Mail, Message
mail = Mail()
app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')
# mail = Mail(app)
print(app.config)
mail.init_app(app)
@app.route("/")
def index():
print('Sending email')
msg = Message('Tutorial point3', sender = 'test@gmail.com', recipients = ['test@gmail.com'])
msg.body = "Hello Flask message sent from Flask-Mail "
mail.send(msg)
return "Sent"
if __name__ == '__main__':
app.run()
Below is configuration file
# config.py
class BaseConfig:
"""Base configuration"""
DEBUG = False
TESTING = False
SQLALCHEMY_TRACK_MODIFICATIONS = False
SECRET_KEY = 'my_precious'
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 465
MAIL_USE_SSL = True
MAIL_USE_TLS = False
MAIL_USERNAME = 'test@google.com'
MAIL_PASSWORD = 'test'
class DevelopmentConfig(BaseConfig):
"""Development configuration"""
DEBUG = True
Somehow version 2 does not work and give me this error
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials x4sm75923481pfk.51 - gsmtp')
The error message says my credential is wrong but I think that it not true because in version 1 I use exact same username and password and it worked fine.
To find out if my configuration from config.py is loaded, I did print(app.config)
(although I am not sure if it was a good way) and it looks like my configuration is correctly loaded to my flask app.
<Config {'DEBUG': True, 'TESTING': False, 'PROPAGATE_EXCEPTIONS': None, 'PRESERVE_CONTEXT_ON_EXCEPTION': None, 'SECRET_KEY': 'my_precious', 'PERMANENT_SESSION_LIFETIME': datetime.timedelta(31), 'USE_X_SENDFILE': False, 'LOGGER_NAME': '__main__', 'LOGGER_HANDLER_POLICY': 'always', 'SERVER_NAME': None, 'APPLICATION_ROOT': None, 'SESSION_COOKIE_NAME': 'session', 'SESSION_COOKIE_DOMAIN': None, 'SESSION_COOKIE_PATH': None, 'SESSION_COOKIE_HTTPONLY': True, 'SESSION_COOKIE_SECURE': False, 'SESSION_REFRESH_EACH_REQUEST': True, 'MAX_CONTENT_LENGTH': None, 'SEND_FILE_MAX_AGE_DEFAULT': datetime.timedelta(0, 43200), 'TRAP_BAD_REQUEST_ERRORS': False, 'TRAP_HTTP_EXCEPTIONS': False, 'EXPLAIN_TEMPLATE_LOADING': False, 'PREFERRED_URL_SCHEME': 'http', 'JSON_AS_ASCII': True, 'JSON_SORT_KEYS': True, 'JSONIFY_PRETTYPRINT_REGULAR': True, 'JSONIFY_MIMETYPE': 'application/json', 'TEMPLATES_AUTO_RELOAD': None, 'MAIL_PASSWORD': 'test', 'MAIL_PORT': 465, 'MAIL_SERVER': 'smtp.gmail.com', 'MAIL_USERNAME': 'test@google.com', 'MAIL_USE_SSL': True, 'MAIL_USE_TLS': False, 'SQLALCHEMY_TRACK_MODIFICATIONS': False}>
Thank you for your help in advance!
Update1: Modify the title for the better understanding. Also when I tested both versions, I used actual gmail address and password, but in the code snippet I replaced all credentials to "test" to protect my account