AnswerBun.com

How to send email through contact.html template in django

Here is my contact.html code

<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._%+-][email protected][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>

and here is my views.py file

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,
        ['[email protected]'],
        fail_silently= False
    )
    return render(request,'contact.html',{'name': name})
else:
    return render(request,'contact.html',{})

Here is my settings.py file

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

It seems my action button in contact.html is not doing any job
(NOTE: My localhost 1025 is working)

How can I solve this please help

Thank You.

Stack Overflow Asked by Nishant Sagar on December 28, 2020

1 Answers

One Answer

For sending mail you have to use django mail package. You can follow below code :

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)

Apart from this you have to create one template for email body called "email_template.html". I provide simple snippet for that ...

File name : email_template.html

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

Add following variables in settings.py

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'your email password'
EMAIL_USE_TLS = True

And also you have to give permission to your email for access less secure app. You can give that using this link.

For simple form template "contact.html" follow below code snippet :

<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._%+-][email protected][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>

Or else you can follow this link for how to manage form in django

Answered by Parth Ghinaiya on December 28, 2020

Add your own answers!

Related Questions

First negative number row in Oracle

3  Asked on November 24, 2021 by user10916118

   

how to compare hashmap in Java?

2  Asked on November 24, 2021

   

Java Functional Interfaces and Lambda Expressions

2  Asked on November 24, 2021 by vilitaria

     

How to fill array in C++?

4  Asked on November 24, 2021 by kuz

 

Using while to exit for loop

3  Asked on November 24, 2021 by lojcs

   

How to register SFML Vector2 operators with AngelScript?

2  Asked on November 24, 2021 by veridis_quo_t

     

How to make a new line in Wrap Widget

2  Asked on November 24, 2021 by hng-trnh

   

AlarmManager and BroadcastReceiver in Android Oreo+

3  Asked on November 24, 2021 by kelton-holanda-fontenele

         

HOW? Controller return nothing/current view

5  Asked on November 24, 2021 by dent-argue

 

PHP get array child

2  Asked on November 24, 2021 by tgezginis

     

How to enable/disable table column text on click

1  Asked on November 24, 2021 by asif-zaidi

       

C# Assembly.Load() method loads the wrong dll?

1  Asked on November 24, 2021 by suns-flower

         

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.