Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a email from a form django

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!.

like image 900
Luis Bermúdez Avatar asked Nov 28 '25 19:11

Luis Bermúdez


1 Answers

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)
like image 128
arjun Avatar answered Dec 01 '25 11:12

arjun