错误:SMTPRecipientsRefused 553,'5.7.1#在django中处理联系表单时

mml*_*mln 9 python django smtp sendmail

我试图在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')}
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

编辑:如果任何其他人跟随djangobook,这是导致它的部分:

        send_mail(
            request.POST['subject'],
            request.POST['message'],
            request.POST.get('email', 'noreply@example.com'), #get rid of 'email'
            ['siteowner@example.com'],
Run Code Online (Sandbox Code Playgroud)

Ala*_*air 12

解释在错误消息中.您的电子邮件主机拒绝了该电子邮件,因为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()
Run Code Online (Sandbox Code Playgroud)

在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()
Run Code Online (Sandbox Code Playgroud)

编辑:

在评论中,您询问如何在邮件正文中包含发件人的地址.在messagefrom_email仅仅是字符串,这样你就可以将它们组合起来,但是你想您发送电子邮件之前.

请注意,您不应该from_email从cleaning_data 获取参数.你知道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'],
        )
Run Code Online (Sandbox Code Playgroud)

  • 不要删除 - 它将帮助下一个谷歌搜索`SMTPRecipientsRefused` :) (7认同)