Django 2 Sendmail with timeweb.ru - python

I checked Django app in Ubuntu 18.04 without ufw
When added to settings.py these:
EMAIL_HOST = 'smtp.timeweb.ru'
EMAIL_PORT = 465
EMAIL_HOST_USER = 'myemail#timeweb.ru'
DEFAULT_FROM_EMAIL = 'myemail#timeweb.ru'
SERVER_EMAIL = 'myemail#timeweb.ru'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_USE_TLS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Run in the console
python manage.py shell
>>> from django.core.mail import send_mail
>>> send_mail('ddd','fffffffffff', 'myemail#timeweb.ru',['test#gmail.com'],fail_silently=False)
After 7 minutes I get this error.
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/mikhail/Django/Chapter01/mysite/env/lib/python3.7/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/home/mikhail/Django/Chapter01/mysite/env/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/mikhail/Django/Chapter01/mysite/env/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/mikhail/Django/Chapter01/mysite/env/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 338, in connect
(code, msg) = self.getreply()
File "/usr/lib/python3.7/smtplib.py", line 394, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
What am I doing wrong?

Related

ConnectionRefusedError while sending email through gmail using Django

I am trying to send e-mails through my Gmail account for my Django application. I'm using below configurations in settings.py file.
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST_USER = 'username#gmail.com'
EMAIL_HOST = 'smtp.gmail.com' # tried smtp-relay.gmail.com also
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = 'Gmail password / app password'
Import statements in this regard:
from django.core.mail import send_mail, EmailMessage
from django.conf import settings
Following is in my views function:
message = EmailMessage(subject=email_subject, body=email_message, from_email=settings.EMAIL_HOST_USER, to=[email, 'username#gmail.com'])
message.send(fail_silently=False)
I have tried this also:
send_mail(email_subject, email_message , settings.EMAIL_HOST_USER, [email, 'username#gmail.com'] , fail_silently=False, )
Any help is much appreciated. Please note that I have tried:
enabling less secure apps button in Gmail
Then, I disabled less secure apps, enabled 2 factor authentication and use app passwords option of Google.
Here is the complete traceback:
Traceback (most recent call last):
File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/jatinsinghbhati/Documents/workspaces/pollsite/signup/views.py", line 40, in index
message.send(fail_silently=False)
File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/mail/message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "/Users/jatinsinghbhati/Documents/workspaces/djangoenv/lib/python3.9/site-packages/django/core/mail/backends/smtp.py", line 62, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/smtplib.py", line 310, in _get_socket
return socket.create_connection((host, port), timeout,
File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/socket.py", line 843, in create_connection
raise err
File "/Users/jatinsinghbhati/.pyenv/versions/3.9.0/lib/python3.9/socket.py", line 831, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused
[25/Feb/2021 19:38:20] "POST /signup/ HTTP/1.1" 500 101708
It should be
EMAIL_PORT = 587
But not
EMAIL_PORT = '587'
PORT number is an integer not a string.

Sending email in Django throws Address family not supported by protocol exception

I'm sending emails using Django version 2.2 and have tested it on the local machine and it works. But the same code throws [Errno 97] Address family not supported by protocol exception.
I also couldn't find any solution online.
I have rechecked my code and there is nothing wrong in it.
I'm using google smtp server with app_password.
Below are Details.
settings.py file
EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "*******#gmail.com"
EMAIL_HOST_PASSWORD = "app_password"
Email Sending Function
subject = 'Account Password reset mail'
ctx = {
"user" : self.user,
"website" : settings.WEBSITE_NAME,
"site_url" : settings.WEBSITE_URL,
}
msg = render_to_string('login/forgot_password_email.html',ctx)
from_email = settings.WEBSITE_ADMIN_EMAIL
to_email = [self.cleaned_data.get('email'),]
result = send_mail(subject, '', from_email, to_email, fail_silently=False, html_message=msg)
return result
I even try to login to server through SSH and execute through the shell but returns the same error.
>>> from django.core.mail import send_mail
>>> res = send_mail('Subject here', 'Here is the message.', 'example#gmail.com', ['example#gmail.com'], fail_silently=False,)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/lib/python3.7/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/home/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/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/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/lib/python3.7/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/home/y6d62pncrpga/virtualenv/public_html/django/app2/3.7/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/alt/python37/lib64/python3.7/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/opt/alt/python37/lib64/python3.7/smtplib.py", line 307, in _get_socket
self.source_address)
File "/opt/alt/python37/lib64/python3.7/socket.py", line 727, in create_connection
raise err
File "/opt/alt/python37/lib64/python3.7/socket.py", line 711, in create_connection
sock = socket(af, socktype, proto)
File "/opt/alt/python37/lib64/python3.7/socket.py", line 151, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
OSError: [Errno 97] Address family not supported by protocol
Thanks in Advance.

