如何通过 django 中的contact.html 模板发送电子邮件

Posted

技术标签:

【中文标题】如何通过 django 中的contact.html 模板发送电子邮件【英文标题】:How to send email through contact.html template in django 【发布时间】:2020-11-14 23:08:57 【问题描述】:

这是我的contact.html代码

<div class="col-lg-8">
                            
form class="form-area " id="myForm" action="% url 'contact' %" method="post" class="contact-form text-right">

<div class="row">

% csrf_token %    
<div class="col-lg-6 form-group">

<input name="name" placeholder="Enter your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" class="common-input mb-20 form-control" required="" type="text">
                                    
<input name="email" placeholder="Enter email address" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]1,63$" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" class="common-input mb-20 form-control" required="" type="email">

<input name="subject" placeholder="Enter subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter subject'" class="common-input mb-20 form-control" required="" type="text">
</div>

<div class="col-lg-6 form-group">
<textarea class="common-textarea form-control" name="message" placeholder="Enter Messege" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Messege'" required=""></textarea>   
</div>

这是我的views.py文件

def contact(request):
if request.method == 'POST':
    name = request.POST['name']
    email = request.POST['email']
    subject = request.POST['subject']
    message = request.POST['message']

    send an email 
    send_mail(
        subject,
        name,
        message,
        email,
        ['nishantkkr68@gmail.com'],
        fail_silently= False
    )
    return render(request,'contact.html','name': name)
else:
    return render(request,'contact.html',)

这是我的 settings.py 文件

EMAIL_HOST = 'localhost'
EMAIL_HOST = '1025'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False

似乎我在contact.html 中的操作按钮没有做任何工作 (注意:我的 localhost 1025 正在工作)

请帮忙解决这个问题

谢谢。

【问题讨论】:

【参考方案1】:

要发送邮件,您必须使用 django 邮件包。您可以按照以下代码:

from django.core.mail import EmailMultiAlternatives
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from django.template.loader import get_template

def send_email_smtp(from_email, recipient_list, subject, body):
    msg = EmailMultiAlternatives(subject, body, from_email, recipient_list)
    msg.attach_alternative(body, "text/html")
    msg.send()
    return True, None

name = request.POST['name']
recipient_email = request.POST['email']
subject = request.POST['subject']
message = request.POST['message']

html_template = get_template('email_template.html')
html_content = html_template.render(
    
        'message': message,
    )
send_email_smtp(settings.EMAIL_HOST_USER, [recipient_email],
                subject, body=html_content)

除此之外,您还必须为电子邮件正文创建一个名为“email_template.html”的模板。我为此提供了简单的 sn-p ...

File name : email_template.html

<html lang="en">
    <head>
    </head>
    <body>
         <h1>  message  </h1>
    </body>
    </html>

在settings.py中添加以下变量

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = 'nishantkkr68@gmail.com'
EMAIL_HOST_PASSWORD = 'your email password'
EMAIL_USE_TLS = True

而且您还必须授予您的电子邮件权限才能访问不太安全的应用程序。你可以使用这个link 来提供它。

对于简单的表单模板“contact.html”,请遵循以下代码 sn-p:

<form class="your css classes" action="" method="post" enctype="multipart/form-data">
  % csrf_token %
  <div class="col-lg-6 form-group">

<input name="name" placeholder="Enter your name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter your name'" class="common-input mb-20 form-control" required="" type="text">
                                    
<input name="email" placeholder="Enter email address" pattern="[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]1,63$" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter email address'" class="common-input mb-20 form-control" required="" type="email">

<input name="subject" placeholder="Enter subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter subject'" class="common-input mb-20 form-control" required="" type="text">
</div>

<div class="col-lg-6 form-group">
<textarea class="common-textarea form-control" name="message" placeholder="Enter Messege" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Enter Messege'" required=""></textarea>   
</div>
<input type="submit" class="" value="submit" style="margin-bottom: 40px;">
</form>

否则你可以关注link 了解如何在 django 中管理表单

【讨论】:

使用还得加上这个属性setting.py : EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = '' EMAIL_HOST_PASSWORD = '' EMAIL_USE_TLS = Ture 兄弟,请给我发电子邮件至 nishantkkr68@gmail.com,以便我可以将整个 contact.html 文件发送给您,并在查看后告诉我应该怎么做才能解决我的问题。 @NishantSagar 您只能在此处提供该文件。 @NishantSagar 请查看更新后的答案,您可以在 Linkedin 上与我联系这是我的个人资料 link。

以上是关于如何通过 django 中的contact.html 模板发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何通过 Django 中的模型属性之一对对象数组进行分组?

如何通过关系将复选框值保存到 django 中的 ManyToManyField?

如何通过 django 读取 excel 文件中的复选框值?

如何返回通过django中的视图的静态文件?

如何返回通过django中的视图的静态文件?

如何通过 django 中的contact.html 模板发送电子邮件