pythonemailsslsmtp

I'm encountering "SMPTHeloError: (501, b'Syntactically invalid HELO argument(s)')" on one device but not others


I'm making a program that is supposed to regularly send me emails so that I can do regular status checks on work equipment. The computer I need to use the program on gives me the error SMPTHeloError: (501, b'Syntactically invalid HELO argument(s)'), why does this show up and how do I get rid of this error?

When I test on my personal laptop, the code executes just fine, and when I test on another computer it works just fine. I've looked around for solutions, but all I could find was the host name being potentially wrong, but I checked my devices' name and it does not have any underscores or invalid characters and is structured the same way as my personal laptop, which has no issues executing the code.

Here's an example python code to reproduce the issue:

import smtplib, ssl
from email.message import EmailMessage

msg=EmailMessage()
msg['From'] = "Status Updater <example@example.com>"
msg['To'] = "John Doe <example@example.com>"
msg.set_content("Email Contents")
context = ssl.create_default_context()
with smtplib.SMTP_SSL("mail.example.com", 465, context=context) as server:
    server.login('example@example.com', "password")
    server.send_message(msg)

Solution

  • By using the debug transcript I figured out that what was causing the error was indeed an underscore in the hostname, but it wasn't because of the device name. It seems it appends the Connection Specific DNS Suffix at the end of your hostname (and that Suffix had an underscore in it) and I needed to edit that in order to stop the error from happening anymore. This is the guide I used in order to change the DNS Suffix.