简体   繁体   中英

Customize a sender in Flask-Mail?

Flask-Mail allows you to customize who sends the email, but when I try to do so, it just uses my login email that I used to authenticate to the server.

Example.

app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_SSL'] = True
app.config['MAIL_USERNAME'] = 't********3@gmail.com'
app.config['MAIL_PASSWORD'] = '********'
app.config['MAIL_DEFAULT_SENDER'] = None
app.config['DEBUG'] = True

mail = Mail(app)


@app.route('/')
def index():
    msg = Message("Hello",
                sender='johndoe@gmail.com',
              recipients=["t*********3@gmail.com"])
    mail.send(msg)
    return 'message sent!'

Even though i put a sender, it still uses the ['MAIL_USERNAME'] email. Any input would be much appreciated. Thanks.

I believe this is something Google's SMTP does for you. I was able to reproduce the issue and got the same results as you: sender name was kept, but sender email was set to the google account email, no matter what the sender or default sender is.

However using a different SMTP (sendgrid) worked just fine and I can see my sender email displaying correctly.

I did not test it, but you can probably work around the issue with gmail by configuring 'Send As' in account settings and adding the email you want to be the sender here is how to do it . Or just use a different smtp.

Update: I did test it after all and it works if you add your new sender email in gmail account settings.

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