I use Django and every time when I try to send an email I get this response
Internal Server Error: /order/
Traceback (most recent call last):
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\generic\base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Daniil\Desktop\Admin\py\qazpoligrah1\main\views.py", line 53, in post
email.send()
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\mail\message.py", line 298, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\mail\backends\smtp.py", line 124, in send_messages
new_conn_created = self.open()
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\mail\backends\smtp.py", line 91, in open
self.connection.login(self.username, self.password)
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 739, in login
(code, resp) = self.auth(
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 642, in auth
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 432, in docmd
return self.getreply()
File "C:\Users\Daniil\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 405, in getreply
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
my settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.mail.yahoo.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'email#yahoo.com'
EMAIL_HOST_PASSWORD = 'password'
my views.py
def post(self,request):
print(request.POST.get('mail'), request.POST.get('type'))
prod_type = models.ProductTypes.objects.filter(pk =
request.POST.get('type'))
order_msg = prod_type[0].name
email = EmailMessage('Qazpoligraph', order_msg,
settings.EMAIL_HOST_USER, to= (request.POST.get('mail'),))
email.send()
return redirect('/')
I tried to change the EMAIL_PORT and the EMAIL_HOST but nothing changed
Alright. I've solved my problem. I think it may be helpful for another people who ran into such a type of this problem
The problem was in settings.py
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.yourmail.com'
EMAIL_HOST_USER = 'email'
EMAIL_HOST_PASSWORD = 'password'
I studied smtplib where port is filled automatically. Then I dropted "EMAIL_BACKEND " and after made a decision to drop port in settings. Fortunately it works after that. Finally I have two working scripts which can send email messages(smtplib and django)
Related
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.
I have a simple method (send_activation_email) called from a view which handles user registration by extending default django user auth. It sends an activation email. Now I get the error:
SMTPConnectError at /accounts/register/ - (421, b'Server busy, too
many connections')
the method implementation:
def send_activation_email(user, request):
current_site = get_current_site(request)
subject = 'Activate your membership Account'
message = render_to_string('accounts/account_activation_email.html',{
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
html_message = get_template('accounts/account_activation_html_email.html').render({
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
send_mail(subject=subject,
message=message,
from_email= None,
recipient_list=[user.email],
html_message= html_message
#,fail_silently=False
)
I have never come across this issue before and have not found any useful answers searching on here. Clearly, this has to do with SMTP and but the thing is I have had the same error trying different providers: sengrid, zoho, outlook. And definitely, the email goes through on console.
The logs:
Internal Server Error: /accounts/register/
Traceback (most recent call last):
File "C:\Users\[**]\membership\myEnv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\[**]\membership\myEnv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\[**]\membership\accounts\views.py", line 70, in register
send_activation_email(user, request)
File "C:\Users\[**]\membership\accounts\views.py", line 217, in send_activation_email send_mail(subject=subject,
File "C:\Users\[**]\membership\myEnv\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
return mail.send()
File "C:\Users\[**]\membership\myEnv\lib\site-packages\django\core\mail\message.py",
line 284, in send
return self.get_connection(fail_silently).send_messages([self])
File "C:\Users\[**]\membership\myEnv\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "C:\Users\[**]\membership\myEnv\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:\Program Files\Python310\lib\smtplib.py", line 258, in __init__
raise SMTPConnectError(code, msg)
smtplib.SMTPConnectError: (421, b'Server busy, too many connections')
I would deeply appreciate any help I can get. Even if it's just about where to look. Thanks
And yes email config:
somewhere in settings.py -
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_HOST_USER = env('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD')
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = env('EMAIL_HOST_USER')
I have Used Django EmailMessage function . Here is views.py file.
def send_bill_mail(request):
context_dict={
"name": "abc"
}
html_message = render_to_string('home/email_templates/bill_generation.html', context_dict)
try:
email = EmailMessage(
' Welcome to store',
html_message,
to=['abc#gmail.com']
)
email.content_subtype = "html"
email.send()
except Exception as e:
messages.error(request,"Something Went Wrong !")
return redirect('home:dashboard')
Here Is settings.py
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'xyz#gmail.com'
EMAIL_HOST_PASSWORD = 'xyz'
EMAIL_PORT = 587
Following Exception occured during processing of views.
Exception happened during processing of request from ('127.0.0.1', 50522)
Traceback (most recent call last):
File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 650, in process_request_thread
self.finish_request(request, client_address)
File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 360, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 720, in __init__
self.handle()
File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 174, in handle
self.handle_one_request()
File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "C:\Users\sanke\AppData\Local\Programs\Python\Python37\lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
This issue generally happens when you run project locally , may be you should try hosting it somewhere on internet
or
Using connections can solve your problem like below
def send_bill_mail(request):
context_dict={
"name": "abc"
}
html_message = render_to_string('home/email_templates/bill_generation.html', context_dict)
email_list = []
try:
email = EmailMessage(
' Welcome to store',
html_message,
to=['abc#gmail.com']
)
email.content_subtype = "html"
email_list.append(email)
connection = mail.get_connection()
connection.send_messages(email_list)
except Exception as e:
messages.error(request,"Something Went Wrong !")
return redirect('home:dashboard')
I have a Django application and when I run it locally it all works fine. But in production that runs in a docker container it can not send mail anymore I get the error
Traceback (most recent call last):
File "/app/training/schema.py", line 167, in mutate
fail_silently=False,
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/mail/__init__.py", line 60, in send_mail
return mail.send()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/mail/message.py", line 294, in send
return self.get_connection(fail_silently).send_messages([self])
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 103, in send_messages
new_conn_created = self.open()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/mail/backends/smtp.py", line 63, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/app/.heroku/python/lib/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/app/.heroku/python/lib/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/app/.heroku/python/lib/python3.6/smtplib.py", line 307, in _get_socket
self.source_address)
File "/app/.heroku/python/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/app/.heroku/python/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
I am using these settings:
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'me#gmail.com'
EMAIL_HOST_PASSWORD = 'password'
EMAIL_PORT = 587
Does someone know what I am doing wrong?
I would check connectivity status to the destination host smtp.gmail.com at port 587 from your production host.
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.