简体   繁体   中英

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='testing@test.com',
                  recipients=[UserEmail])

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

@app.route('/EmailTest/')
def EmailTestPage():
    EmailFunction('developer@test.com')
    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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM