django - SMTPSenderRefused at/(501, b'5.1.7 无效地址 [MAXPR01MB0396.INDPRD01.PROD.OUTLOOK.COM ]', ' =?utf-8?q?Your?=')

标签 django office365

我无法使用 office365 设置在 django 应用程序上发送邮件 我的电子邮件服务设置。

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.office365.com'
EMAIL_HOST_USER = "***********"
EMAIL_HOST_PASSWORD = '***********'
EMAIL_PORT = 587

我的模型.py

from django.db import models
from django import forms
from django.core.validators import RegexValidator
class ContactModel(models.Model):
    contact_name = models.CharField("Your Name ",max_length=20)
    contact_email = models.EmailField("Your Email ")
    content = models.TextField("Message ",max_length=1500)
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',
                                 message="Please Enter a valid Contact number(in +999999) format'. Up to 15 digits allowed Minimum 9 digits.")
    phone_number = models.CharField("Contact Number(in +9999 format)",validators=[phone_regex], max_length=17, blank=True)  # validators should be a list
    needacallback = models.BooleanField(" I want an Informational Phone Call")

我的观点.py

from django.shortcuts import render,render_to_response
from . import forms
from django.core.mail import EmailMessage
from django.shortcuts import redirect
from django.template.loader import get_template
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
from django.http import HttpResponse

from django.views.decorators.csrf import ensure_csrf_cookie




    def home(request):
        form = forms.ContactForm(request.POST or None, request.FILES or None)
        if request.method == 'POST':
            # form = form_class(data=request.POST)

            if form.is_valid():
                contactform = form.save(commit=False)
                contact_name = request.POST.get(
                    'contact_name'
                    , '')
                contact_email = request.POST.get(
                    'contact_email'
                    , '')
                needacallback = request.POST.get(
                    'needacallback'
                    , '')
                phone_number = request.POST.get(
                    'phone_number'
                    , '')
                form_content = request.POST.get('content', '')

                # Email the profile with the
                # contact information
                template = get_template('contact_template.html')
                context = {
                    'contact_name': contact_name,
                    'contact_email': contact_email,
                    'form_content': form_content,
                    'phone_number': phone_number,
                    'needacallback': needacallback,
                }
                content = template.render(context)
                if needacallback:
                    subject = "This person requested for a call"
                else:
                    subject = "New Message from " + contact_name
                email = EmailMessage(
                    subject,
                    content,
                    "Contact me" + '',
                    ['myemail@gmail.com'],
                    headers={'Reply-To': contact_email}
                )
                email.send()
                contactform.save()
                return redirect('thanks')

        return render(request, 'index.html', {
            'form': form,
        })

    def thanks(request):

        return render(request, 'thanks.html')

我遇到的错误是

SMTPSenderRefused at / (501, b'5.1.7 Invalid address [MAXPR01MB0396.INDPRD01.PROD.OUTLOOK.COM]', '=?utf-8?q?Your?=')

Template是一个普通的联系表单,由form.as_p()构成

最佳答案

正如错误指出的那样,您的发件人地址不正确。根据docs这是本例中的第三个参数:

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)

还有 this answer如果您想将发件人姓名添加到脚本中,可能会有用。

关于django - SMTPSenderRefused at/(501, b'5.1.7 无效地址 [MAXPR01MB0396.INDPRD01.PROD.OUTLOOK.COM ]', ' =?utf-8?q?Your?='),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51532268/

相关文章:

django - 在 Django 中查找多对多类别树的根

azure - 是否可以使用 LDAP 从 Office365 - Exchange Online/Azure 检索数据?

office365 - 获取 Office 365 中用户的应用程序或权限列表

rest - 使用 Office365 REST API 检索用户照片

excel - 如何在 Excel 中将两个垂直数字列表合并为一个长垂直列表?

Django:用多个计数注释模型非常慢

python - Django 未能为 python-ldap 构建wheel

python - 如何使用 Django ORM 按时间戳字段中的日期进行分组?

office365 - 使用 Office 365 API 的身份验证问题

javascript - 在哪里存储身份验证 token (前端)以及如何将其放入多个端点的 http header 中?