Sending SMTP email via Django in production environment - django

I am using Django-Registration for my website on a linode ubuntu
virtual. I get connection problem sending activation email when users
sign up.
Here is my settings:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_USE_TLS = True
I've tested locally and it works. I tested interactively via python
manage.py shell on the host and it works when I logged in as root. I
think there is a problem with permission since Apache is running under
www-data. Do you have any ideas how to fix this?
EDIT:
Someone on Linode IRC suggests that I install ssmtp package...I followed the instruction found here http://tombuntu.com/index.php/2008/10/21/sending-email-from-your-system-with-ssmtp/ and reboot the server...everything works great now.
Thank you

Try setting
EMAIL_USE_TLS = 1
rather than EMAIL_USE_TLS = True. That's my guess. If that doesn't work, try
EMAIL_DEBUG = False
although honestly I'm not sure what that one does.

Related

I'm not receiving the password reset email link when I click on the Reset password in my Blogging Web App built using Django Framework

I'm not receiving the password reset email link when I click on the Reset password in my Blogging Web App built using Django Framework, although the info "An email has been sent with instructions to reset your password." is prompted as well.
I'm guessing something with my setting USER_HOST_EMAIL and USER_HOST_PASSWORD in the environment variables.
The settings.py in my Django setup looks like this:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = os.environ.get('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_HOST_PASSWORD')
Any ideas on what could possibly be the problem?
I use EMAIL_PORT = 465, try changing it, If not, show how you are leaving your URl so I can try to help you better
I don't think the problem was with the URI or the port number since I happened to change the HOST email address to the one with no two-factor authentication. That apparently worked without any other changes needed!

Email Sends In Development But Not In Production

I am able to send these automated emails through Dajngo while in my production environment, though in development I get the error
[Errno 101] Network is unreachable
Exception Location: /opt/alt/python38/lib64/python3.8/socket.py in create_connection, line 796
My settings
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'email.email#email'
EMAIL_HOST_PASSWORD = 'password'
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
I think the connection is being refused by Outlook, however it's accepted in my development environment (I used powershell to enable SMTP AUTH).
PS - I have tried different port numbers.
Any help would be greatly appreaciated.
Thank you.
To anyone having similar difficulties. The problem was that A2 Hosting doesn't allow smtp. I am currently working on finding a workaround. Il update this once i figure it out. If i forget and your stuck, drop me a msg and il tell u how i end up fixing it.

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'

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...

Not able to send email - Django

My settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'rakil#gmail.com'
EMAIL_HOST_PASSWORD = '*******'
DEFAULT_FROM_EMAIL = 'testing#testing.com
But a mail is not sent to the address, in the console the print.html is printing when I click on send_email, but it's not sending any email.
I am using Django 1.3.7 and Python 2.6.
I don't know the problem is with the version or some logic problem.
In settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
console.EmailBackend will print the mail in the console. So using
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
solved my problem. It is already documented here: Django docs: Email
I personally just switched to my production server when this happened. Because this is a new IP location, Google attempted to protect my account by blocking the sign-in.
To fix this, I followed the steps in this answer:
First, enable access to less secure apps in your Google account here: https://www.google.com/settings/security/lesssecureapps
Since I had already done this, and my problem was now because of a new IP address, I had to manually confirm the next sign-in attempt using this link: https://accounts.google.com/DisplayUnlockCaptcha
See the linked answer for more information.