开发者

How to change the From address in Django Email?

开发者 https://www.devze.com 2023-04-08 15:14 出处:网络
As noted in the docs, the SERVER_EMAIL setting is supposed to change the \'From\' address in crash emails sent to ADMINS from the Django (1.3.1) server.

As noted in the docs, the SERVER_EMAIL setting is supposed to change the 'From' address in crash emails sent to ADMINS from the Django (1.3.1) server.

But it's not.

Does this work for you in 1.3.1? (Or any Django version)

Django insists on just using my EMAIL_HOST_USER - my email login/actual address - as the from address. I'm using Gmail as an 开发者_运维技巧SMTP server, so I wonder if that could have something to do with it. Does Gmail block this sort of thing? I swear I've gotten this to work before.

It's a little annoying because we have multiple projects that all seem to be emailing from the same address, and we have to dig through the traceback to see which project it is.


The problem is Gmail. All the way down to the smtplib library, the correct 'from' address is specified, and this library sends the right address to Gmail.

This Gmail Support page implies (especially near the bottom under "Note for POP/IMAP" users) that you need to add an address as an 'additional email address' under Gmail's settings to be able to send mail from it over Gmail's SMTP servers.

This of course requires verification; since my 'from' address doesn't have an inbox (it's fake!) its not currently possible for me. But at least it's not a Django bug! : )

(Note: this is a pretty obvious way for Gmail to stop you from spamming, I'm sure that's why they do it.)


From what I can see in the code (1.3.1) the stack trace emails are sent using the mail_admins method with SERVER_EMAIL as the specified from address:

mail = EmailMultiAlternatives(u'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
                message, settings.SERVER_EMAIL, [a[1] for a in settings.ADMINS],
                connection=connection)

Which is defined as:

class EmailMultiAlternatives(EmailMessage):
   [...]
   def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
            connection=None, attachments=None, headers=None, alternatives=None,
            cc=None):

I would suggest putting trace output in EmailMultiAlternatives to verify that the proper email address is being used.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号