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********[email protected]'
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='[email protected]',
recipients=["t*********[email protected]"])
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With