错误:SMTPRecipientsRefused 553, '5.7.1 #while 在 django 中处理联系表单
问题描述
我正在尝试在 django 1.3、python 2.6 中创建联系表单.
出现以下错误的原因是什么?
错误:
SMTPRecipientsRefused at/contact/{'test@test.megiteam.pl': (553, '5.7.1 <randomacc@hotmail.com>: 发件人地址拒绝:不属于用户 test@test.megiteam.pl')}
我的设置.py:
EMAIL_HOST = 'test.megiteam.pl'EMAIL_HOST_USER = 'test@test.megiteam.pl'EMAIL_HOST_PASSWORD = '###'DEFAULT_FROM_EMAIL = 'test@test.megiteam.pl'SERVER_EMAIL = 'test@test.megiteam.pl'EMAIL_USE_TLS = 真
如果任何其他人关注 djangobook,这就是导致它的部分:
send_mail(request.POST['主题'],request.POST['message'],request.POST.get('email', 'noreply@example.com'), #摆脱'email'['siteowner@example.com'],
解决方案 解释在错误信息中.由于您从联系表单中获取的发件人地址 randomcc@hotmail.com
,您的电子邮件主机拒绝了该电子邮件.
相反,您应该使用自己的电子邮件地址作为发件人地址.您可以使用 reply_to
选项将回复发送给您的用户.
email = EmailMessage('主题','身体在这里','test@test.megiteam.pl',['to@example.com',],reply_to='randomcc@hotmail.com',)电子邮件.send()
在 Django 1.7 及更早版本上,没有 reply_to
参数,但您可以手动设置 Reply-To
标头:
email = EmailMessage('主题','身体在这里','test@test.megiteam.pl',['to@example.com',],headers = {'Reply-To': 'randomacc@hotmail.com'},)电子邮件.send()
在评论中,您询问了如何在邮件正文中包含发件人的地址.message
和 from_email
只是字符串,因此您可以在发送电子邮件之前随意组合它们.
请注意,您不应从 clean_data 中获取 from_email
参数.你知道 from_address
应该是 test@test.megiteam.pl
,所以使用它,或者从你的设置中导入 DEFAULT_FROM_EMAIL
.p>
请注意,如果您像上面的示例一样使用 EmailMessage
创建消息,并将回复设置为标题,那么当您点击回复按钮时,您的电子邮件客户端应该会做正确的事情.下面的示例使用 send_mail
使其与 您链接到的代码相似..p>
从 django.conf 导入设置...如果 form.is_valid():cd = form.cleaned_data消息 = cd['消息']# 从表单的清理数据构造消息体正文 = """发件人:%s消息:%s""" % (cd['email'], cd['message'])发送邮件(cd['主题'],身体,settings.DEFAULT_FROM_EMAIL, # 使用您的电子邮件地址,而不是表单中的那个['test@test.megiteam.pl'],)
im trying to make a contact form in django 1.3, python 2.6.
Whats the reason of following error?
error:
SMTPRecipientsRefused at /contact/
{'test@test.megiteam.pl': (553, '5.7.1 <randomacc@hotmail.com>: Sender address
rejected: not owned by user test@test.megiteam.pl')}
my settings.py:
EMAIL_HOST = 'test.megiteam.pl'
EMAIL_HOST_USER = 'test@test.megiteam.pl'
EMAIL_HOST_PASSWORD = '###'
DEFAULT_FROM_EMAIL = 'test@test.megiteam.pl'
SERVER_EMAIL = 'test@test.megiteam.pl'
EMAIL_USE_TLS = True
edit: If any1 else was following djangobook, this is the part causing it:
send_mail(
request.POST['subject'],
request.POST['message'],
request.POST.get('email', 'noreply@example.com'), #get rid of 'email'
['siteowner@example.com'],
解决方案
The explanation is in the error message. Your email host is rejecting the email because of the sender address randomacc@hotmail.com
that you have taken from the contact form.
Instead, you should use your own email address as the sender address. You can use the reply_to
option so that replies go to your user.
email = EmailMessage(
'Subject',
'Body goes here',
'test@test.megiteam.pl',
['to@example.com',],
reply_to='randomacc@hotmail.com',
)
email.send()
On Django 1.7 and earlier, there isn't a reply_to
argument, but you can manually set a Reply-To
header:
email = EmailMessage(
'Subject',
'Body goes here',
'test@test.megiteam.pl',
['to@example.com',],
headers = {'Reply-To': 'randomacc@hotmail.com'},
)
email.send()
Edit:
In the comments you asked how to include the sender's address in the message body. The message
and from_email
are just strings, so you can combine them however you want before you send the email.
Note that you shouldn't get the from_email
argument from your cleaned_data. You know that the from_address
should be test@test.megiteam.pl
, so use that, or maybe import DEFAULT_FROM_EMAIL
from your settings.
Note that if you create a message using EmailMessage
as in my example above, and set the reply to header, then your email client should do the right thing when you hit the reply button. The example below uses send_mail
to keep it similar to the code you linked to.
from django.conf import settings
...
if form.is_valid():
cd = form.cleaned_data
message = cd['message']
# construct the message body from the form's cleaned data
body = """
from: %s
message: %s""" % (cd['email'], cd['message'])
send_mail(
cd['subject'],
body,
settings.DEFAULT_FROM_EMAIL, # use your email address, not the one from the form
['test@test.megiteam.pl'],
)
相关文章