Django电子邮件后端(不断发送电子邮件不正确的“发件人”) [英] Django Email backend (keeps sending email from incorrect "sender")

查看:137
本文介绍了Django电子邮件后端(不断发送电子邮件不正确的“发件人”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有几个实例,我试图在Django视图中发送一封电子邮件。



我希望能够在视图中对电子邮件发件人进行硬编码。当我尝试这样做,它继续发送电子邮件从我的设置文件中指定的默认帐户。



这是一个例子:

 如果testform.is_valid() :
beta = testform.save()
subject =嗨Beta测试员
sender =correct@email.com

recipient = [testform.cleaned_data ['email']]

text = loader.get_template('registration / beta_email.txt')
html = loader.get_template('registration / beta_email.html')

site_name ='mysite'
d =上下文({'site_name':site_name})
text_content = text.render(d)
html_content = html.render(d)
#发送两个邮件版本,纯文本和html
msg = EmailMultiAlternatives(subject,text_content,sender,recipient)
msg.attach_alternative(html_content,text / html)
msg .send()

返回HttpResponseRedirect('/ splash /')

我以为我可以在这里明确指定发件人参数。而且,当我测试它,电子邮件是从我的设置文件中列出的地址发送,配置如下:

  EMAIL_USE_TLS = True 

EMAIL_HOST ='smtp.gmail.com'

EMAIL_HOST_USER='wrong@email.com'

EMAIL_HOST_PASSWORD ='私人'

DEFAULT_FROM_EMAIL='wrong@email.com'

只需要删除DEFAULT_FROM_EMAIL常量来使其工作?我尝试这样做,似乎工作,但我很困惑。在Django文档中,建议在视图中设置发件人应该覆盖DEFAULT。

解决方案

我终于弄清楚了这个问题。不幸的是,gmail在验证的smtp上重写了
信封。



如果您想了解,您必须使用第三方邮件服务器(不会像这样做),然后将邮件发送给Gmail用户。


I have several instances in my project where I attempt to send an email within a Django view.

I want to be able to hardcode the email sender within the view. When I try to do so, though, it continues to send the emails from the default account specified in my settings file.

Here is an example:

        if testform.is_valid():
            beta=testform.save()
            subject="Hi Beta Tester"  
            sender="correct@email.com"

            recipient=[testform.cleaned_data['email']]

            text=loader.get_template('registration/beta_email.txt')
            html=loader.get_template('registration/beta_email.html')

            site_name='mysite'
            d=Context({'site_name':site_name})
            text_content=text.render(d)
            html_content=html.render(d)
                #This sends two mail versions, plain text and html
            msg=EmailMultiAlternatives(subject, text_content, sender, recipient)
            msg.attach_alternative(html_content, "text/html")
            msg.send()

            return HttpResponseRedirect('/splash/')

I thought that I could send specify the sender argument explicitly here. And, yet, when I test it, the email is being sent from the address listed in my settings file, configured as the following:

       EMAIL_USE_TLS=True

       EMAIL_HOST='smtp.gmail.com'

       EMAIL_HOST_USER='wrong@email.com'

       EMAIL_HOST_PASSWORD='private'

       DEFAULT_FROM_EMAIL='wrong@email.com'

Do I just have to remove the DEFAULT_FROM_EMAIL constant to make it work? I tried doing so and it seems to be working but I'm confused. In the Django documentation, it suggests that setting sender in the view should override the DEFAULT.

解决方案

I've finally figured out the issue here. Unfortunately, gmail rewrites the from and the envelope on authenticated smtp.

If you want to get around that, you have to use a third party mail server (which doesn't act like such a prissy) and then send mail to gmail users.

这篇关于Django电子邮件后端(不断发送电子邮件不正确的“发件人”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