在Django中通过contact.html模板发送电子邮件,可以按照以下步骤进行操作:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.qq.com' # SMTP服务器地址
EMAIL_PORT = 465 # SMTP服务器端口号
EMAIL_HOST_USER = 'sender@example.com' # 发件人邮箱
EMAIL_HOST_PASSWORD = 'password' # 发件人邮箱密码
EMAIL_USE_SSL = True # 使用SSL加密连接
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER # 默认发件人邮箱
from django.shortcuts import render
from django.core.mail import send_mail
from django.conf import settings
def send_email(request):
if request.method == 'POST':
subject = 'Contact Form Submission' # 邮件主题
message = 'This is a test email.' # 邮件内容
from_email = settings.DEFAULT_FROM_EMAIL # 发件人邮箱
recipient_list = ['recipient@example.com'] # 收件人邮箱列表
send_mail(subject, message, from_email, recipient_list)
return render(request, 'success.html')
return render(request, 'contact.html')
from django.urls import path
from . import views
urlpatterns = [
path('send_email/', views.send_email, name='send_email'),
]
<!DOCTYPE html>
<html>
<head>
<title>Contact Form</title>
</head>
<body>
<h1>Contact Form</h1>
<form method="post" action="{% url 'send_email' %}">
{% csrf_token %}
<!-- 表单字段 -->
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
以上是在Django中通过contact.html模板发送电子邮件的基本步骤。当用户填写完联系表单并提交后,将会触发send_email视图函数,该函数会使用Django提供的send_mail函数发送邮件。发送成功后,页面将跳转到一个success.html模板页面,可以自行创建该模板文件并显示发送成功的消息。
此外,腾讯云提供的相关产品中,可以使用腾讯云的SMTP邮件推送服务(https://cloud.tencent.com/document/product/1047)来发送邮件。
领取专属 10元无门槛券
手把手带您无忧上云