Django smtp connection with office 365 Outlook - django

We are receiving the following error using the Office 365 SMTP server to send mails from the Django application.
Traceback (most recent call last):
File "/home/app/views.py", line 598, in send_mail
django.core.mail.send_mail(
File "/usr/local/lib/python3.8/site-packages/django/core/mail/__init__.py", line 61, in send_mail
return mail.send()
File "/usr/local/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 69, in open
self.connection.login(self.username, self.password)
File "/usr/local/lib/python3.8/smtplib.py", line 732, in login
raise last_exception
File "/usr/local/lib/python3.8/smtplib.py", line 721, in login
(code, resp) = self.auth(
File "/usr/local/lib/python3.8/smtplib.py", line 644, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, b'5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Mailbox. Visit https://aka.ms/smtp_auth_disabled for more information. [PN2PR01CA0172.INDPRD01.PROD.OUTLOOK.COM]')
following setup did in the Django setting.py
EMAIL_HOST=smtp.office365.com
EMAIL_USE_TLS=True
EMAIL_PORT=587 # also tried port 25
EMAIL_USER=test#example.com
EMAIL_PASSWORD=test
And following the steps for setting up the Outlook 365 SMTP relay account (Steps)
and also checked the Authenticated SMTP for the admin account. Is I am doing anything wrong or missing anything?
It will be appreciated for any help, Thank you :)

Related

Send email with django + gunicorn + nginx get timeout

