【问题标题】:Error: SMTPRecipientsRefused 553, '5.7.1 #while working on contact form in django错误:SMTPRecipientsRefused 553, '5.7.1 #while 在 django 中处理联系表单
【发布时间】:2012-03-06 02:00:11
【问题描述】:

我正在尝试在 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'],

【问题讨论】:

    标签: python django smtp sendmail


    【解决方案1】:

    解释在错误消息中。由于您从联系表单中获取的发件人地址 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()
    

    编辑:

    在 cmets 中,您询问了如何在邮件正文中包含发件人的地址。 messagefrom_email 只是字符串,因此您可以在发送电子邮件之前随意组合它们。

    请注意,您不应该从您的cleaned_data 中获取from_email 参数。你知道from_address 应该是test@test.megiteam.pl,所以使用它,或者从你的设置中导入DEFAULT_FROM_EMAIL

    请注意,如果您在上面的示例中使用EmailMessage 创建消息,并将回复设置为标题,那么当您点击回复按钮时,您的电子邮件客户端应该会做正确的事情。下面的示例使用send_mail 使其与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'],
            )
    

    【讨论】:

    • 想删除,但另一方面不想让你在没有+分的情况下离开 :) 谢谢
    • 不要删除——它会帮助下一个用谷歌搜索SMTPRecipientsRefused的人:)
    • hm,有一个问题要问你,考虑到我使用此代码:http://pastebin.com/y3UfpU1y 我如何在已发送邮件的正文中附加发件人电子邮件地址(或任何其他附加字段)?
    猜你喜欢
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2019-10-13
    • 2012-11-15
    • 1970-01-01
    • 2013-02-16
    • 2016-06-08
    • 1970-01-01
    相关资源
    最近更新 更多