I already setup the email. It was working perfectly but after sometime it is not working and sending the mail. I don't know where the problem was created. Can someone help me out with this??
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'myemail'
EMAIL_HOST_PASSWORD = 'mypass'
this is my email function code:
def my_email():
order = Order.objects.filter(createdAt__gt=Now()-timedelta(minutes=1))
p = str(settings.BASE_DIR)
with open(p + '/templates/email.html') as f:
order_message = f.read()
for o in order:
print(o._id)
email = EmailMultiAlternatives(subject='Thank you', body=order_message, from_email='laksura.com.bd#gmail.com', to=['sohanur.shanto#northsouth.edu'] )
html_template = get_template('email.html').render()
html_template = render_to_string('email.html', {'name': o.user, 'order_id': o._id, 'total': o.totalPrice, 'created': o.createdAt})
email.attach_alternative(html_template, "text/html")
email.send()
I am getting this error
new_conn_created = self.open()
File "C:\Python39\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:\Python39\lib\smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python39\lib\smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python39\lib\smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "C:\Python39\lib\socket.py", line 843, in create_connection
raise err
File "C:\Python39\lib\socket.py", line 831, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Gmail blocks the use of Gmail by less secure apps by default. You will have to change this setting if you have to use Django-mail.
If you don't change this setting, Gmail won't allow your account to send mail via your Django web app.
Related
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)
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 have problem in sending mail in Django
I set my gmail following this link:
https://support.google.com/mail/answer/7126229?visit_id=1-636278779262945155-948643181&rd=1#cantsignin
And i tried every solution online but still get [Errno -2]
I found someone said it is because the DNS problem ,can some one tell me what is wrong with my code and is there any solution ?
views.py
import django
from django import settings
from django.core.mail import send_mail
def contact(request):
send_mail('subject','message',settings.EMAIL_HOST_USER,['zwt467875460#gmail.com'],fail_silently = False)
return HttpResponseRedirect('/contact/thanks')
def thanks(request):
return HttpResponse('thanks!')
settings.py
#email config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gamil.com'
EMAIL_PORT = 587
EMAIL_HOST_USER='zwt467875460#gmail.com'
EMAIL_HOST_PASSWORD='*********' #my gmail password
EMAIL_USER_TLS = True
DEFAULT_FORM_EMAIL = EMAIL_HOST_USER
ACCOUNT_EMAIL_VERIFICATION = 'none'
traceback error
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/Django-1.10.6-py2.7.egg/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/Django-1.10.6-py2.7.egg/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/Django-1.10.6-py2.7.egg/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/wenting/WTDjango/mysite/books/views.py", line 30, in contact
send_mail('subject','message',settings.EMAIL_HOST_USER,['zwt467875460#gmail.com'],fail_silently = False)
File "/usr/local/lib/python2.7/dist-packages/Django-1.10.6-py2.7.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/usr/local/lib/python2.7/dist-packages/Django-1.10.6-py2.7.egg/django/core/mail/message.py", line 342, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python2.7/dist-packages/Django-1.10.6-py2.7.egg/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-1.10.6-py2.7.egg/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 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno -2] Name or service not known
[15/Apr/2017 18:45:56] "GET /contact/ HTTP/1.1" 500 102295
and in the Django error page:
Exception Location: /usr/lib/python2.7/socket.py in create_connection, line 557
I guess my problem is that Gmail think i dont have a fixed or right DNS
so i cant create a socket
And then i tried sendmail :
after install sendmail ,sendmail-cf,and configure it ,
i tried :
echo "Subject: sendmail test" | sendmail -v zwt467875460#gmail.com
and the output is (i omit the sending details):
354 Enter mail, end with "." on a line by itself
>>> .
050 <zwt467875460#gmail.com>... Connecting to smtp.gmail.com via relay...
050 <zwt467875460#gmail.com>... Deferred: Connection refused by smtp.gmail.com
250 2.0.0 v3GKMtLU003555 Message accepted for deliveryzwt467875460#gmail.com... Sent (v3GKMtLU003555 Message accepted for delivery)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 localhost.localdomain closing connection
The connection is refused by smtp.gmail.com
Check if IMAP/POP are enabled from your Gmail account settings.
Also check that access to 'less secure apps' is enabled .
If not, follow the link :https://support.google.com/accounts/answer/6010255
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.
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.