我无法使用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")
我的views.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')
我面临的错误是
SMTPSender在/(501,b'5.1.7无效地址处被拒绝 [MAXPR01MB0396.INDPRD01.PROD.OUTLOOK.COM]','=?utf-8?q?Your?=')
模板是由form.as_p()
形成的普通联系表单答案 0 :(得分:1)
错误指出,您的发件人地址不正确。根据{{3}},它是第三个参数,如本例所示:
class Griddler::EmailsController < ActionController::Base
skip_before_action :verify_authenticity_token, raise: false
protect_from_forgery with: :null_session
def create
normalized_params.each do |p|
process_email email_class.new(p)
end
head :ok
end
private
delegate :processor_class, :email_class, :processor_method, :email_service, to: :griddler_configuration
private :processor_class, :email_class, :processor_method, :email_service
def normalized_params
Array.wrap(email_service.normalize_params(params))
end
def process_email(email)
processor_class.new(email).public_send(processor_method)
end
def griddler_configuration
Griddler.configuration
end
end
如果要将发件人姓名添加到脚本中,docs也可能有用。