错误:SMTP收件人被拒绝553,“5.7.1 #在Django中处理联系表单时发生”

10

我正在尝试在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')}

我的 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

编辑:如果还有其他人在关注djangobook,那么这就是引起问题的部分:

        send_mail(
            request.POST['subject'],
            request.POST['message'],
            request.POST.get('email', 'noreply@example.com'), #get rid of 'email'
            ['siteowner@example.com'],
1个回答

14

错误信息中已经有解释了。你的邮件主机因为发件人地址是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()

编辑:

在评论中您问如何将发件人地址包含在邮件正文中。 messagefrom_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'],
        )

想要删除,但另一方面又不想让你离开没有加分 :) 谢谢 - mmln
7
不要删除 - 这将有助于下一个谷歌搜索“SMTPRecipientsRefused”的人 :) - Alasdair
嗯,我有一个问题要问你,考虑到我正在使用这段代码:http://pastebin.com/y3UfpU1y,我该如何在发送的邮件正文中附加发件人的电子邮件地址(或任何其他附加字段)? - mmln

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接