Giving email account a name when sending emails wi

2019-01-31 10:20发布

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.com

when looking at all emails in the inbox, people see the email's sender as :
do_not_reply or do_not_reply@domain.com 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:
Dont Reply<do_not_reply@domain.com>

As a result, the name displayed for the email's sender in the inbox is:
Dont 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'

Thanks

3条回答
Animai°情兽
2楼-- · 2019-01-31 10:54

You can actually use "Dont Reply <do_not_reply@domain.com>" 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', 'Dont Reply <do_not_reply@domain.com>', ['youremail@example.com'])
查看更多
祖国的老花朵
3楼-- · 2019-01-31 11:06

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()
查看更多
虎瘦雄心在
4楼-- · 2019-01-31 11:16

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_replay@domain.com>', ['youremail@example.com'])

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

This will send mail to youremail@example.com with Dont Reply wil be dispalyed as name instead of email 'do_not_replay@domain.com'.

查看更多
登录 后发表回答