He visto que Django tiene dos funciones para enviar emails: send_email y EmailMessage.
Al parecer la segunda es más actual y permite más funciones, pero he probado con ambas y ninguna me ha funcionado
Los ficheros que utilizo son los siguientes:
"apps/contact/forms.py"
Código Python:
Ver original
from django import forms class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField(widget=forms.Textarea) sender = forms.EmailField() cc_myself = forms.BooleanField(required=False)
"apps/contact/urls.py"
Código Python:
Ver original
from django.conf.urls import patterns, include, url from .views import * urlpatterns = patterns('', url(r'^$', ContactFormView.as_view(), name='contact'), )
"apps/contact/views.py"
Código Python:
Ver original
from django.views.generic import FormView from django.core.mail import EmailMessage from django.core.urlresolvers import reverse_lazy from .forms import ContactForm class ContactFormView(FormView): template_name = 'contact/form.html' form_class = ContactForm success_url = reverse_lazy('contact') def form_valid(self, form): subject = form.cleaned_data['subject'] message = form.cleaned_data['message'] sender = form.cleaned_data['sender'] cc_myself = form.cleaned_data['cc_myself'] if cc_myself: recipients.append(sender) mail = EmailMessage(subject, message, sender, recipients) mail.send() return super(ContactFormView, self).form_valid(form)
"templates/contact/form.html"
Código Python:
Ver original
{% extends 'general/base.html' %} {% load i18n %} {% block head %}Contacta{% endblock %} {% block title %} Contacta {% endblock %} {% block content %} <div class="form-div"> <form action="/contact/" method='POST'> {% csrf_token %} {% for field in form %} {% ifequal field.name "subject" %} <input id="id_subject" type="text" name="subject" placeholder="{% trans 'Subject' %}"/> {% else %}{% ifequal field.name "message" %} <textarea id="id_message" rows="5" cols="40" placeholder="{% trans 'Message' %}"></textarea> {% else %}{% ifequal field.name "sender" %} <input id ="id_sender" type="email" name="sender" placeholder="{% trans 'Email' %}"/> {% else %}{% ifequal field.name "cc_myself" %} <div class="checkbox"> <input id="id_cc_myself" type="checkbox" name="cc_myself" /> <label for="id_cc_myself">{% trans 'CC Myself' %}</label> </div> {% else %}{{ field }} {% endifequal %}{% endifequal %}{% endifequal %}{% endifequal %} {% endfor %} <p class="submit"> <input type="submit" name="mail" class="button" value="{% trans 'Send' %}" /> </p> </form> </div> {% endblock %}
"settings.py"
Código Python:
Ver original
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.gmail.com' EMAIL_PORT = 587 EMAIL_HOST_USER = '[email protected]' EMAIL_HOST_PASSWORD = 'pass' EMAIL_USE_TLS = True
He probado a enviarlo desde consola mediante "python manage.py shell" y ha funcionado correctamente
¿Alguna idea?
Gracias