send_mail works locally but not on production hosting - django

The following works locally but as I deployed it to production hosting at Digital Ocean, the email is not sending as I test on shell command (python manage.py shell) like below. The send_mail line just got stuck there and am getting error: [Errno 101] Network is unreachable after few minutes. How can I capture the error on the email sending? Please advise how can I troubleshoot this issue.
from django.core.mail import send_mail
send_mail('test email', 'hello world', 'xxxx#gmail.com', ['xxxx#gmail.com'],fail_silently=False)
# Email settings
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'xxxx' #my gmail password
EMAIL_HOST_USER = 'xxxx#gmail.com' #my gmail username
EMAIL_PORT = 587
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

Your Django app settings look correct but you should also head over to https://accounts.google.com/DisplayUnlockCaptcha and make sure you enable access to lower security applications.
Also consider creating an application-specific password if you're using Two-Factor-Authentication.

Related

How to send with Sendgrid a reset password email from a Django app deployed in Heroku?

I have been struggling for a while in trying to correctly setup the settings.py to send an email for password reset.This is my current configuration:
SENDGRID_API_KEY = os.environ["SENDGRID_API_KEY"]
SENDGRID_PASSWORD= os.environ["SENDGRID_PASSWORD"]
SENDGRID_USERNAME= os.environ["SENDGRID_USERNAME"]
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = os.environ['SENDGRID_USERNAME']
#EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.environ["SENDGRID_PASSWORD"]
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ["DEFAULT_FROM_EMAIL"]
#SENDGRID_SANDBOX_MODE_IN_DEBUG = False
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
I have come across the following posts that are related to my problem but none of them have worked:
Sending SMTP email with Django and Sendgrid on Heroku
No module named 'sendgrid_backend' in django
Send email with Sendgrid in Django
Setting up email with Sendgrid in Heroku for a Django App
Heroku, Django, and Sendgrid - emails not sending?
When I used the EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"(after I installed the django-sendgrid-v5 library) I didn't receive any error but I didn't receive any email :'( and in the previous cases I encountered the following error SMTPServerDisconnected at /password-reset/ and Connection unexpectedly closed. Any help, suggestion, comment, crazy idea is really appreciated because I have spent several hours in this problem that will be a milestone in my project.
Thank you everyone :)
I had a similar issue deploying a Django app on Google Cloud App Engine. The following configuration (in settings.py) worked for me:
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # Exactly that.
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587 # 25 or 587 (for unencrypted/TLS connections).
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL')
For SENDGRID_API_KEY, use the API key created in the 'Email API/Integration Guide' section. In your case, choose the SMTP Relay option.
For DEFAULT_FROM_EMAIL, use the verified single sender email. You can find it in 'Settings/Sender Authentication'. This is what you type in "from email address" when you create a Sender. More details here.
I don't recommend Robert D's answer because by using ALLOWED_HOSTS = ['*'] you will be prone to HTTP Host header attacks. From the documentation:
"A value of '*' will match anything; in this case you are responsible to provide your own validation of the Host header (perhaps in a middleware; if so this middleware must be listed first in MIDDLEWARE)."
More details here.
I sympathise, having also spent too long on this. But just fixed it today, with as little as the following added into settings.py (and everything else 'out of the box' from Django re password reset).
ALLOWED_HOSTS = ['*']
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True

How to Send an Email Django

Previously, I was using SendGrid to serve emails using Django's SMTP backend, which worked perfectly fine. However, now I would like my project to use Microsoft Exchange. When I updated my SMTP configuration in settings.py, upon the submission of some form to be emailed, the page timesout when trying to reach the server: TimeoutError: [Errno 60] Operation timed out.
settings.py
# E-Mail
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.microsoft365.com'
EMAIL_HOST_USER = 'username#domain.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
views.py
# Send email
send_mail('!!New Mail!! ', content, 'noreply#domain.com', ['username#domain.com'], fail_silently=False)
I solved my problem when I used the correct SMTP port (25, 465, 587) for my particular mail server.
settings.py
EMAIL_PORT = 25

SMTPSenderRefused at /users/password-reset/ (530, b'5.5.1 Authentication Required. Learn more at\n5.5.1 https://support.google.com/ma

I am getting this error while sending mail using django.
/usr/lib/python3.6/smtplib.py in sendmail, line 867
/usr/lib/python3.6/smtplib.py in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
tried mailjet also same issues
If your are using Google SMTP make sure you have the following settings set in your settings.py file:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'user emailid'
EMAIL_HOST_PASSWORD = 'password'
Also make sure you are logged in with gmail with the provided email id and password in your machine.
Also you need to enable access for less secure apps in your Google account. Here is the link to help you change your Google Account configuration settings link
If you saved your email/password in environ variable for the first time. Please close your terminal and open it again, I hope this will solve your problem. I have run into the same problem and solved issues just like this.
You need to create app password via your google account.
You can't use your email address to send mail.
Go to this link: https://myaccount.google.com/security
For me the error was caused by:
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
as the env variables where not correctly imported.
Also remember to enable access for less secure apps in your Google account.
Your users must be active and have valid e-mail.
Go here https://myaccount.google.com/security, press on app password and gererate 16-symbolic password and put it to settings.
And use it in settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'put here your email-address with 16-symbolic password'
EMAIL_HOST_PASSWORD = 'put here your 16-symbols symbolic'

How to setup Smtp and webfaction email host in a single settings.py file in django1.5.1?

This the one host that i created I want to add another one host also.
EMAIL_USE_TLS = True`
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587 `
EMAIL_HOST_PASSWORD = 'developer#sangeeth'
EMAIL_HOST_USER = 'developer.sangeeeth#gmail.com'
More than one EMAIL_HOST cannot be defined in django settings.py. Read more about email settings and sending emails in django. If you want to use different email hosts for sending emails, you can try python smtplib for sending emails instead of django's email feature.

Setting up email with Sendgrid in Heroku for a Django App

I am deploying a Django app on Heroku, and using the Sendgrid addon to send out validation email when a user registers on the site.
I followed the instructions here and pasted the following into settings.py:
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'sendgrid_username'
EMAIL_HOST_PASSWORD = 'sendgrid_password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
However, my app is crashing after registration.
What exactly am I supposed to put for EMAIL_HOST_USER and EMAIL_HOST_PASSWORD?
Under the developer's tab in the sendgrid addon in heroku, it gives me the username app*******#heroku.com, and for password it just says "Your Password". Is the password my Heroku password?
Also, do I need to include DEFAULT_FROM_EMAIL in my settings.py file? And where do I tell Sendgrid what it is?
EDIT: I've set DEBUG = True, and it looks like the error is:
SMTPSenderRefused
(550, 'Cannot receive from specified address <info#myapp.com>: Unauthenticated senders not allowed', 'info#myapp.com')
it looks like the problem is happening before Sendgrid does its thing. Do I need to authenticate the email address with Heroku somehow?
Within your settings.py include:
import os
EMAIL_HOST_USER = os.environ['SENDGRID_USERNAME']
EMAIL_HOST= 'smtp.sendgrid.net'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = os.environ['SENDGRID_PASSWORD']
Edit: changed EMAIL_PASSWORD to EMAIL_HOST_PASSWORD as that's the correct spelling.
In the intervening 10 years, the above answer has become obsolete. Sendgrid now uses an API key.
https://docs.sendgrid.com/for-developers/sending-email/django
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True