96

I am sending emails to users using Django through Google Apps.

When the user receives emails sent from the Django app, they are from: do_not_reply@domain.example

when looking at all emails in the inbox, people see the email's sender as: do_not_reply or do_not_reply@domain.example depending on the email client used

If I log into that "do_not_reply" account using the browser and Google Apps itself and then send an email to myself, the emails are from: Don't Reply <do_not_reply@domain.example>

As a result, the name displayed for the email's sender in the inbox is: Don't Reply

In Django, is there a way to attach a "name" to the email account being used to send emails?

I have reviewed Django's mail.py, but had no luck finding a solution http://code.djangoproject.com/browser/django/trunk/django/core/mail.py?rev=5548

Using:

  • Django 1.1
  • Python 2.6
  • Ubuntu 9.1
  • settings.EMAIL_HOST = smtp.gmail.com
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
rfadams
  • 1,898
  • 2
  • 19
  • 21

3 Answers3

174

You can actually use "Don't Reply <do_not_reply@domain.example>" as the email address you send from.

Try this in the shell of your Django project to test if it also works with gapps:

>>> from django.core.mail import send_mail
>>> send_mail('subject', 'message', "Don't Reply <do_not_reply@domain.example>", ['youremail@example.com'])
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Gregor Müllegger
  • 4,973
  • 1
  • 23
  • 22
  • 1
    Thanks for posting! Wish I had seen this yesterday b/c it would have saved me some time. I was just reading the documentation over at and finally read the "from_email" more closely and found the same answer as you. Was just coming here to post an answer when I saw yours. Anyway, thanks again! Glad to finally have this working – rfadams Jan 22 '10 at 20:48
  • 13
    You can also set DEFAULT_FROM_EMAIL on settings.py like this: `DEFAULT_FROM_EMAIL = 'Dont Reply '` – Marc Tudurí Apr 16 '15 at 13:18
  • Just make sure, you dont set the same email with name as auth_user , because it will fail while authentication – iamkhush Oct 01 '17 at 09:46
  • 1
    @iamkhush - What do you mean by `setting the same email with name as aut_user`. I'm getting an authentication error. I couldn't understand why? – Praful Bagai Sep 03 '19 at 04:36
  • If you want to have do_not_reply@domain.com as the display name, use `'"do_not_reply@domain.com" '` (note the `"`'s around the display name. Otherwise django will complain – DIJ Mar 02 '22 at 16:12
0

Apart from the send_mail method to send email, EmailMultiAlternatives can also be used to send email with HTML content with text content as an alternative.

try this in your project

from django.core.mail import EmailMultiAlternatives
text_content = "Hello World"
# set html_content
email = EmailMultiAlternatives('subject', text_content, 'Dont Reply <do_not_reply@domain.example>', ['youremail@example.com'])

email.attach_alternative(html_content, 'text/html')
email.send()

This will send mail to youremail@example.com with Don't Reply wil be displayed as name instead of email do_not_reply@domain.example.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Basant Kumar
  • 129
  • 1
  • 3
-5

I use this code to send through gmail smtp (using google apps). and sender names are OK

def send_mail_gapps(message, user, pwd, to):
    import smtplib
    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(user, pwd)
    mailServer.sendmail(user, to, message.as_string())
    mailServer.close()
jujule
  • 11,125
  • 3
  • 42
  • 63
  • This is actually the same code that Django's EmailMessage classes uses in the background. Regardless, I tried your method and the email still has `do_not_reply` as the sender's name. Thanks for trying though – rfadams Jan 21 '10 at 18:49
  • 3
    and what about settings.DEFAULT_EMAIL_FROM ? – jujule Jan 22 '10 at 10:53