SMTP connection timeout in Sentry - django

I'm using Sentry to monitor a Django app. I copied the following (correct and tested) email settings from the Django app to my Sentry config file:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.privateemail.com'
EMAIL_HOST_USER = 'info#ookmijnbedrijf.nl'
EMAIL_HOST_PASSWORD = '***'
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_USE_TLS = False
SERVER_EMAIL = EMAIL_HOST_USER
Everything looks right on the SMTP Settings page, but when I try to send a test email I get this:
Connection unexpectedly closed: timed out
My Django app is sending emails correctly with these exact settings. What am I doing wrong?

Turns out Sentry only supports SMTP with TLS (not SSL). Made an issue about this.

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

Failing at sending mail from django

I'm trying to send an "email" from a website in django.
I have completed the main code for doing so:
-)the view function
-)the URLs mapping to make the function reachable from code
-)the sending form at a template
So my sending form would trigger the view function using the path specified in the URLS.
On my server, I have a "postfix" instance installed and tried.
I tried to edit changes in the settings.py and the views.py for about 2 days now but nothing worked.
The errors range between these two
1)
SMTPNotSupportedError at /website/email_send
when settings are
EMAIL_HOST = 'mydomain.com'
EMAIL_PORT = 25 //same for port 587
EMAIL_HOST_USER = 'uname'
EMAIL_HOST_PASSWORD = 'pwd!'
EMAIL_USE_TLS = True
2)
gaierror at /website/email_send
[Errno -2] Name or service not known
when settings are
EMAIL_HOST = 'mail.mydomain.com' or 'smtp.mydomain.com'
EMAIL_PORT = 25 //same for port 587
EMAIL_HOST_USER = 'uname'
EMAIL_HOST_PASSWORD = 'pwd!'
EMAIL_USE_TLS = True
I expect the email to be sent using a form in my django site run on a server using postfix
The problem is now fixed.
I found this question and used its answer:
Postfix + Django: SMTPException: SMTP AUTH extension not supported by server
I removed EMAIL_HOST_USER = 'uname' and EMAIL_HOST_PASSWORD = 'pwd!'
Then it worked without errors. The settings now are:
...
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
EMAIL_USE_TLS = True
...

Send email through Zoho SMTP

