我制作了一个联系表单,我用mailtrap服务测试了一下,它可以很好地接收到消息。
但是当我为一个真实的邮件帐户设置smpt参数时,我有这个错误消息
SMTPRecipientsRefused at /contact/
{'info@mysite.net': (550, b'relay not permitted: you must be authenticated to send messages')}
smtp服务器和邮件帐户在主机alwasdata.net上,但我测试了一个outlook帐户,它总是相同的事情总是这个相同的错误。它似乎来自contact方法中的那行:
message,
'info@mysite.net',
['info@othersite.net'],
fail_silently=False,
在settings.py中我有这个配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = "info@mysite.net"
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_HOST = 'smtp-blablabla.net'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
subject = "Message d'un visiteur sur votre site"
body = {
'Nom': form.cleaned_data['first_name'],
'Tel': form.cleaned_data['tel'],
'Email': form.cleaned_data['email'],
'Message':form.cleaned_data['message'],
}
message = "n".join(body.values())
try:
send_mail(
subject,
message,
'info@mysite.net',
['info@othersite.net'],
fail_silently=False,
)
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('/')
form = ContactForm()
return render(request,'pages/email_form.html',{'form':form})
forms.py
from django import forms
class ContactForm(forms.Form):
first_name = forms.CharField(max_length=100, required=True)
tel = forms.CharField(max_length=15, required=True)
email = forms.EmailField(required=False)
message = forms.CharField(widget=forms.Textarea, required=True)
我更改了一些参数:
我把所有的电子邮件地址放在我的网站所在的服务器上,
try:
send_mail(
subject,
message,
'info@mysite.net',
['info@mysite.net'],
fail_silently=False,
)
,在我的主机上,我改变了邮件服务器的参数,使它重定向到我的目标电子邮件地址。现在是work