I am trying to send an Email from python code. The Code works fine when tested on my local Server. But when I deploy these changes on my Google Compute Engine VM, the Email sending stops and Connection Error starts coming.
Error Trace:
File "/usr/local/lib/python2.7/dist-packages/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/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 571, in create_connection
raise err
error: [Errno 110] Connection timed out
Code:
from django.core.mail import EmailMessage
msg = EmailMessage("Test Sub", "Msg", "abc#abc.in", ["def#abc.in"])
msg.content_subtype = "html"
msg.send()
Email settings in settings.py File:
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.zoho.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'abc#abc.in'
EMAIL_HOST_PASSWORD = 'abcxxxabc'
DEFAULT_FROM_EMAIL = 'abc#abc.in'
DEFAULT_TO_EMAIL = 'def#abc.in'
Can someone please suggest what could be the cause for this behavior ? And how can this issue be resolved ?
Thanks,
You can't send mail directly from google cloud, port 587 is blocked. You're gonna have to find another solution; take a look at Mailgun.
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 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 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.
I was trying to send an email from my Django app using gmail. I added these email settings to settings.py
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemail#gmail.com'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
But it throws this name resolution error:
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/__init__.py", line 62, in send_mail
return mail.send()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/message.py", line 303, in send
return self.get_connection(fail_silently).send_messages([self])
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open()
File "/usr/local/lib/python3.4/dist-packages/Django-1.8.7-py3.4.egg/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params)
File "/usr/lib/python3.4/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.4/smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.4/smtplib.py", line 292, in _get_socket
self.source_address)
File "/usr/lib/python3.4/socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
Note: I am working behind a proxy.
In addition, I tried to connect to google.com through socket:
import socket
socket.gethostbyname('www.google.com')
It throws:
[Errno 11001] getaddrinfo failed
enable pop3 in gmail.com
create specific password for django application
(http://support.google.com/accounts/bin/answer.py?hl=en&answer=185833)
Solutions here