2012-04-30 11 views
9

電子メールを送信するビューが使用されていない場合、私はsend_mail(...)をpythonシェルに入力して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

あなたのSPAM受信トレイを確認しましたか? SPFレコードを作成しましたか? http://support.google.com/a/bin/answer.py?hl=ja&answer=33786 – jpic

答えて

10

は、このようにして設定を調整します。

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

はあなたのコードを調整します。

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

あなたはは、ヘッダインジェクションを防止する気にしない場合: は(あなたがそれを気にする必要があります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('/') 

これは電子メールを送信する危険な方法です

最初に電子メールを送信しようとすると、電子メールを送信するように指示する電子メールが届きます。 「安全性の低いアプリ」(https://www.google.com/settings/security/lesssecureapps)を「アクティブにする」必要があります。もう一度やり直してください。二度目の作品です。

関連する問題