我的表格從我的settings.py中列出的帳戶發送電子郵件罰款,但在郵件中我只獲得表格的主題部分和消息部分。電子郵件中沒有發件人部分,所以我不知道誰會發送電子郵件。有誰知道如何解決這一問題?Django聯繫表格郵件錯誤
我forms.py
class ContactForm(forms.Form):
subject = forms.CharField(max_length=50)
message = forms.CharField()
sender = forms.EmailField()
我Views.py接觸功能
def contact(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
subject = form.cleaned_data['subject']
message = form.cleaned_data['message']
sender = form.cleaned_data['sender']
recipients = ['[email protected]']
send_mail(subject, message, sender, recipients)
return HttpResponseRedirect('/thanks/')
else:
form = ContactForm()
return render(request, "contact.html", {'form':form,})
我Settings.py
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'mypassword'
感謝您的回覆,但問題實際上並非發送電子郵件。 send_mail()只允許發送兩個字段:Subject和Message。但是,我的表單有一個選項讓用戶列出他們的個人電子郵件,這是不允許傳入send_mail()函數的。有沒有辦法給send_mail()添加字段? – user1835351
你想添加什麼其他領域?爲了獲得更大的靈活性,您可能需要查看EmailMessage類。 https://docs.djangoproject.com/en/dev/topics/email/?from=olddocs#emailmessage-objects – jondykeman