I am try to send email in my django project using Celery and RabbitMQ.
Here is my project on GitHub https://github.com/Loctarogar/Django-by-Example.
In my tasks.py file typed admin#myshop.com but i tried change it to my real google mail and that have no effects.
Every time I have this Error:
[2017-11-20 20:09:36,747: ERROR/ForkPoolWorker-4] Task orders.tasks.order_created[a8fe13ed-08dc-4971-82ff-f184e197ab8d] raised unexpected: ConnectionRefusedError(111, 'Connection refused')
Traceback (most recent call last):
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/app/trace.py", line 374, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/morilon/dj/shop/lib/python3.5/site-packages/celery/app/trace.py", line 629, in __protected_call__
return self.run(*args, **kwargs)
File "/home/morilon/dj/shop/myshop/orders/tasks.py", line 14, in order_created
mail_sent = send_mail(subject, message, 'admin#myshop.com', [order.email])
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/message.py", line 348, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 104, in send_messages
new_conn_created = self.open()
File "/home/morilon/dj/shop/lib/python3.5/site-packages/django/core/mail/backends/smtp.py", line 64, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.5/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.5/smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.5/smtplib.py", line 306, in _get_socket
self.source_address)
File "/usr/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I had exactly the same problem, but I solved it by setting up my settings.py just like skulegirl says, and just add this to my settings.py
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
I hope it helps you.
Your code repo is configured to use the console backend for emails, but your backtrace is showing the smtp backend is being used. I assume you've changed the backend and that's what's causing the error.
By default, the smtp backend looks for an smtp server on localhost. I'm guessing you don't have one running there.
If you want to send via gmail's smtp servers you can do so, you just need to configure the approriate settings in your settings.py file. See the django docs for what needs to be configured. Read the gmail docs for the exact smtp host address to use.
Related
I am attempting to set up a script that will fire off a series of emails automatically.
Below I have the code I am using to test this process.
import smtplib, ssl
username = 'me#company.com' # input('Enter username: ')
password = 'mypassword' # input('Enter password: ')
port = 443
smtp_server = "outlook.office365.com"
sender_email = username
receiver_email = "me#company.com"
message = "Hi there! This message is sent from Python."
context = ssl.create_default_context()
# s = smtplib.SMTP_SSL('outlook.office365.com')
# s.set_debuglevel(1)
with smtplib.SMTP(smtp_server, port) as server:
server.starttls(context=context)
server.login(username, password)
server.sendmail(sender_email, receiver_email, message)
When I run it, I get the following error:
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('outlook.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I want to send the email from Outlook specifically.
Checking the 'Accounts' settings I see my server is running on port 443 and has a domain of https://outlook.office365.com/subdomain/Exchange.asmx
I'm only using the outlook.office365.com portion of it since that's all that is shown in most examples I see. However I have tried using the full domain as well as just https://outlook.office365.com/. Nothing seems to work.
I have been using the info found here to guide me.
Originally I was using the domain smtp.office365.com but changed it after finding outlook.office365.com in my accounts settings. It fails with the same error anyway.
Traceback (most recent call last):
File "/Users/gx8k/repos/namespace_auditer/./send_mail.py", line 15, in <module>
s = smtplib.SMTP_SSL('smtp.office365.com')
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1050, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 1056, in _get_socket
new_socket = super()._get_socket(host, port, timeout)
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 844, in create_connection
raise err
File "/usr/local/Cellar/python#3.9/3.9.7_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 832, in create_connection
sock.connect(sa)
TimeoutError: [Errno 60] Operation timed out
I also tried with port 587 rather than 443. (443 is what I find in my outlook settings) - same error either way.
Update: I think the answer to my question is that mac's don't use SMTP...now I have to figure out how to do this in IMAP.
What protocol does Outlook for Mac use?
By default, Outlook is set to IMAP protocol. If you choose to set your account on IMAP, you may use the following settings then click Add Account.
Additional Update:
I ended up scratching this approach altogether. I ended up using an internal API service to send notifications out to all relevant parties.
For Office 365 accounts, the SMTP server name is smtp.office365.com, post 587, TLS on.
I was having previously an issue where I was getting ValueError: server_hostname cannot be an empty string or start with a leading dot. Checked this post in Stack Overflow and when I followed the instruction and run in the console:
import smtplib
smtplib.SMTP_SSL(host='gmail-smtp-in.l.google.com').connect(host='gmail-smtp-in.l.google.com', port=25)
but I am getting a time-out as per below error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 1034, in __init__
SMTP.__init__(self, host, port, local_hostname, timeout,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 1040, in _get_socket
new_socket = socket.create_connection((host, port), timeout,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 808, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 796, in create_connection
sock.connect(sa)
I get the issue only when I enable in my code server.starttls(). If this is commented out, it can send email fine.
Below is a snippet of my code:
server = smtplib.SMTP_SSL()
server.connect('gmail-smtp-in.l.google.com', 25) # mail server want to connect to.
server.set_debuglevel(True) # shows communication with the server in the console
server.ehlo()
server.starttls()
server.ehlo()
maybe you could try using python 3.9. I dont know what operating system youre using but if youre using mac you have to use pip3 instead of pip so python can understand it (mac has python 2 as default) Hope i could help :)
In a python script, I have the following:
with smtplib.SMTP_SSL(sender_server, 465, context=context) as server:
server.login(sender_email, sender_password)
server.sendmail(
sender_email, email, message.as_string()
)
On the Windows machine I wrote the script on, everything works as expected and the email is sent and delivered without issue. However, when I try to run the same code on my Linux VPS, a TimeoutError is thrown every time.
Traceback (most recent call last):
File "script.py", line 151, in <module>
with smtplib.SMTP_SSL(sender_server, 465, context=context) as server:
File "/usr/lib/python3.6/smtplib.py", line 1031, in __init__
source_address)
File "/usr/lib/python3.6/smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.6/smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.6/smtplib.py", line 1037, in _get_socket
self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
What could be causing this difference? My Google searches didn't bear much fruit, but maybe I'm just searching for the wrong terms.
TimeoutError: [Errno 110] Connection timed out
This means that access to the remote system is likely blocked somewhere on the way to the system. Given that you have this problem on a "Linux VPS" it is likely that the connection is blocked by the company hosting this VPS or that you need to configure your machine specifically to allow such access. Check with your specific hoster and its documentation.
I'm running Django with an Apache2 server on an Ubuntu backend. On load of my site, I get a 500 internal server error. The Apache log looks like:
AH00094: Command line: '/usr/sbin/apache2'
mod_wsgi (pid=9568): Exception occurred processing WSGI script '/home/ubuntu/project/project/wsgi.py'.
Traceback (most recent call last):
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site- packages/django/core/handlers/wsgi.py", line 177, in __call__
response = self.get_response(request)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 216, in get_response
'request': request
File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1321, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
self.emit(record)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/utils/log.py", line 117, in emit
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/utils/log.py", line 120, in send_mail
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 97, in mail_admins
mail.send(fail_silently=fail_silently)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/message.py", line 292, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-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 571, in create_connection
raise err
error: [Errno 111] Connection refused
This looks to me to be an error when the mail_admins method is called. I can post the contents of wsgi.py or other files if that is needed. My application involves no explicit email usage, so I'm not sure how this is happening. Any ideas? Thanks.
Django has a special logger for sending an email to admins when a 500-error occurs. Check out https://docs.djangoproject.com/en/1.9/topics/logging/#django.utils.log.AdminEmailHandler for more info.
It seems that an exception is thrown and django tries to propagate it by sending you an email, but the email cannot be send so you get another exception! Probably the cause of the real exception is before the exception that is caused by unable to send the email. However, I propose to fix your email configuration so that django will be able to send you these emails when a 500-error occurs, it is a nice feature (and a first step to a full exception tracker like Sentry). If, for some reason you are unable to send emails from that server then I recommend at least to configure your email backend (https://docs.djangoproject.com/en/1.9/topics/email/#email-backends) to either console (EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend') or file
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = '/tmp/app-messages' # change this to a proper location
so that the mail backend will work.
I'm trying to configure a website using Django 1.8 and Plesk, and setting the email variables to any value gets the traceback below. I've tried with Gmail and the email server from the host, and the error is the same. I have tested Python's smtplib and successfully sent an email through that, so it looks like it may be something between Django and Plesk. If I set up the website on a test CentOS 6 server, then I don't run into this problem.
Example Config settings
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_PASSWORD = "mypassword"
EMAIL_HOST_USER = "user#gmail.com"
EMAIL_PORT = 587
Traceback
mod_wsgi (pid=1703): Exception occurred processing WSGI script '/var/www/vhosts/s1085163.instanturl.net/oozoa.wsgi'.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/whitenoise/base.py", line 119, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 218, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 256, in handle_uncaught_exception
'request': request
File "/usr/local/lib/python2.7/logging/__init__.py", line 1185, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1278, in _log
self.handle(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1288, in handle
self.callHandlers(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 1328, in callHandlers
hdlr.handle(record)
File "/usr/local/lib/python2.7/logging/__init__.py", line 751, in handle
self.emit(record)
File "/usr/local/lib/python2.7/site-packages/django/utils/log.py", line 129, in emit
self.send_mail(subject, message, fail_silently=True, html_message=html_message)
File "/usr/local/lib/python2.7/site-packages/django/utils/log.py", line 132, in send_mail
mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 98, in mail_admins
mail.send(fail_silently=fail_silently)
File "/usr/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 "/usr/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/local/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "/usr/local/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
Might be there is an issues with the firewall. Please try to disabled plesk firewall and IPtables on your server for the testing purpose and check it again.