How to change reply-to and return-path header with

2020-02-11 12:59发布

问题:

I am using gmail smtp in my django website. I have a contact form where user put email and message then I send a mail to the admin with :

   email = EmailMessage('email subject', 'email message', settings.EMAIL_HOST_USER,
                        ['admin.mysite@gmail.com'],
                          headers = {'Reply-To': 'user@foo.bar',
                               'Sender': 'user@foo.bar','from': 'user@foo.bar','Return-Path': 'user@foo.bar'})
   email.send(fail_silently=False)

email is sent/received correctly but when admin client select reply in gmail, it always reply to the settings.EMAIL_HOST_USER and not the user address. On the email original header the From and Return-path are set with the setting.EMAIL_HOST_USER

回答1:

Google violates the RFPs defining the expected operation of an SMTP server, rewriting the headers. This may be the root cause of your problem: http://lee-phillips.org/gmailRewriting/



回答2:

If you use smtp.gmail.com to send a message, and the sender's email address is not yourgoogleemailname@gmail.com, then Gmail will rewrite the headers and set the from address to yourgoogleemailname@gmail.com. See http://lifehacker.com/111166/how-to-use-gmail-as-your-smtp-server for more info, and for a possible solution.



回答3:

I have the same problem for a year, due to the Gmail restriction.

But today I retry and it works, it seems that Google changed its policy.

# settings.py
....
DEFAULT_FROM_EMAIL = 'Infoma Admin<no-reply@infoma.co.nz>'

Django 2.1.7