Search code examples
pythonsmtplibpysimplegui

Sending email with smtplib, no errors but no messages delivered


**UPDATE2: In the last bit of code of V2 I swapped smtp.sendmail(email_address, address, msg,) with smtp.sendmail(email_address, phone_book, msg,) Accessing the phone_book directly seems to have solved the issue. Here is the working code for anyone that is looking:

import smtplib

email_address = '' # Your email goes here
email_password = '' # Your password goes here
phone_book = [''] # List of receivers


def Add_Email():
    client_email = input('Email of receiver? ')
    phone_book.append(client_email)

def Add_Subject_Message_Send():    
    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            smtp.login(email_address, email_password)

            subject = input('Enter your subject here: ')
            body = input('Enter your message here: ')

            msg = f'Subject: {subject}\n\n{body}'

            for i in phone_book:
                address = i
                smtp.sendmail(email_address, phone_book, msg,)


Add_Email()
Add_Subject_Message_Send()

**

**UPDATE: I swapped the code with the simplest versions without GUI. V1 works when subject and body are defined in code. V2 doesn't work when user defines subject and body. Now V2 has this error message:

Traceback (most recent call last):
  File "c:/Users/Me/Desktop/work/infosend/test4.py", line 33, in <module>
    Add_Subject_Message_Send()
  File "c:/Users/Me/Desktop/work/infosend/test4.py", line 29, in Add_Subject_Message_Send
    smtp.sendmail(email_address, address, msg,)
  File "C:\python\lib\smtplib.py", line 885, in sendmail
    raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'': (555, b'5.5.2 Syntax error. l15sm65056407wrv.39 - gsmtp')}

**

I'm sending emails using smtplib. As long as subject and body of the message are defined in the code everything works and email gets delivered. When there is no subject or body defined in the code, no errors shows, but message is not delivered.

I want to create a function that will allow me to write what the subject and message is, rather than defining it in the code. My function seems to work, but no messages are being delivered and no error messages received.

Attaching two versions of my code.

First version works. It has defined subject and body.

Second version doesn't work. Includes functions to define subject and body. No errors received in terminal.

V1

import smtplib

email_address = '' # Enter your email address here
email_password = '' # Enter your email password here

phone_book = [''] # Here enter the email of receiver

with smtplib.SMTP('smtp.gmail.com', 587) as smtp: # Connects with GMAIL
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            smtp.login(email_address, email_password)

            subject = 'test3' # Subject and body defined in code = works
            body = 'test3'

            msg = f'Subject: {subject}\n\n{body}'

            for i in phone_book:
                address = i
                smtp.sendmail(email_address, address, msg,)

V2

    import smtplib

email_address = '' # Your email goes here
email_password = '' # Your password goes here
phone_book = [''] # List of receivers


def Add_Email():
    client_email = input('Email of receiver? ')
    phone_book.append(client_email)

def Add_Subject_Message_Send():    
    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            smtp.login(email_address, email_password)

            subject = input('Enter your subject here: ')
            body = input('Enter your message here: ')

            msg = f'Subject: {subject}\n\n{body}'

            for i in phone_book:
                address = i
                smtp.sendmail(email_address, address, msg,)


Add_Email()
Add_Subject_Message_Send()

Solution

  • Please refer to my code. I have done this and it's working. You can get some lead by refering my code.

    Check main function code.

    import smtplib
    
    from string import Template
    
    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText
    
    MY_ADDRESS = 'xyz@gmail.com'
    PASSWORD = 'YourPassword'
    
    
    def get_contacts(filename):
        names = []
        emails = []
        with open(filename, mode='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(filename):
        with open(filename, 'r', encoding='utf-8') as template_file:
            template_file_content = template_file.read()
        return Template(template_file_content)
    
    
    def main():
        names, emails = get_contacts('C:/Users/VAIBHAV/Desktop/mycontacts.txt')  # read contacts
        message_template = read_template('C:/Users/VAIBHAV/Desktop/message.txt')
        s = smtplib.SMTP(host='smtp.gmail.com', port=587)
        s.starttls()
        s.login(MY_ADDRESS, PASSWORD)
        for name, email in zip(names, emails):
            msg = MIMEMultipart()  # create a message
            message = message_template.substitute(PERSON_NAME=name.title())
            print(message)
            msg['From'] = MY_ADDRESS
            msg['To'] = email
            msg['Subject'] = "Sending mail to all"
            msg.attach(MIMEText(message, 'plain'))
            s.send_message(msg)
            del msg
        s.quit()
    if __name__ == '__main__':
        main()