0
votes

Hi I am trying to send an email to myself when an exception is raised. However I am getting an exception I don't know how to solve. Here is the code and exception. I have tried all SO answers but in vain

import logging
import logging.handlers
import smtplib

s = smtplib.SMTP_SSL("smtp.gmail.com", 587)
s.login('[email protected]', 'myPassword')
s.starttls()

logger = logging.getLogger()
logger.addHandler(s)

try:
    a = 2/0
except Exception as e:
  logger.exception('Unhandled Exception')
  s.sendmail('[email protected]', '[email protected]', 'Hi')
  s.close()

ERROR LOG:

Traceback (most recent call last): File "try.py", line 5, in s = smtplib.SMTP_SSL("smtp.gmail.com", 587)

File "/usr/lib/python2.7/smtplib.py", line 788, in init SMTP.init(self, host, port, local_hostname, timeout)

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 794, in _get_socket new_socket = ssl.wrap_socket(new_socket, self.keyfile, self.certfile)

File "/usr/lib/python2.7/ssl.py", line 487, in wrap_socket ciphers=ciphers)

File "/usr/lib/python2.7/ssl.py", line 243, in init self.do_handshake()

File "/usr/lib/python2.7/ssl.py", line 405, in do_handshake self._sslobj.do_handshake()

ssl.SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol

Thanks for help in advance.

1

1 Answers

0
votes

587 is a non-SSL port and hence SMTP_SSL call is failing. Try using port 465 as

smtplib.SMTP_SSL("smtp.gmail.com", 465)

or use the method for port 587

smtplib.SMTP("smtp.gmail.com", 587)