What is causing SMTPAuthenticationError - django

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

Related

ConnectionRefusedError: while trying send email via django

I have a simple django app. So i tried to test my send-mail function. I have allowed my mail to be used for third-party applications. Everything looks pretty good but again and again i catch same error
settings.configure()
def send_email(email=None):
send_mail(
'Subject here',
'Here is the message.',
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=['georgdavidov2#gmail.com'],
fail_silently=False
)
so i got an error message
Traceback (most recent call last):
File "C:\DEV\FindHero\backend\find_hero\services\send_email.py", line 17, in <module>
send_email()
File "C:\DEV\FindHero\backend\find_hero\services\send_email.py", line 8, in send_email
send_mail(
File "C:\Program Files\Python311\Lib\site-packages\django\core\mail\__init__.py", line 87, in send_mail
return mail.send()
^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\site-packages\django\core\mail\message.py", line 298, in send
return self.get_connection(fail_silently).send_messages([self])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\site-packages\django\core\mail\backends\smtp.py", line 124, in send_messages
new_conn_created = self.open()
^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\site-packages\django\core\mail\backends\smtp.py", line 80, in open
self.connection = self.connection_class(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\socket.py", line 850, in create_connection
raise exceptions[0]
File "C:\Program Files\Python311\Lib\socket.py", line 835, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061]
Hee is my django settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.yandex.ru'
EMAIL_PORT = 465
EMAIL_HOST_USER = "emaily#yandex.ru"
EMAIL_HOST_PASSWORD = "password"
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
Then i just call my func right there using send_mail() and tried to call it in python manage.py shell without settings.configure()
UPD
im using special password created by yandex and using 2 factor auth.
You should activate 2-Step Verification in your email and use app passwords instead of your general email password.

Django smtp connection with office 365 Outlook

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 :)

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.

Django email not sending

I am trying to send email through Django as part of django-userena, but I am not able to get email to send at all. In my settings, I have:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myuser#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
I try to send an email from the Django console with:
from django.core.mail import EmailMessage
email = EmailMessage('Mail Test', 'This is a test', to=['otheruser#gmail.com'])
email.send()
It hangs on the send command and doesn't actually send the email. If I stop the command, I get this traceback:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 274, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 87, in send_messages
new_conn_created = self.open()
File "/home/myuser/Copy/Projects/Programming/myproject/venv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 48, in open
local_hostname=DNS_NAME.get_fqdn())
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 356, in getreply
line = self.file.readline()
File "/usr/lib/python2.7/socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
Any help on why this isn't going through?
I had this same problem. I am using Django 1.6. It turns out I needed to use SSL to send email via gmail. So I used this handy package: https://github.com/bancek/django-smtp-ssl
$ pip install django-smtp-ssl
Then settings.py should have this:
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'email#gmail.com'
EMAIL_HOST_PASSWORD = 'YOUR_PASSWORD'
Of course, if you are using Django 1.7 then you can just add EMAIL_USE_SSL = True to settings.py and use the default backend.

Configuring Django/Pinax Email

I'm trying to configure automated emails for my Pinax app.
In the shell (python manage.py shell) I am doing the following to test my email settings:
>>> send_mail ('Test','Test','myemail#myemail.com',['myemail#myemail.com'])
However I am getting the follow traceback:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/__init__.py", line 61, in send_mail
connection=connection).send()
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/message.py", line 175, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 78, in send_messages
new_conn_created = self.open()
File "/Users/myname/Desktop/myenv2/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 41, in open
local_hostname=DNS_NAME.get_fqdn())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 60] Operation timed out
My email settings in settings.py are as follow:
# These are the email settings for sending out confirmation emails, alerts,etc.
DEFAULT_FROM_EMAIL = 'Mysite <myemail#myemal.com>'
EMAIL_USE_TLS = True
EMAIL_HOST = 'box401.bluehost.com'
EMAIL_HOST_USER = 'myemail#myemal.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
EMAIL_SUBJECT_PREFIX = '[Your Site] '
Any insight into what could be wrong? I checked my password and the settings with the host and they seem fine.