在这里我试图接收来自普通用户的电子邮件,并使用联系表单将消息保存在管理页面中。此代码将数据按我的意愿保存在管理页面中,但并未将用户的电子邮件发送到从联系人表单发送消息,而不是采用已配置的电子邮件表单settings.py。**用户如何从该联系人表单向gmail管理收件箱发送电子邮件?**从此代码将电子邮件发送到admin gmail收件箱但是在admin gmail收件箱中收到的电子邮件地址是settings.py
中配置的电子邮件views.py
if request.method == 'POST':
form = SendMessageForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
message = form.cleaned_data['message']
try:
send_mail(name, message, email, ['admin mail address'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('index')
else:
return HttpResponse(form.errors)
else:
form = SendMessageForm()
设置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "email"
EMAIL_HOST_PASSWORD = 'pass'
EMAIL_PORT = '587'
模板
<form method="post" class="volunter-form">
{% csrf_token %}
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Your Name">
</div>
<div class="form-group">
<input type="text" name="email" class="form-control" placeholder="Your Email">
</div>
<div class="form-group">
<textarea name="message" id="" cols="30" rows="3" class="form-control" placeholder="Message"></textarea>
</div>
<div class="form-group">
<input type="submit" value="Send Message" class="btn btn-white py-3 px-5">
</div>
</form>
答案 0 :(得分:0)
我认为您需要自定义电子邮件配置。最好您尝试自定义email backend
还有一个类似的问题:Dynamic Django Mail Configuration