#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Help with Flask-Mail using Heroku + Mailgun


    Hi,

    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 on Stack Overflow called 'Flask on Heroku with MailGun config issues' (I tried to hyperlink but this newly registered account won't allow me to). However, I can see no resolution in the question on SO, 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):

    Code:
    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?

    Any help would be appreciated, thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by FlaskGuy
    Code:
    [...]
    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)
    [...]
    I would first verify that these environment variables are correct by printing them out.

    Have you verified that you can send mail from the command line of the server you're running on?

IMN logo majestic logo threadwatch logo seochat tools logo