Piers Lillystone
Piers Lillystone

Reputation: 257

Emails not sending when using Flask-Mail with Heroku and Mailgun

I have been writing a simple app to test how to send emails via an SMTP method (needs to be SMTP to be portable to different SMTP services) using Flask-Mail. For this I am trying to use Mailgun through Heroku, but after much trial, error and research I still cannot seem to get emails to send.

My question is on a similar vein to this question, Flask on Heroku with MailGun config issues, however I can see no resolution in this question other than to use Mailgun's API, which isn't feasible for the project I am working on.

Currently I have the flask/flask-mail code set up as follows (stripped down of course):

from flask import Flask
from flask.ext.mail import Mail
from flask.ext.mail import Message

app = Flask(__name__)
mail = Mail(app)

app.config.setdefault('SMTP_SERVER', environ.get('MAILGUN_SMTP_SERVER'))
app.config.setdefault('SMTP_LOGIN', environ.get('MAILGUN_SMTP_LOGIN'))
app.config.setdefault('SMTP_PASSWORD', environ.get('MAILGUN_SMTP_PASSWORD'))
app.config.setdefault('MAIL_SERVER', environ.get('MAILGUN_SMTP_SERVER'))
app.config.setdefault('MAIL_USERNAME', environ.get('MAILGUN_SMTP_LOGIN'))
app.config.setdefault('MAIL_PASSWORD', environ.get('MAILGUN_SMTP_PASSWORD'))
app.config.setdefault('MAIL_USE_TLS', True)

def EmailFunction(UserEmail):
    msg = Message("Hello",
                  sender='[email protected]',
                  recipients=[UserEmail])

    msg.html = "<b>testing</b>"
    mail.send(msg)
    return msg.html

@app.route('/EmailTest/')
def EmailTestPage():
    EmailFunction('[email protected]')
    return 'Email Sent'

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=True)

Am I missing something? And is there a way to test what is going wrong as the code passes and 'Email Sent' is returned, but no email is sent/received seemingly.

Thanks for any help you can provide!

Upvotes: 1

Views: 2782

Answers (1)

CraigKerstiens
CraigKerstiens

Reputation: 5954

It looks like you're not reading in the SMTP port. By default Flask mail likely tries to use 25 whereas mailgun uses 587.

Upvotes: 1

Related Questions