SMTP Authentication Error with Django on Heroku - python

I am trying to send emails from my django app using the the gmail smtp servers. The emails are being sent when I run the application on my local server. But I'm getting an SMTP Authentication Error while using it on heroku.
Traceback - link
settings.py -
# Email configuration.
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '***************#gmail.com'
EMAIL_HOST_PASSWORD = '************'
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = '******************#gmail.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
And I have rechecked the account password, also I have enabled the access of less secure apps from my google account. But still I am getting this error.

Disabling CAPTCHA for clients
If you are not using 2-factor authentication and you have verified the credentials in your Python source are correct, follow these steps:
Login to gmail in your browser
Navigate to the DisplayUnclockCaptcha page.
Click the continue button, and you will see the message
Account access enabled Please try signing in to your Google account again from your new device or application.
Run your Python script - your login attempt should be successful.

Try using sendgrid, because gmail smtp has some problem. I too was unable to do using gmail smtp. But it worked perfectly using sendgrid. And this is also free for basic use.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'username'
EMAIL_HOST_PASSWORD = 'userpassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'asdf#example.com'

Change from mail(#gmail.com) domain to your company domain .then it will work. we can't use from mail domain #gmail.com, DEFAULT_FROM_EMAIL = '******************#yourcompanydomain.com'

Related

Getting Server Error (500) on django herokuapp while sending email from gmail SMTP

I have created a Django app and deployed on to Heroku.
I have a view which sends invitation to a new person.
Everything is working fine on the local machine. After I deployed it on to Heroku with
DEBUG=FASLE
in setttings.py
I'm getting an error Server Error (500)
here is what i have added in my settings.py file
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = 'youremail#gmail.com'
EMAIL_HOST_PASSWORD = 'yourpwd'
# lesssecureappas - https://myaccount.google.com/u/2/lesssecureapps?utm_source=google-account&utm_medium=web
# cpatcha -- https://accounts.google.com/b/0/DisplayUnlockCaptcha
EMAIL_SUBJECT_PREFIX = 'test'
SERVER_EMAIL = EMAIL_HOST_USER
I have even allowed access to captcha and less secure apps.
It is working fine for 24 hours on production when I grant access. The very next day it is rasing the same error.
How to approach this issue.
I have hardcoded the values instead of variables. Please suggest any ideas?

Email verification after signup code works but email is not sent

My code works and outputs the email to the console but I don't receive any email in my inbox.
My settings:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail#gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
First, you need to set EMAIL_BACKEND in setting like this.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Second, you need to define SMTP credentials details in settings.
For development on localhost, you can run local SMTP server by running following command on terminal.
python -m smtpd -n -c DebuggingServer localhost:1025
For more info Sending email
Add this in settings.py to print the email on console.
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
Refer: Test sending email without email server

Django - Sending email works in shell, but not on localhost - ConnectionRefusedError

I'm new to Django, but I have read on the order of 50 pages (mostly SO and Django documentation) and haven't found anything that quite works for my situation.
I'm merely trying to send an email via the send_mail function in Django; although I'm hoping to get this working through SMTP on Gmail, right now it doesn't even work via localhost. When I run my test_email.py file where I'm testing this send_mail function, I get the following error:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
When I run this same send_mail call in the shell (via python manage.py shell), it's completely fine and displays the email just as I would expect. It's only once I try to run it in Django that the error pops up. Note that I get the above error no matter which permutation of the following 2 sets of settings I put in my settings.py file:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
I've also played around with the following in my settings.py file, and various mixes of these settings with the previous set (though I guess I'm getting ahead of myself seeing how localhost isn't working):
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail#gmail.com' # Note I'm using my actual gmail address here in real life
EMAIL_HOST_PASSWORD = '****' # Note I'm using my actual gmail password here in real life
DEFAULT_FROM_EMAIL = 'myemail#gmail.com' # Note I'm using my actual gmail address here in real life
SERVER_EMAIL = 'myemail#gmail.com' # Note I'm using my actual gmail address here in real life
I'm running the following command in a CMD prompt off to the side when I try to run going the localhost route:
python -m smtpd -n -c DebuggingServer localhost:1025
Here's the test_email.py file I'm trying to run in Django, where I get the connection refused error:
from django.core.mail import send_mail,EmailMessage
from django.conf import settings
settings.configure()
# Create your tests here.
# Email test #1: send email from my personal email to myself
send_mail(
'Email Test #1',
'Test if I can send email from my personal Gmail account to another email account via Django project.',
settings.EMAIL_HOST_USER,
['myemail#gmail.com'],
fail_silently=False
)
Any thoughts on why this isn't even working via localhost or suggestions on other things to try would be greatly appreciated.
Thanks!
I think there are some problem with gmail smtp service. Try using smtp service form sendgrid. It is also free for basic usage. I too was facing the same issue earlier.
In project settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgridusername'
EMAIL_HOST_PASSWORD = 'sedgridpasseord'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'yourmail#site.com'
still error
Try if `smtp` service is working on your your server.
The following configuration worked for me:
settings.py
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = 'sendgrid-api-key' # this is your API key
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = 'your-email#example.com' # this is the sendgrid email
I used this configuration, and it worked properly, and I strongly suggest using environment variables to fill EMAIL_HOST_PASSWORD and DEFAULT_FROM_EMAIL:
import os # this should be at the top of the file
# ...
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD", "")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
DEFAULT_FROM_EMAIL = os.getenv("DEFAULT_FROM_EMAIL", "")
In order to send an email, use the following code:
from django.conf import settings
from django.core.mail import send_mail
send_mail('This is the title of the email',
'This is the message you want to send',
settings.DEFAULT_FROM_EMAIL,
[
settings.EMAIL_HOST_USER, # add more emails to this list of you want to
]
)
Make sure your settings are in the project settings.py NOT app settings.py as follows
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
note the empty string for EMAIL_HOST_USER I think this is your mistake
Once you run your virtual python SMTP using
python -m smtpd -n -c DebuggingServer localhost:1025
You will be able to send to your local smtp server with no problem.
Source
Try to enable this option in your google account after you make sure that your mail and password is right.
https://myaccount.google.com/lesssecureapps?pli=1
google blocks the access to insecure apps.
your code seems to be right!

Django password_reset won't start tls authentication to send email

I have the following set up below:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL__USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = None #'user#gmail.com'
EMAIL_HOST_PASSWORD = None #'password'
DEFAULT_FROM_EMAIL = 'user#gmail.com'
SERVER_EMAIL = 'user#gmail.com'
EMAIL_PORT = 587
in settings.py. However, when I call password_reset, which handles sending an email in Django, SMTP AUTH extension not supported by server. is thrown. Is there an additional step to the password_reset() method? I'm simply passing this to my free smtp.gmail.com server through my Gmail account.
You have a double underscore in the EMAIL_USE_TLS setting.
Additional steps to set up Gmail SMTP:
-Make sure to enable IMAP under Gmail -> Cog icon -> Settings -> Forward and POP/IMAP
-Make sure to let less-secure apps access the account (at your own risk)

Sending mail in Django 1.5 with Gmail smtp

I try to send mail with Gmail smtp in my Django App.
I followed some instructions to configure my setting.py file.
Here is my code snippet in setting.py file.
EMAIL_BACKEND = 'django.core.mail.backends.dummy.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myusername#gmail.com'
EMAIL_HOST_PASSWORD = 'my_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
then I tried these commands below to send mail to my own gmail account:
python manage.py shell
>>> from django.core.mail import EmailMessage
>>> email = EmailMessage('Mail Test', 'This is a test', to=['myusername#gmail.com'])
>>> email.send()
it returned 1 to me, it should be successful.
However, I can't find the testing mail in my inbox in my gmail.
Are there anything which I missed to set?
I run my Django site on localhost in Debug mode. And the Django's version is 1.5.
Thanks in advance! Any suggestion is appreciated.
UPDTED:
I changed the following code
EMAIL_BACKEND = django.core.mail.backends.dummy.EmailBackend
to
EMAIL_BACKEND = django.core.mail.backends.smtp.EmailBackend
And, I have succeed to send the mail.
You email backend is set to dummy. Change it to smtp
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Documentation
Dummy backend just prints to the console, and does nothing

Categories

Resources