Stack Overflow Asked by Nishant Sagar on December 28, 2020
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._%+-]+@[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.
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._%+-]+@[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
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP