2

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

2 回答 2

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
于 2014-02-26T15:15:31.547 回答
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')

希望这能解决您的问题。

于 2013-06-24T07:54:53.890 回答