在Django中发送邮件-gmail smtp问题 [英] sending mail in django - issue with gmail smtp

查看:81
本文介绍了在Django中发送邮件-gmail smtp问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I own a simple portfolio website @ www.manojmj.com

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

I have a contact form on the site where users can fill a form and send it to me via email

现在,我已经配置了我的Gmail帐户以通过Django发送邮件.

Right now, I have configured my Gmail account for sending mails via django.

我知道,如果我使用gmail作为提供商,则邮件中的发件人地址将替换为settings.py中指定的我自己的地址.

I know the from address in the mail will be replaced by my own address as given in settings.py if I use gmail as my provider and there is no way around this.

我对此很好,但是真正的问题是,当我在本地主机上运行我的项目时,电子邮件的发送就很好了,但是一旦部署它,我会收到这样的SMTP错误.

I am ok with this, but the real issue is that, while I'm running my project on localhost, the emails are being sent just fine, but once I deploy it, I get an SMTP error like this.

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中的邮件设置

mail setting in my 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中的我的功能

my function in 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上自行检查错误

You can check out the error yourself @ manojmj.com

我的问题是:

  1. django还没有其他方法可以从联系表发送电子邮件吗?
  2. 如果没有,如何纠正此smtp错误?

推荐答案

这些设置适用于我的Gmail:

These settings work for me with 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

这篇关于在Django中发送邮件-gmail smtp问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