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

2020-02-11 12:53发布

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

3条回答
等我变得足够好
2楼-- · 2020-02-11 13:19

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楼-- · 2020-02-11 13:20

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>'

enter image description here

Django 2.1.7

查看更多
手持菜刀,她持情操
4楼-- · 2020-02-11 13:34

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/

查看更多
登录 后发表回答