简体   繁体   中英

Heroku, Flask, Flask-Mail, Gmail

I'm attempting to set up a heroku app using Flask and I'm having trouble with Flask-Mail.

I can run my script from bash and it works every time, but when I push it to Heroku it will work for a little while then stop. Below are the relevant parts of the script:

from flask.ext.mail import Mail
from flask.ext.mail import Message
app.config.update(dict(
    DEBUG = True,
    MAIL_SERVER = 'smtp.gmail.com',
    MAIL_PORT = 465,
    MAIL_USE_TLS = False,
    MAIL_USE_SSL = True,
    MAIL_USERNAME = 'xxx@xxx.com',
    MAIL_PASSWORD = 'xxx',
))
mail = Mail(app)
msg = Message(
          'Hello',
       sender = 'xxx@xxx.com',
       recipients = ['xxx@xxx.com'])
msg.body = "This is the email body"
msg.html = '<b>HTML</b> body 1234'
with app.open_resource("image.jpg") as fp:
    msg.attach("image.jpg", "image/jpg", fp.read())
@app.route('/test1')
def test1():
    with app.app_context():
        mail.send(msg)
    return "Sent"

I can go to that route and will send it, but if I try again a few minutes later the page will load just fine, but no email will be sent. No errors show in the log.

Does anyone have any ideas on how to solve this issue?

The following config is what I used for heroku.

MAIL_SERVER = 'smtp.gmail.com',
MAIL_PORT = 587,
MAIL_USE_TLS = True,
MAIL_USE_SSL = False,
MAIL_USERNAME = 'my_username@gmail.com',
MAIL_PASSWORD = 'my_password',

I think you need to use a Heroku addon (which, by the way, can be free but will require a credit card).

https://devcenter.heroku.com/articles/smtp

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