2013-06-24 126 views
1

我自己一個簡單的組合網站@ www.manojmj.com在Django發送郵件 - 問題與Gmail SMTP

我在該網站的聯繫人表格,用戶可以填寫一個表格,並通過電子郵件發送

給我

現在,我已經配置了我的gmail帳戶,通過django發送郵件。

我知道從郵件中的地址將被我自己的地址替換settings.py中給出的,如果我使用gmail作爲我的提供者,並且沒有辦法解決此問題。

我對此很滿意,但真正的問題是,即時通訊在本地主機上運行我的項目,電子郵件正在發送很好,但一旦我部署它,我得到這樣的SMTP錯誤。

SMTPAuthenticationError at/
(535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8  http://support.google.com/mail/bin/answer.py?answer=14257\n5.7.8 {BADCREDENTIALS}  r2sm18714441qeh.7 - gsmtp') 
Request Method: POST 
Request URL: http://www.manojmj.com/ 
Django Version: 1.4.3 
Exception Type: SMTPAuthenticationError 
Exception Value:  
(535, '5.7.8 Username and Password not accepted. Learn more at\n5.7.8  http://support.google.com/mail/bin/answer.py?answer=14257\n5.7.8 {BADCREDENTIALS} r2sm18714441qeh.7 - gsmtp') 
Exception Location: /app/.heroku/python/lib/python2.7/smtplib.py in login, line 613 
Python Executable: /app/.heroku/python/bin/python 
Python Version: 2.7.4 
Python Path:  
['/app', 
'/app/.heroku/python/bin', 
'/app/.heroku/python/lib/python2.7/site-packages/distribute-0.6.36-py2.7.egg', 
'/app/.heroku/python/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg', 
'/app', 
'/app/.heroku/python/lib/python27.zip', 
'/app/.heroku/python/lib/python2.7', 
'/app/.heroku/python/lib/python2.7/plat-linux2', 
'/app/.heroku/python/lib/python2.7/lib-tk', 
'/app/.heroku/python/lib/python2.7/lib-old', 
'/app/.heroku/python/lib/python2.7/lib-dynload', 
'/app/.heroku/python/lib/python2.7/site-packages', 
'/app/.heroku/python/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info'] 
Server time: Mon, 24 Jun 2013 00:52:42 -0500 

郵件設置,在我的settings.py

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

我在views.py

def home(request): 
    context = RequestContext(request) 
    if request.method=='POST': 
     email = request.POST.get('email') 
     matter = request.POST.get('message') 
     subject = request.POST.get('subject') 
     subject = "This mail is from " + " "+ email +" regarding " + " " +subject 
     matter = "Email = "+ " "+ email + "\n\n\n"+ matter 
     if subject and matter and email: 
      try: 
       send_mail(subject, matter, email,['[email protected]'], fail_silently=False) 
      except BadHeaderError: 
       return HttpResponse("no response") 
     else: 
      return HttpResponse("Enter all fields") 
return render_to_response("public/index.html",context) 

功能您可以檢查出錯誤自己@ manojmj.com

我問題是:

  1. django是否有任何其他方式從聯繫人表單發送電子郵件?
  2. 如果不是,我該如何糾正這個smtp錯誤?
+0

Gmail中有非常嚴密的保安。因此,當您部署它時,由於IP更改,gmail檢測到可疑活動。很可能你在那裏有一個驗證碼。嘗試在瀏覽器中登錄到Gmail,看看它是否顯示你的驗證碼? –

回答

1

嘗試這將肯定

class Mail: 
def send_mail(self, message): 

    import smtplib 
    from email.MIMEMultipart import MIMEMultipart 
    from email.MIMEText import MIMEText 

    gmailUser = '[email protected]' 
    gmailPassword = 'abc123' 
    recipient = '[email protected]' 

    msg = MIMEMultipart() 
    msg['From'] = gmailUser 
    msg['To'] = recipient 
    msg['Subject'] = "Success of mail " 
    msg.attach(MIMEText(message)) 

    mailServer = smtplib.SMTP('smtp.gmail.com', 587) 
    mailServer.ehlo() 
    mailServer.starttls() 
    mailServer.ehlo() 
    mailServer.login(gmailUser, gmailPassword) 
    mailServer.sendmail(gmailUser, recipient, msg.as_string()) 
    mailServer.close() 


m = Mail() 
m.send_mail('your messgae') 

希望的工作,這將solvwe您的問題

1

這些設置爲我的工作與Gmail:

import os 

# EMAIL Settings 
EMAIL_USE_TLS = True 
EMAIL_HOST = 'smtp.gmail.com' 
EMAIL_HOST_USER = os.environ['ric_email_user'] 
EMAIL_HOST_PASSWORD = os.environ['ric_email_pw'] 
EMAIL_PORT = 587