2

I recently registered a domain on bluehost.com. For now, I want to send simple 'reset password' emails. Using the "Standard (without SSL/TLS) Settings" works fine.

However, the bluehost documentation suggests using Secure SSL/TLS Settings, so I want to give that a try:

Username:           Your email address: john@example.com
Password:           The password for that email account. 
Outgoing Server:    mail.example.com
Outgoing Port:      465 (SMTP)

Here is my code (anonymized parameters):

import smtplib, ssl, os

# Params
port = 465 
smtp_server = "mail.mydomain.com"
sender_email = "sender@mydomain.com"
receiver_email = "receiver@gmail.com"
password = os.environ.get("SMTP_PASSWORD")

# As documented in https://docs.python.org/3/library/smtplib.html
""" Deprecated since version 3.6: keyfile and certfile are deprecated 
in favor of context. Please use ssl.SSLContext.load_cert_chain() instead, 
or let ssl.create_default_context() select the system’s trusted
CA certificates for you. """
context = ssl.create_default_context()

# Create the connection and authenticate
with smtplib.SMTP_SSL(smtp_server, port, context=context) as s:
    s.set_debuglevel(1)
    s.login(sender_email, password)
    s.quit()

The code fails on:

...Python37-32\lib\ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

I contacted bluehost tech support, who clarified no certificates were missing or anything. Their answer was unsatisfying in the sense that they just said "SSL sometimes doesn't work. Reset your password and try again" (the latter didn't work).

I'm hoping I'm simply overlooking a mistake in my code. Can someone point me in the right direction? I've run out of Google.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Robin Teuwens
  • 121
  • 1
  • 5

0 Answers0