不能在Django或Wagtail的send_mail中设置发件人电子邮件地址。

huangapple go评论47阅读模式
英文:

Cannot set a sender email address in Django or Wagtail send_mail

问题

The task: 有一个网站提供新闻简报。每当有订阅者订阅时,我会向一个电子邮件地址发送新闻简报订阅通知。为此,我使用来自wagtail.admin.mail的send_mail - 我认为我使用send_mail的方式并不重要。

The problem: 我提供了一个from_email,但出于某种原因,我收到的电子邮件包含EMAIL_HOST_USER作为发件人 - 这是用于发送电子邮件的谷歌邮件帐户的电子邮件。我还尝试过 from django.core.mail import send_mail,但情况一样。是否有任何线索,我如何能够使它工作?也许他们阻止使用自定义电子邮件地址作为发件人显示?

Details: 电子邮件后端使用smtp.gmail.com和django.core.mail.backends.smtp.EmailBackend。

英文:

The task: There is a website, offering a newsletter. I´m sending a newsletter subscription notification to an email address everytime a subscriber subscribes. For this I use send_mail from wagtail.admin.mail – I think it´s not relevant in which way I´m using send_mail.

The problem: I provide a from_email, but for some reason the email I receive contains the EMAIL_HOST_USER as sender – which is the email of the google mail account used for sending emails. I also tried from django.core.mail import send_mail but it´s the same. Is there any clue, how I can manage to make it work? Maybe they block to use custom email address showed as a sender?

Details: smtp.gmail.com and django.core.mail.backends.smtp.EmailBackend is used for the email backend.

答案1

得分: 1

查看django.core.mail.message.EmailMessage

您可以在该类中显式设置所有消息属性,包括连接和from_email

对于连接,因为我在站点上使用不同的凭据,我有一个模型来存储连接设置,并为手头的工作提取相关设置。

例如,在我的Contact表单类中,我有一个用于邮件设置的属性:

@property
def mail_settings(self):
    email_settings = EmailSettings.objects.filter(type='contact').first()
    if email_settings:
        use_ssl = getattr(email_settings, 'use_ssl')
        return {
            'host': getattr(email_settings, 'host'),
            'port': getattr(email_settings, 'port'),
            'username': getattr(email_settings, 'username'),
            'password': getattr(email_settings, 'password'),
            'ssl_setting': use_ssl,
            'tls_setting': not use_ssl
        }

然后在send_mail()方法中,我使用django.core.mail.get_connection获取连接

connection = mail.get_connection(**self.mail_settings)

您可以将此连接传递给EmailMessage构造函数。

英文:

Have a look at django.core.mail.message.EmailMessage

You can explicitly set all the message properties in that class, including the connection and the from_email

For connection, because I use different credentials across my site, I have a model to store connection settings and pull the relevant one for the job in hand.

For instance, in my Contact form class, I have a property for the mail settings:

@property
def mail_settings(self):
    email_settings=EmailSettings.objects.filter(type='contact').first()
    if email_settings:
        use_ssl = getattr(email_settings, 'use_ssl')
        return {
            'host' : getattr(email_settings, 'host'),
            'port' : getattr(email_settings, 'port'),
            'username' : getattr(email_settings, 'username'),
            'password' : getattr(email_settings, 'password'),
            'ssl_setting' : use_ssl,          
            'tls_setting' : not use_ssl
        }

then in the send_mail() method, I get the connection with django.core.mail.get_connection

    connection = mail.get_connection(**self.mail_settings)

You can pass this connection into the EmailMessage constructor.

答案2

得分: 0

我再次检查了。Gmail SMTP 服务器确实不允许使用自定义的 from_email 地址。但发送者可以在帐户设置中进行设置。如上所讨论。

来源:
更改 send_mail 上的发件地址

英文:

I checked again. The Gmail SMTP server indeed doesn´t allow to use a custom from_email. But the sender can be set in the account settings. As discussed above.

Source:
changing from address on send_mail

huangapple
  • 本文由 发表于 2023年5月21日 19:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定