2012-10-22 36 views
6

我甚至不知道如何調試:我在Django中的一個視圖中使用send_mail。在本地使用應用程序時它工作正常(使用我在生產中使用的相同SMTP設置),並且它在生產中的shell(再次使用相同的設置)正常工作。但是當我在生產中實際使用該應用程序時,該消息不會發送。任何想法如何排除故障?Django中的Send_mail,在shell中工作,在本地工作,而不在視圖中

我不會在整個視圖中粘貼,但這裏是相關的部分。

if request.method == 'POST': 
    message, form = decideform(request.POST) 
    if form.is_valid(): 
     invitation.status = form.cleaned_data['status'] 
     invitation.save() 
     message, form = decideform(request.POST) 
     update = 'Thank you for updating your status.' 

     # Send an email confirming their change 
     subject = 'Confirming your RSVP [%s %s]' % (invitation.user.first_name, invitation.user.last_name) 
     body = message + ' Remember, the URL to update or change your RSVP is http://the.url%s.' % invitation.get_absolute_url() 
     send_mail(subject, body, '[email protected]', ['[email protected]', invitation.user.email], fail_silently=True) 

而這裏的相關位從我LOCAL_SETTINGS文件,以改變爲安全起見突出的細節:

EMAIL_HOST = 'mail.mydomain.com' 
EMAIL_PORT = 587 
EMAIL_USE_TLS = True 
EMAIL_HOST_USER = '[email protected]' 
DEFAULT_FROM_USER = '[email protected]' 
EMAIL_HOST_PASSWORD = 'p4ssw0rd' 
+3

歡迎來到SO!您是否嘗試過使用'fail_silently = False'並尋找任何'SMTPException'? –

+0

謝謝César!我確實嘗試了將fail_silently標誌設置爲False(本地,生產shell和生產中的應用程序),並且在任何時候都沒有捕獲到SMTPExceptions。 (我假設生產中的應用程序會將該異常寫入錯誤日誌。) – mthomps

回答

0

檢查權限。這可能是因爲你有權限運行send_mail,但該應用程序沒有。

0

我遇到同樣的問題。 3年後:) 複製send_mail內嵌的代碼固定,但我仍然不知道爲什麼

 connection = django.core.mail.get_connection(username=None, password=None, fail_silently=False) 
     mail = django.core.mail.message.EmailMessage('hello', 'world', '[email protected]', ['[email protected]'],            connection=connection) 
     mail.send()    
0

你可以嘗試Django的sendgrid代替,這是非常容易configure.Use設置這樣

EMAIL_BACKEND = "sgbackend.SendGridBackend" 
SENDGRID_USER = 'xxxxxx' 
SENDGRID_PASSWORD = 'xxxxxx' 
EMAIL_PORT = 1025 
相關問題