**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()