2

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

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

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.

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

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

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)

You can check out the error yourself @ manojmj.com

My questions are:

  1. Doesn't django have any other way to send email messages from contact forms?
  2. If not, how do I rectify this smtp error?
Karthik Vg
  • 108
  • 13
Manoj M J
  • 379
  • 3
  • 15
  • 1
    Gmail has very tight security. So when you have deployed it gmail has detected a suspicious activity because of IP change. Most probably you are having a captcha there. Try to login to the gmail in your browser and see if it shows you captcha or not? – Aamir Rind Jun 24 '13 at 06:20

2 Answers2

2

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
Aaron Lelevier
  • 18,600
  • 11
  • 68
  • 105
1

Try this it will work for sure.

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

Hope this will solve your problem.

Alok Agarwal
  • 2,980
  • 3
  • 21
  • 32