im试图在django 1.3、python 2.6中创建一个联系人表单。
以下错误的原因是什么?
错误:
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')}
我的设置.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
编辑:如果其他任何一个在djangobook后面,这就是导致它的部分:
send_mail(
request.POST['subject'],
request.POST['message'],
request.POST.get('email', 'noreply@example.com'), #get rid of 'email'
['siteowner@example.com'],
错误消息中有解释。由于您从联系表格中获取的发件人地址randomacc@hotmail.com
,您的电子邮件主机拒绝了该电子邮件。
相反,您应该使用自己的电子邮件地址作为发件人地址。您可以使用reply_to
选项,以便回复到您的用户。
email = EmailMessage(
'Subject',
'Body goes here',
'test@test.megiteam.pl',
['to@example.com',],
reply_to='randomacc@hotmail.com',
)
email.send()
在Django 1.7及更早版本中,没有reply_to
参数,但您可以手动设置Reply-To
头:
email = EmailMessage(
'Subject',
'Body goes here',
'test@test.megiteam.pl',
['to@example.com',],
headers = {'Reply-To': 'randomacc@hotmail.com'},
)
email.send()
编辑:
在评论中,您询问了如何在邮件正文中包含发件人的地址。message
和from_email
只是字符串,所以在发送电子邮件之前,您可以根据需要组合它们。
请注意,您不应该从cleaned_data中获取from_email
参数。您知道from_address
应该是test@test.megiteam.pl
,所以使用它,或者从您的设置中导入DEFAULT_FROM_EMAIL
。
请注意,如果您像上面的示例中那样使用EmailMessage
创建邮件,并将回复设置为标头,那么当您点击回复按钮时,您的电子邮件客户端应该做正确的事情。下面的示例使用send_mail
使其与您链接到的代码保持相似。
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'],
)