I am trying to send email from my django-based website, but I got some problem - SMTPServerDisconnected Connection unexpectedly closed
My setting.py:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'me#mydomain.com'
EMAIL_HOST_PASSWORD = 'XXXXXX'
I am using django 1.5.1, python 2.7.3. Anyone can solve this problem?
Thanks for your help
I'm having the same problem with connection timeouts. It seems to me that there are issues around SSL sockets in the default Django SMTP library. In the development version of Django there is an option to set EMAIL_USE_SSL = True which allows for the use of an implicit TLS connection (as opposed to explicit, which is specified by EMAIL_USE_TLS = True). Generally the former (implicit) uses port 465, while the latter (explicit) uses port 587. See the Django docs -- compare the development version with version 1.5. Note that the option EMAIL_USE_SSL is NOT present in 1.5.
Thus, the problem is that Zoho's default SMTP server uses port 465 and requires SSL, but the EMAIL_USE_TLS option doesn't fulfill this requirement. (Side note: maybe try setting this option to False? I didn't try that.) Anyway, my best guess is that this is a Django-specific issue and may not be solved until 1.7.
As for a solution to your problem: you can definitely access Zoho's SMTP server with Python (2.7.1)'s smtplib (see script below). So, if you want a slightly inelegant fix, you could go that route. I've tested this in Django 1.5.1 and it works like a charm.
Here's the stand-alone Python script (which can be adapted for use in a Django project):
import smtplib
from email.mime.text import MIMEText
# Define to/from
sender = 'sender#example.com'
recipient = 'recipient#example.com'
# Create message
msg = MIMEText("Message text")
msg['Subject'] = "Sent from python"
msg['From'] = sender
msg['To'] = recipient
# Create server object with SSL option
server = smtplib.SMTP_SSL('smtp.zoho.com', 465)
# Perform operations via server
server.login('sender#example.com', 'password')
server.sendmail(sender, [recipient], msg.as_string())
server.quit()
Try checking that the above script runs with your Zoho credentials before plugging it into your web project. Good luck!
In my case I was receiving that:
SMTPServerDisconnected: Connection unexpectedly closed
with these settings:
EMAIL_PORT = 465
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_USE_SSL = True
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'dio#streetbarz.com'
EMAIL_HOST_PASSWORD = 'password'
After setting server.set_debuglevel(1), I discovered that my DEFAULT_FROM_EMAIL was different from EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = "dio#streetbarz.com"
Adding that fixed the problem.
B.Welsh's answer doesn't solve the problem if you want to get error reports by email.
So for anyone who needs it:
The port for Zoho's TLS is 587 as defined in their SMTP Server Configuration Page .
That page also points out that you can't use a "from" different than the email you're using, otherwise it won't go through.
There's the code you need in settings.py to get the error report by email working:
DEBUG = False
TEMPLATE_DEBUG = DEBUG
ADMINS = (('Yourname', 'youremail#yourdomain.com'),)
SERVER_EMAIL = constants.SENDER_EMAIL
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = constants.SENDER_EMAIL
EMAIL_HOST_PASSWORD = constants.EMAIL_PASSWORD
I've got a way to send using django 1.6.8. First, you have to install the django-smtp-ssl available in the GitHub. Run the code:
pip install django-smtp-ssl
and add following to your settings.py:
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'mail.example.com'
EMAIL_PORT = 465
See the link https://github.com/bancek/django-smtp-ssl
SMTP Configuration settings for Zoho Mail - TLS use port 587 and for ssl 465. so
useEMAIL_PORT = 587 if you use EMAIL_USE_TLS = True
I found that Zoho does not like a standard django.core.mail.send_mail approach. The issue appears to be related to the Content-type. There are multiple ways you could work around this, my approach was to switch to EmailMessage which has a richer interface and allows you to pass the Content-type in a header.
Switching from this:
from django.core import mail
mail.send_mail(subject='Hello',
message='Body goes here',
from_email='user#example.com',
recipient_list=['user#example.com'])
to this:
from django.core.mail import EmailMessage
email = EmailMessage(
subject='Hello',
body='Body goes here',
from_email='user#example.com',
to=['user#example.com'],
reply_to=['user#example.com'],
headers={'Content-Type': 'text/plain'},
)
email.send()
Other Zoho mail settings:
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'user#example.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
This solved my issues with Zoho mail sending and is compatible with other queuing plugins like django-yubin.
A little bit unrelated to the question, but please note that Zoho Mail does not offer IMAP/POP support anymore with their free plan. Hopefully, I can save some of you debugging time with this post.
```
FREE PLAN
Up to 25 Users
5GB* /User, 25MB Attachment Limit
Webmail access only+. Single domain hosting.
```
+IMAP/POP Support Available exclusively with the paid plans.
https://www.zoho.com/workplace/pricing.html?src=zmail
Old free plans (registered before 2018???) seem to still have IMAP/POP Support Available
Source: https://help.zoho.com/portal/community/topic/zoho-free-tier-pop-imap-activesync-no-longer-free
According to the discussion on this link, we also need to check the correct smtp url.
In my case i was using smtp.zoho.com, however the correct choice was smtp.zoho.in. Hope that helps. You can find that after logging in to zoho and checking the domain url.
Try 1 instead of True:
EMAIL_USE_TLS = 1
EMAIL_PORT = 465
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'me#mydomain.com'
EMAIL_HOST_PASSWORD = 'XXXXXX'
alternatively try an alternate port:
EMAIL_USE_TLS = 1
EMAIL_PORT = 587
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = 'me#mydomain.com'
EMAIL_HOST_PASSWORD = 'XXXXXX'
Your stmp email backend class might be old. Goto
python/site-packages/django/core/mail/stmp.py
file and make sure you have USE_SSL as an option. If not, simply replace the whole file with one that does. Here you go. Worked for me with ZOHO.
stmp.py file
Excuse the poor formatting of this response, it's my first contribution to SO...

Need clarification in Django mail sending scenario

I build a mail sending function in django.I did it by referring django Doc .
In doc to specify a email backend,need to add this line in settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
settings.py for email
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'anjunair#gmail.com'
EMAIL_HOST_PASSWORD = '*********'
DEFAULT_FROM_EMAIL = 'quartentine#name.com'
Mail is sending to the email if the EMAIL_BACKEND removed from settings.Need some clarification about this.
Thanks
The docs don't tell you to do that. That console email backend is specifically to NOT use SMTP - it outputs directly to the console.
Remove it, and keep it that way if you want to send via smtp (gmail).

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