python - 错误 : SMTPRecipientsRefused 553, '5.7.1 #while working on contact form in django

标签 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')}

我的设置.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 header :

email = EmailMessage(
    'Subject',
    'Body goes here',
    'test@test.megiteam.pl',
    ['to@example.com',],
    headers = {'Reply-To': 'randomacc@hotmail.com'},
)
email.send()

编辑:

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

注意你不应该得到 from_email来自你的 cleaned_data 的论点。你知道 from_address应该是 test@test.megiteam.pl ,所以使用它,或者导入 DEFAULT_FROM_EMAIL从您的设置。

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

关于python - 错误 : SMTPRecipientsRefused 553, '5.7.1 #while working on contact form in django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9281334/

相关文章:

python - 无法启动 Windows 快捷方式

python - 按索引将键值插入列表中的嵌套字典

python - Django 模板 : get many-to-many value in formset

php - 通过 SMTP 和 PHPMailer 发送外发邮件时出现超时错误

email - 使用 Rust lettre 库从 Exchange SMTP 获取 "Client was not authenticated to send anonymous mail during MAIL FROM"错误

python - 另一个 Python 模块重载问题

javascript - Django CORS 访问控制允许来源丢失

django - 自定义 LoginView django extra_context

django - django 的 Secure_ssl_redirect 设置在 heroku 上不执行任何操作

email - 多部分/替代内容类型是否需要内容传输编码?