I'm trying to send an actual email to myself from my website using send_mail. I had used localhost and the following cmd command,
python -m smtpd -n -c DebuggingServer localhost:1025
in order to test it. It intercepts with no problem, but I don't see anything in my inbox.
My email provider works with SSL, so I turned it to True and enabled 'access to the mailbox using mail clients' in my mail settings.
Here is the settings.py file:
EMAIL_HOST = '178.68.164.41' # My current ip address
EMAIL_PORT = '465' # Port of the email host
EMAIL_HOST_USER = 's...6#rambler.ru' # Here is my actual email
EMAIL_HOST_PASSWORD = '.....' # Here is my actual password from my email login
EMAIL_USE_TLS = False
EMAIL_USE_SSL = True
Here is the views.py file:
from django.shortcuts import render
from django.core.mail import send_mail
from .forms import ContactForm
def contact(request):
form = ContactForm
if request.method == 'POST':
message_name = request.POST.get('name')
message_email = request.POST.get('email')
message = request.POST.get('message')
send_mail(message_name, message, message_email, ['s****6#rambler.ru'])
return render(request, 'contact.html', {'form': form})
Here's the exception:
Internal Server Error: /contact/
Traceback (most recent call last):
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\1\PycharmProjects\StasProject\sales_project\contact\views.py", line 12, in contact
send_mail(message_name, message, message_email, ['sta6006#rambler.ru'])
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
return mail.send()
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\message.py", line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 1043, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\smtplib.py", line 1051, in _get_socket
new_socket = self.context.wrap_socket(new_socket,
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\1\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
Exception Type: ConnectionResetError at /contact/
Exception Value: [WinError 10054] Remote host forcibly terminated existing connection
Related
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.
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.
I'm trying to set up self-serve password resets on a django setup. I have configured it to use the in-built password reset system, and have tested everything on my dev machine, and works fine. However, in production (i.e. actually sending emails), when I try to get a password reset email, it throws an SMTPServerDisconnected error:
[22/Sep/2017 15:04:59] ERROR [django.request:256] Internal Server Error: /password_reset/
Traceback (most recent call last):
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 145, in inner
return func(*args, **kwargs)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 110, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/contrib/auth/views.py", line 182, in password_reset
form.save(**opts)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 256, in save
html_email_template_name=html_email_template_name)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 213, in send_mail
email_message.send()
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 107, in send_messages
sent = self._send(message)
File "/srv/virtualenvs/sis_esv/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 123, in _send
self.connection.sendmail(from_email, recipients, message.as_bytes(linesep='\r\n'))
File "/usr/lib/python2.7/smtplib.py", line 735, in sendmail
self.rset()
File "/usr/lib/python2.7/smtplib.py", line 469, in rset
return self.docmd("rset")
File "/usr/lib/python2.7/smtplib.py", line 394, in docmd
return self.getreply()
File "/usr/lib/python2.7/smtplib.py", line 368, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
SMTPServerDisconnected: Connection unexpectedly closed
I'm using G-Suite (Google) as the SMTP relay; the settings I'm using are:
EMAIL_HOST = 'smtp-relay.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
The weird thing is I can send emails fine in other places within the system, using django's django.core.mail.send_mail() function.
The problem turned out to be that I hadn't set DEFAULT_FROM_EMAIL, thus django was attempting to send from a localhost email address, which Google's SMTP servers silently rejected. In other uses for send_mail() I had specified the from address, so this didn't appear as an issue there.
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.
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