I'm trying to deploy a Django project in AWS with gunicorn + nginx but I'm getting a timeout error. My email backend uses AWS SES with SMTP authentication.
The first 2 emails are sent normally, after these I'm getting this error.
The account is already an unblocked restriction for sending an email.
Local django development server works fine, it sends emails normally. In local virtual machine with same EC2 specs sends emails normally.
Running in django shell I'm getting this log at third email sent:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 5, in sendE
File "/home/ubuntu/myproj/share/forms.py", line 23, in share_email
email_message.send()
File "/home/ubuntu/.local/share/virtualenvs/myproj-rqmaW6Yv/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/ubuntu/.local/share/virtualenvs/myproj-rqmaW6Yv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/ubuntu/.local/share/virtualenvs/myproj-rqmaW6Yv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/usr/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/usr/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
```
Any idea?
Ok after testing a lot, I Found EC2 instances have a restriction over default smtp port connection. Change the port from 25 to 587 and it works perfectly.
More info: https://aws.amazon.com/es/premiumsupport/knowledge-center/smtp-connectivity-timeout-issues-ses/

What is causing SMTPAuthenticationError

I just deployed my application to an ubuntu server. I have ssl, so I made the respective modifications in my settings.py, it looks like this:
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'email#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_SSL = True
I believe this is the Right configuration but when a email is sent I get something like:
Traceback (most recent call last):
File "/home/admin/Mefid/TransversalScripts/comunication.py", line 122,
in send_email
fail_silently=False, html_message=html_message)
File "/home/admin/Mefid/venv2/lib/python3.7/site-
packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/home/admin/Mefid/venv2/lib/python3.7/site-
packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/admin/Mefid/venv2/lib/python3.7/site- packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/admin/Mefid/venv2/lib/python3.7/site-packages/django_smtp_ssl.py", line 14, in open
self.connection.login(self.username, self.password)
File "/usr/lib/python3.7/smtplib.py", line 730, in login
raise last_exception
File "/usr/lib/python3.7/smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.7/smtplib.py", line 642, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvc\n5.7.14 YlL_uB0GK_TLI557gRMyVYqHRr9iVrfj5mJ3jaEuyHjSmmevxT5-8ocIMMRh0BVOPqnrCR\n5.7.14 prX-jJCduiyPcF2RrYAWJkg4A5jUboKUMLzBEyz72VWQLPYiOKxFcZ5SBvrICb> Please\n5.7.14 log in via your web browser and then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 i11sm7651758oia.9 - gsmtp')
'''
I followed a guide here https://github.com/bancek/django-smtp-ssl is there anything else I should be aware of? Thanks for your help.
Make sure you have turned on Less secure app access in Google account
Go to your Google Account settings, find Security -> Account permissions -> Access for less secure apps, enable this option.
About this option: https://support.google.com/accounts/answer/6010255

Weblate setup SES email for password reset

I tried to set up a Weblate instance on AWS to send password reset emails. I followeed the following steps:
pip install boto
pip install django-ses
sudo vim /opt/bitnami/apps/django/lib/python3.7/site-packages/Django-2.1.4-py3.7.egg/django/conf/global_settings.py
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = 'xxxx'
AWS_SECRET_ACCESS_KEY = 'xxx'
However it still doesn't work properly and I'm not getting any errors. my AWS key has a full permission to SES.
Update:
If i run send_mail from ./manage.py shell I get this output:
>>> send_mail('Subject here', 'Here is the message.', 'xx#gmail.com',['xx#gmail.com'], fail_silently=False)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/message.py", line 291, in send
return self.get_connection(fail_silently).send_messages([self])
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/opt/bitnami/apps/weblate/venv/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/opt/bitnami/python/lib/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/opt/bitnami/python/lib/python3.7/socket.py", line 727, in create_connection
raise err
File "/opt/bitnami/python/lib/python3.7/socket.py", line 716, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
You're editing Django default settings, which is weird way to change configuration, but it seems that Bitnami is recommending this approach. It seems the setting is not applied properly as the test command still uses default SMTP backend.
Things to check:
is setting actually applied: ./manage.py shell -c 'from django.conf import settings; print(settings.EMAIL_BACKEND)'
was django-ses installed into system? better run the installation as root sudo pip install django-ses
aren't you mixing Python 2 and Python 3 environments (not sure how this is done in Bitnami images)

scheduling an async email job fails with error SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')

I'm trying to schedule an asynchronous email job.
I'm using django-rq as the queueing mechanism. I've tried numerous fixes such as changing the password, ensuring that it's correct etc. I can manually sending SMTP via REPL.
Update #1: the settings are being being picked up correctly by the worker as far as I can ascertain. The job correctly picks up the correct value for settings.DEFAULT_FROM_EMAIL and has the correct email address set as an arg for send_mail.
The following code works successfully (no django-rq):
send_mail(
newClaim.linkedOffer.commsPromoHeadline,
msg_plain,
settings.DEFAULT_FROM_EMAIL,
[newRecipient.email],
html_message=msg_html,
)
whilst the following code generates an SMTP authentication error (traceback lower down):
django_rq.enqueue(
send_mail,
newClaim.linkedOffer.commsPromoHeadline,
msg_plain,
settings.DEFAULT_FROM_EMAIL,
[newRecipient.email],
html_message=msg_html,
)
traceback:
Traceback (most recent call last):
File "/home/user1/webapps/dev_django_platform/ENV/lib/python2.7/site-packages/rq/worker.py", line 568, in perform_job
rv = job.perform()
File "/home/user1/webapps/dev_django_platform/ENV/lib/python2.7/site-packages/rq/job.py", line 495, in perform
self._result = self.func(*self.args, **self.kwargs)
File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/message.py", line 286, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/backends/smtp.py", line 92, in send_messages
new_conn_created = self.open()
File "/home/user1/webapps/dev_django_platform/lib/python2.7/Django-1.7.7-py2.7.egg/django/core/mail/backends/smtp.py", line 59, in open
self.connection.login(self.username, self.password)
File "/usr/local/lib/python2.7/smtplib.py", line 622, in login
raise SMTPAuthenticationError(code, resp)
SMTPAuthenticationError: (535, '5.7.3 Authentication unsuccessful')
Why is is throwing an exception and how can I fix it?
My assumption is the enqueued task is run on a separate worker environment. Inside Django, the send_mail function knows where to look up the Auth details from. But inside the worker, it can not find them, since it's a separate process.
You need to write your custom send_mail function which does not depend on Django to provide it's settings.

django mail webform not connection to mail server

I spent a lot of time developing the form and connecting all the pieces together and now I cannot connect to my smtp server through django settings. Here are my settings...
DEFAULT_FROM_EMAIL = 'auto#domain.com'
EMAIL_HOST = 'smtp.mailhost.com'
EMAIL_HOST_USER = 'auto#domain.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_PORT = 465
I tried to send it through my form and it didn't work so I tried to send one through the shell..
from django.core.mail import send_mail
send_mail('subject','message','auto#domain.com', ['me#domain.com'], fail_silently=False)
and I get this traceback...
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 286, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 92, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 50, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 312, in connect
(code, msg) = self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 363, in getreply
raise SMTPServerDisconnected("Connection unexpectedly
closed")
SMTPServerDisconnected: Connection unexpectedly closed
You should recheck the data you use: login, password, etc.
If it doesn't help, try to use port 587.
SMTP uses this one. I hope it will help.
495 is now deprecated.