Search code examples
python-3.xgmail

Gmail account. Python 3.8 idle script. Error: smtplib.SMTPSenderRefused: (503, b'5.5.1 EHLO/HELO first


I'm doing an exercise on writing a module in python 3.8 idle (Mac) to send emails from my gmail account. It is giving me the error: smtplib.SMTPSenderRefused: (503, b'5.5.1 EHLO/HELO first.

THE COMPLETE RUNNING RESULT:

= RESTART: /Users/mimikatz/Desktop/python/Python_note&exercise/send_email_gmail.py

person_name
Thank you for sharing! 

Traceback (most recent call last):
File "/Users/mimikatz/Desktop/python/Python_note&exercise/send_email_gmail.py", line 58, in <module>
main()

File "/Users/mimikatz/Desktop/python/Python_note&exercise/send_email_gmail.py", line 51, in main
s.send_message(msg)

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 970, in send_message
return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,

File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/smtplib.py", line 871, in sendmail
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (503, b'5.5.1 EHLO/HELO first. c18sm12642612wmk.18 - gsmtp', '[email protected]')

The first two lines (person_name Thank you for sharing!) are from the 'message.txt' file.

MY CODE IS:

import smtplib
from string import Template
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from_addr = '[email protected]'
password = 'xxxxxxxxxxx'
smtp_server = 'smtp.gmail.com'

def get_contacts(self):
    names = []
    emails = []
    with open(self, 'r', encoding='utf-8') as contacts_file:
        for a_contact in contacts_file:
            names.append(a_contact.split()[0])
            emails.append(a_contact.split()[1])
    return names, emails

def read_template(self):
    with open(self, 'r', encoding='utf-8') as template_file:
        template_file_content = template_file.read()
    return Template(template_file_content)

def main():
    names, emails = get_contacts('contacts.txt') # read contacts
    message_template = read_template('message.txt')

    # set up the SMTP server
    s = smtplib.SMTP_SSL(smtp_server)
    s.ehlo()
    s.connect(smtp_server,465)
    s.login(from_addr, password)

    # For each contact, send the email:
    for name, email in zip(names, emails):
        msg = MIMEMultipart()     # create a message
        # add in the actual person name to the message template
        message = message_template.substitute(person_name = name.title())
        # prints out the message body for our sake
        print(message)
        # setup the parameters of the message
        msg['From'] = from_addr
        msg['To'] = email
        msg['Subject'] = 'This is TEST'
        # add in the message body
        msg.attach(MIMEText(message, 'plain', 'utf-8'))
        # send the message via the server set up earlier.
        s.send_message(msg)
        del msg

    # Terminate the SMTP session and close the connection
    s.quit()

if __name__ == '__main__':
    main()

I saw this code in one of the questions asked here and it looked great to me as an example, so I wanted to try it out. But my problem turns out different than what the original one had. From the running result, it looks like the """print(message)""" command is successfully loaded. The problem occurs at """s.send_message(msg)""". I checked online several similar cases but couldn't find answer that suits this condition.

Really grateful to any help :)

Acorus

Problem solved thanks to Konrad

Swapping the .ehlo() with .connect()

& Changing the password to an authentication code generated from gmail setting 2-step verification app password. I used "mail" & "mac" to generate the verification code.


Solution

  • While I have never used Python to send emails, I've spent some time communicating with SMTP servers via Telnet.

    You need to connect to the server first, then send EHLO, then authenticate and finally send the message. Your code seems to try sending EHLO before connecting to the server.

    Try swapping those two lines.