smtplib.SMTPConnectError: (451, b'Request action aborted on MFE proxy, SMTP server is not available.')

Please i have been facing this error for the past three days , i have already checked all answers and i have made appropriate corrections, allowed less secure apps, off 2 factor authentication, cross checked username/password
But non of these help
settings.py code
SERVER_EMAIL = 'infopython#gmail.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_HOST_USER = SERVER_EMAIL
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
ADMINS = [
('admin', 'admin5#gmail.com'),
]
MANAGERS = ADMINS
Output
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 335, in execute
output = self.handle(*args, **options)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\sendtestemail.py", line 33, in handle
recipient_list=kwargs['email'],
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\__init__.py", line 60, in send_mail
return mail.send()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\message.py", line 294, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "C:\Users\LENOVO\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 254, in __init__
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (451, b'Request action aborted on MFE proxy, SMTP server is not available.')
I got this error when mail port was set to 25
EMAIL_PORT = 25
I had to change the port for it to work for me. Do check if your email host provides other ports and try any other available ports.
For me, changing the email port from 25 to 587 worked.
EMAIL_PORT = 587

Django-registration "gaierror at /accounts/register/ [Errno -2] Name or service not known"

I am trying to implement django-registration, but I get a gaierror on the registration page with the following traceback:
Internal Server Error: /accounts/register/
Traceback (most recent call last):
#irlevant
File "/usr/local/lib/python2.7/dist-packages/registration/views.py", line 36, in form_valid
new_user = self.register(form)
File "/usr/local/lib/python2.7/dist-packages/registration/backends/model_activation/views.py", line 29, in register
site=get_current_site(self.request)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py", line 184, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/registration/models.py", line 97, in create_inactive_user
registration_profile.send_activation_email(site)
File "/usr/local/lib/python2.7/dist-packages/registration/models.py", line 182, in send_activation_email
self.user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 363, in email_user
send_mail(subject, message, from_email, [self.email], **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.py", line 61, in send_mail
return mail.send()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/message.py", line 292, 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 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known
Here is my settings.py:
DEBUG = True
ALLOWED_HOSTS = ['*']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '********#gmail.com'
EMAIL_HOST_PASSWORD = '*******'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
ACCOUNT_ACTIVATION_DAYS = 1
REGISTRATION_AUTO_LOGIN = True
SITE_ID = 1
LOGIN_REDIRECT_URL = '/'
Where should i check to find the mistake?
I faces the same problem, it was just an unstable internet connection because the problem disappeared as I changed my connection.

Django send_mail and Exchange server

Currently I have an Exchange server running and am attempting to run send_mail, though get a variety of errors.
Here is my current settings.py
EMAIL_HOST = 'exchange.domain.com'
EMAIL_HOST_USER = 'DOMAIN\user'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 443
EMAIL_USE_TLS = True
and when I run send_mail('''Subject''', '''Message.''', 'myemail#domain.com', ['otheremail#domain.com',], fail_silently=False)
after a very long wait
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
connection=connection).send()
File "/Library/Python/2.7/site-packages/django/core/mail/message.py", line 255, in send
return self.get_connection(fail_silently).send_messages([self])
File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 88, in send_messages
new_conn_created = self.open()
File "/Library/Python/2.7/site-packages/django/core/mail/backends/smtp.py", line 49, in open
local_hostname=DNS_NAME.get_fqdn())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 311, in connect
(code, msg) = self.getreply()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 359, in getreply
+ str(e))
SMTPServerDisconnected: Connection unexpectedly closed: [Errno 54] Connection reset by peer

Categories