gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:15:25 31 4
gpt4 key购买 nike

我无法使用 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com