问题
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:
- Doesn't django have any other way to send email messages from contact forms?
- If not, how do I rectify this smtp error?
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/17268888/sending-mail-in-django-issue-with-gmail-smtp