在django发送邮件 - 与gmail smtp问题

时间:2013-06-24 06:02:52

标签: django heroku django-models gmail django-views

我拥有一个简单的投资组合网站@ www.manojmj.com

我在网站上有一个联系表格,用户可以填写表格并通过电子邮件发送给我

目前,我已将我的Gmail帐户配置为通过django发送邮件。

我知道如果我使用gmail作为我的提供商,那么邮件中的起始地址将替换为我在settings.py中给出的地址,并且无法解决此问题。

我对此感到满意,但真正的问题是,当我在localhost上运行我的项目时,电子邮件发送得很好,但是一旦我部署它,我就会收到这样的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 = 'mymail@gmail.com'
SERVER_EMAIL = 'mymail@gmail.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'mymail@gmail.com'
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,['manojmj92@gmail.com'], 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错误?

2 个答案:

答案 0 :(得分:2)

这些设置适用于我:

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

答案 1 :(得分:1)

试试这个肯定会起作用

class Mail:
def send_mail(self, message):

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

    gmailUser = 'abc@gmail.com'
    gmailPassword = 'abc123'
    recipient = 'xyz@gmail.com'

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

希望这能解决您的问题