I have been trying to make my Flask app send emails with my Gmail account.
I have tried the following configurations in my .env File:
MAIL_SERVER=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=something@gmail.com
MAIL_PASSWORD=special_gmail_password
MAIL_USE_TLS=False
MAIL_USE_SSL=True
and
MAIL_SERVER=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=something@gmail.com
MAIL_PASSWORD=special_gmail_password
MAIL_USE_TLS=True
MAIL_USE_SSL=False
and use the following to set my configurations:
app.config['MAIL_SERVER']= os.environ.get('MAIL_SERVER')
app.config['MAIL_PORT'] = os.environ.get('MAIL_PORT')
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD')
app.config['MAIL_USE_TLS'] = os.environ.get('MAIL_USE_TLS')
app.config['MAIL_USE_SSL'] = os.environ.get('MAIL_USE_SSL')
The mail code:
def send_email(subject, sender, recipients, name, link, body):
msg = Message(subject=subject, sender=sender, recipients=recipients)
msg.html = render_template("email.html", name=name, link=link, content=body, user=current_user, recipient=recipients[0])
# Send the email
mail.send(msg)
mail = Mail(app)
The first configuration gives:
raise SMTPNotSupportedError(
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
The second configuration gives:
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:997)
I know that PythonAnywhere recommends the second configuration on https://help.pythonanywhere.com/pages/SMTPForFreeUsers, but I can't get this one to work as well. How can I send my email in Pythonanywhere?