【问题标题】:sending mail in django - issue with gmail smtp在 django 中发送邮件 - gmail smtp 问题
【发布时间】:2013-06-20 13:42:17
【问题描述】:

我拥有一个简单的投资组合网站@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 错误?

【问题讨论】:

  • Gmail 的安全性非常严格。因此,当您部署它时,gmail 检测到由于 IP 更改而导致的可疑活动。很可能你在那里有一个验证码。尝试在浏览器中登录 gmail,看看它是否显示验证码?

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


【解决方案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')

希望这能解决您的问题。

【讨论】:

    【解决方案2】:

    这些设置适用于我的 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
    

    【讨论】:

      猜你喜欢
      • 2012-12-13
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      • 2017-02-09
      • 2018-11-13
      相关资源
      最近更新 更多