2012-04-30 12 views
9

Quando la vista che invia l'e-mail viene utilizzata non succede nulla, ho quindi inserito send_mail (...) nella shell python e restituito 1 ma non ho ricevuto alcuna e-mail.Django send_mail non funziona

Questo è il mio settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = '[email protected]' 
EMAIL_USE_TLS = True 

Questa è la vista:

def send_email(request): 
    send_mail('Request Callback', 'Here is the message.', '[email protected]', 
     ['[email protected]'], fail_silently=False) 
    return HttpResponseRedirect('/') 
+0

Hai controllato la tua casella di posta SPAM? Hai creato il record SPF? http://support.google.com/a/bin/answer.py?hl=en&answer=33786 – jpic

risposta

10

regolare le impostazioni così:

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

Regolare il tuo codice:

from django.core.mail import EmailMessage 

def send_email(request): 
    msg = EmailMessage('Request Callback', 
         'Here is the message.', to=['[email protected]']) 
    msg.send() 
    return HttpResponseRedirect('/') 
0

Se non si curaPrevenire intestazione iniezione: (si dovrebbe cura su di esso: https://docs.djangoproject.com/es/1.9/topics/email/#preventing-header-injection, ma continuiamo)

Il settings.py:

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

L'views.py (esempio) :

from django.views.generic import View 
from django.core.mail import send_mail 
from django.http import HttpResponse, HttpResponseRedirect 

class Contacto(View): 
     def post(self, request, *args, **kwargs): 
      data = request.POST 
      name = data.get('name', '') 
      subject = "Thanks %s !" % (name) 
      send_mail(subject, data.get('message', ''), '[email protected]', [data.get('email', '')], fail_silently=False) 
     return HttpResponseRedirect('/') 

Questo è un modo pericoloso per inviare una e-mail

Quando provi a inviare l'e-mail, riceverai un'email di google che ti consiglia di non farlo. È necessario 'Attivare' le 'App meno sicure' (https://www.google.com/settings/security/lesssecureapps) e riprovare. La seconda volta funziona.