【问题标题】:Giving email account a name when sending emails with Django through Google Apps通过 Google Apps 使用 Django 发送电子邮件时为电子邮件帐户命名
【发布时间】:2011-01-07 20:40:13
【问题描述】:

我正在通过 Google Apps 向使用 Django 的用户发送电子邮件。

当用户收到从 Django 应用发送的电子邮件时,它们来自:
do_not_reply@domain.com

在查看收件箱中的所有电子邮件时,人们会将电子邮件的发件人视为:
do_not_replydo_not_reply@domain.com,具体取决于所使用的电子邮件客户端

如果我使用浏览器和 Google Apps 本身登录到那个“do_not_reply”帐户,然后给自己发送一封电子邮件,这些电子邮件来自:
Dont Reply<do_not_reply@domain.com>

因此,收件箱中显示的电子邮件发件人姓名为:
Dont Reply

在 Django 中,有没有办法将“名称”附加到用于发送电子邮件的电子邮件帐户?

我查看了 Django 的 mail.py,但没有找到解决方案
http://code.djangoproject.com/browser/django/trunk/django/core/mail.py?rev=5548

使用:
Django 1.1
Python 2.6
Ubuntu 9.1
settings.EMAIL_HOST = 'smtp.gmail.com'

谢谢

【问题讨论】:

    标签: django email smtp


    【解决方案1】:

    您实际上可以使用"Dont Reply <do_not_reply@domain.com>" 作为您发送的电子邮件地址。

    在你的 django 项目的 shell 中试试这个,以测试它是否也适用于 gapps:

    >>> from django.core.mail import send_mail
    >>> send_mail('subject', 'message', 'Dont Reply <do_not_reply@domain.com>', ['youremail@example.com'])
    

    【讨论】:

    • 感谢发帖!希望我昨天看到这个 b/c 它会节省我一些时间。我刚刚在 docs.djangoproject.com/en/dev/topics/email/…> 上阅读了文档,最后更仔细地阅读了“from_email”并找到了与您相同的答案。当我看到你的时候,我只是来这里发布答案。无论如何,再次感谢!很高兴终于有这个工作
    • 你也可以在 settings.py 上设置 DEFAULT_FROM_EMAIL 像这样:DEFAULT_FROM_EMAIL = 'Dont Reply &lt;do_not_reply@domain.com&gt;'
    • 请确保您没有设置与 auth_user 名称相同的电子邮件,因为它会在身份验证时失败
    • @iamkhush - setting the same email with name as aut_user 是什么意思。我收到身份验证错误。我不明白为什么?
    【解决方案2】:

    除了 send_mail 方法发送电子邮件之外,EmailMultiAlternatives 还可以用于发送带有 HTML 内容的电子邮件,作为替代方式。

    在你的项目中试试这个

    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()
    

    这将向 yourremail@example.com 发送邮件,其中不回复将显示为名称,而不是电子邮件“do_not_replay@domain.com”。

    【讨论】:

      【解决方案3】:

      我使用此代码通过 gmail smtp 发送(使用谷歌应用程序)。和发件人姓名都可以

      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()
      

      【讨论】:

      • 这实际上与 Django 的 EmailMessage 类在后台使用的代码相同。无论如何,我试过你的方法,电子邮件仍然有do_not_reply作为发件人的名字。感谢您尝试
      • 那么 settings.DEFAULT_EMAIL_FROM 呢?
      猜你喜欢
      • 2010-10-19
      • 2021-05-06
      • 2011-05-21
      • 1970-01-01
      • 2013-05-28
      • 2020-12-15
      • 1970-01-01
      • 2022-01-13
      • 2013-08-15
      相关资源
      最近更新 更多