Reputation: 654
As the title says i am trying to send a email from a form, but is not working:
As you can see:
<form method="POST" action=''>
{% csrf_token %}
<div class="control-group form-group">
<div class="controls">
<label>Nombre completo:</label>
<input type="text" class="form-control" id="nombre" required data-validation-required-message="Introduzca su nombre.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Asunto:</label>
<input type="text" class="form-control" id="asunto" required data-validation-required-message="Introduzca el asunto del correo.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Correo Electrónico:</label>
<input type="email" class="form-control" id="email" required data-validation-required-message="Introduzca su correo electrónico.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Mensaje:</label>
<textarea rows="10" cols="100" class="form-control" id="contenido" required data-validation-required-message="Escriba su mensaje" maxlength="999" style="resize:none"></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary">Enviar mensaje</button>
</form>
This is my view:
def contact(request):
contactdata = contactData.objects.get()
members = teamMembers.objects.filter(existencia=True)
templates = Templates.objects.get(isSelected=True)
categoria = Clasificacion.objects.filter(existencia=True)
enterprise = enterprisedata.objects.get()
content = request.POST.get('contenido', '')
name = request.POST.get('nombre', '')
email = request.POST.get('email', '')
subject = request.POST.get('asunto', '')
if request.method == 'POST' and email and name:
send_mail(subject, content, email, ['[email protected]'], fail_silently=False)
contexto = {'categoria':categoria,'templates':templates,'members':members,
'contactdata':contactdata,'enterprise':enterprise}
return render(request, 'contact/contact.html', contexto)
I am calling the form with the POST request, but is not sending anything!.
Hope you can help me, thank you!.
Upvotes: 3
Views: 8089
Reputation: 7330
If you want to send mail then you have to configure this setting in your project's settings.py
file
For example if you want to send email through your gmail account:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = "your gmail address"
EMAIL_HOST_PASSWORD = 'your password'
EMAIL_PORT = '587'
And also remember to enable less secure apps
in you google account for sending email.
And in the view you can try like this:
if request.method == 'POST' and email and name:
send_mail(subject, content, settings.EMAIL_HOST_USER, ['[email protected]'], fail_silently=False)
Upvotes: 6
Reputation:
Essentially this is the line of code that sends an email from djagno.
from django.core.mail import send_mail
send_mail(subject, content, from_email, to_list, fail_silently=False)
The subject and content part are intuitive, then what remains is setting up the from_email and the to_list(which could even be 1 email in a list).
You've to define the following variables in your settings file.
EMAIL_USE_TLS = True
EMAIL_HOST = smtp.gmail.com
EMAIL_HOST_USER = [email protected]
EMAIL_HOST_PASSWORD = password
EMAIL_PORT = 587
Django permits the use of gsuite accounts for sending emails. To set up gsuit, go to your Google Account settings, find Security -> Account permissions -> Access for less secure apps, enable this option.
About this option: https://support.google.com/accounts/answer/6010255 Hope this gets you going.
Upvotes: 4