2012-04-30 17 views
9

जब ईमेल भेजता है, तो इसका उपयोग कुछ भी नहीं होता है, फिर मैंने python shell में send_mail (...) दर्ज किया और यह 1 लौटा लेकिन मुझे कोई ईमेल प्राप्त नहीं हुआ।Django send_mail काम नहीं कर रहा

यह मेरी 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 

यह दृश्य है:

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

क्या आपने अपना स्पैम इनबॉक्स चेक किया था? क्या आपने एसपीएफ़ रिकॉर्ड बनाया था? http://support.google.com/a/bin/answer.py?hl=en&answer=33786 – jpic

उत्तर

10

अपनी सेटिंग समायोजित इस प्रकार:

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('/') 
:

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]' 

अपने कोड को समायोजित करें

0

आपहैडर इंजेक्शन की रोकथाम पर ध्यान देते हैं नहीं: (आप इसके बारे में ध्यान देना चाहिए: https://docs.djangoproject.com/es/1.9/topics/email/#preventing-header-injection, लेकिन आइए आगे बढ़ें)

settings.py:

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

views.py (उदाहरण) :

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('/') 

यह ईमेल भेजने के लिए एक खतरनाक तरीका है

जब आप पहली बार ईमेल भेजने का प्रयास करते हैं, तो आपको यह सलाह देने के लिए एक Google ईमेल प्राप्त होगा। आपको 'कम सुरक्षित ऐप्स' (https://www.google.com/settings/security/lesssecureapps) को 'सक्रिय' करना होगा और पुनः प्रयास करें। दूसरी बार काम करता है।

संबंधित मुद्दे