settings.EMAIL_HOST_USER problem

293 views
Skip to first unread message

madhav

unread,
Jan 28, 2009, 12:20:07 PM1/28/09
to Django users
I need some help regarding django emailing. I have set the
settings.EMAIL_HOST_USER to 'do-not...@mysite.com' and whenever I
am trying to send an email in any context, its taking the 'do-not-
re...@mysite.com' as from address and sending it, eventhough I supply
a different from-address while sending that specific email. How do I
fix this?

Karen Tracey

unread,
Jan 28, 2009, 1:07:47 PM1/28/09
to django...@googlegroups.com

Are you using authentication when connecting to the mail server?  This recent thread:

http://groups.google.com/group/django-users/browse_thread/thread/138d1eea6d1125ec?hl=en#

notes similar behavior, when using gmail (which requires authentication, I believe) as the mail server.  It's intuitive behavior -- if you have authenticated to the mail server using one identify, it seems logical that mail sent on that authenticated connection would be "From:" that identity, not some other identity.  For all I know such behavior is mandated by one of the mail-related RFCs, but this is not an area I am familiar with, so I really have no idea. 

All I can say is from a quick look at the django.core.mail code, there doesn't appear to be any special-case code that discards the specified "From" in favor of EMAIL_HOST_USER, so I don't believe it is any code within Django that is causing this.

Karen

madhav

unread,
Jan 28, 2009, 1:18:01 PM1/28/09
to Django users
Thankyou Tracey,
You are right about settings.EMAIL_HOST_USER not getting overriden
from django.core.email. Even I checked it.
But the current behaviour is a bit confusing. Because, I can have a
default email id
for the entire application(like sys...@mysite.com or do-not-
re...@mysite.com) which will
handle all the normal email communications. And as well I want users
of my site
to send notifications to one-another or a similar case where I want to
send
a mail from some other email id other than EMAIL_HOST_USER. This case
is so common.
And i really doubt why django wont support that. I think it might be a
case related to
some overriding and not a limitation in django. What do you say?

On Jan 28, 11:07 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> On Wed, Jan 28, 2009 at 12:20 PM, madhav <madhav....@gmail.com> wrote:
>
> > I need some help regarding django emailing. I have set the
> > settings.EMAIL_HOST_USER to 'do-not-re...@mysite.com' and whenever I
> > am trying to send an email in any context, its taking the 'do-not-
> > re...@mysite.com' as from address and sending it, eventhough I supply
> > a different from-address while sending that specific email. How do I
> > fix this?
>
> Are you using authentication when connecting to the mail server?  This
> recent thread:
>
> http://groups.google.com/group/django-users/browse_thread/thread/138d...

madhav

unread,
Jan 28, 2009, 1:39:14 PM1/28/09
to Django users
To add to my previous reply....
Even EmailMessage class has got from_email attribute which means it
can have dynamic EmailMessage.
If it has to only pick settings.EMAIL_HOST_USER, why would it require
a atttribute called from_email?

Karen Tracey

unread,
Jan 28, 2009, 1:41:36 PM1/28/09
to django...@googlegroups.com
On Wed, Jan 28, 2009 at 1:18 PM, madhav <madha...@gmail.com> wrote:

Thankyou Tracey,
You are right about settings.EMAIL_HOST_USER not getting overriden
from django.core.email. Even I checked it.
But the current behaviour is a bit confusing. Because, I can have a
default email id
for the entire application(like sys...@mysite.com or do-not-
re...@mysite.com) which will
handle all the normal email communications. And as well I want users
of my site
to send notifications to one-another or a similar case where I want to
send
a mail from some other email id other than EMAIL_HOST_USER. This case
is so common.
And i really doubt why django wont support that. I think it might be a
case related to
some overriding and not a limitation in django. What do you say?

From http://docs.djangoproject.com/en/dev/topics/email/

"Although Python makes sending e-mail relatively easy via the smtplib library, Django provides a couple of light wrappers over it, to make sending e-mail extra quick."

If the Django-provided routines don't meet your needs, you may need to use the smtplib library directly. 

Karen

Karen Tracey

unread,
Jan 28, 2009, 2:08:51 PM1/28/09
to django...@googlegroups.com
On Wed, Jan 28, 2009 at 1:39 PM, madhav <madha...@gmail.com> wrote:

To add to my previous reply....
Even EmailMessage class has got from_email attribute which means it
can have dynamic EmailMessage.
If it has to only pick settings.EMAIL_HOST_USER, why would it require
a atttribute called from_email?

EMAIL_HOST_USER (and password) are used to login to the SMTP server:

http://docs.python.org/library/smtplib.html#smtplib.SMTP.login

This step is optional -- not all mail servers require login.

The From address you specify when calling one of the Django routines to send mail is passed along as the from address when sending the mail:

http://docs.python.org/library/smtplib.html#smtplib.SMTP.sendmail

A "from" address is not optional when sending mail, so it has to be specified. 

The question then arises, what does the SMTP server do when these two values do not match?  A user has authenticated using one identity but then sends mail claiming to be from some other identity.  Apparently some (at least gmail, I don't know how others behave, and as I said earlier this may be required behavior) just ignore the value set for "from" and use the authenticated identity as "from". 

If you want to send mail that appears to be from your users, I think this will be hard to do if you are using an SMTP server that requires login.  You will need to ensure all your users have logins on the SMTP server you are using, and you will need to use the Python smtplib routines directly to create the connection, login using the user's id and password, and then send the mail.  (Django has only one setting for the SMTP login/pw -- EMAIL_HOST_USER and EMAIL_HOST_PASSWORD -- to use when connecting to the mail server, and I don't expect extending that to support what you are talking about would be high on the list of things to add.)

If you happen to be using an SMTP server that does not actually require login, though, getting what you are looking for may be as simple as removing the EMAIL_HOST_USER and EMAIL_HOST_PASSWORD settings from your configuration.  Then the Django mail utility routines will not login to the server, and there will be no conflict between the logged-in identity and the From identity specified in the mail.

Karen

madhav

unread,
Jan 28, 2009, 3:21:33 PM1/28/09
to Django users
Awesome Tracey.

Almost all of my doubts regarding django emailing are gone. Except
One. Assuming I haven't set anything in the settings regarding
emailing. So NO EMAIL_HOST_USER, nothing else... I try sending the
email from the django shell(python manage.py shell) to
someva...@xyz.com, using some arbitrary from_email id and not
supplying auth_user and auth_password in the send_email call? Where
will the email be? This context is assuming local sendmail is up and
running.

On Jan 29, 12:08 am, Karen Tracey <kmtra...@gmail.com> wrote:

Karen Tracey

unread,
Jan 28, 2009, 5:22:52 PM1/28/09
to django...@googlegroups.com
On Wed, Jan 28, 2009 at 3:21 PM, madhav <madha...@gmail.com> wrote:

Awesome Tracey.

Almost all of my doubts regarding django emailing are gone. Except
One. Assuming I haven't set anything in the settings regarding
emailing. So NO EMAIL_HOST_USER, nothing else... I try sending the
email from the django shell(python manage.py shell) to
someva...@xyz.com, using some arbitrary from_email id and not
supplying auth_user and auth_password in the send_email call? Where
will the email be? This context is assuming local sendmail is up and
running.

I don't know, what happens when you try?  I've never run a local sendmail, just used mail servers provided by ISPs, etc. therefore I don't know what-all you can configure when running your own nor how you have yours configured.

Karen
Reply all
Reply to author
Forward
0 new messages