-1

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?

Max de Boer
  • 262
  • 3
  • 10
  • Make sure that you correctly read from the environment, i.e., that the config based on the `os.environ.get` in your web app has what you expect it to have. – caseneuve Aug 25 '23 at 08:07

0 Answers0