-1

Hey i want to send an email to a bunch of people but for some reason even if the output of print is more than one email the program sending the email only to first person of the text what can i do ?

# Import Python Packages
import smtplib
# Set Global Variables
gmail_user = 'your@gmail.com'
gmail_password = 'password'
# Create Email 
mail_from = gmail_user

for i in range(2):
  with open('C:\\email.txt', 'r', encoding="utf8") as f
      mail_to = f.read().rstrip()
      
  mail_subject = 'subject'
  mail_message_body = 'body'

  mail_message = '''\
  From: %s
  To: %s
  Subject: %s
  %s
  ''' % (mail_from, mail_to, mail_subject, mail_message_body)
  # Sent Email
  server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  server.login(gmail_user, gmail_password)
  server.sendmail(mail_from, mail_to, mail_message)
  print(mail_to)
server.close()

2 Answers2

1

Send_to must be a string object having addresses separated by ", ".

example :

send_to='xyz1@gmail.com,hhdasn@yahoo.com'
buddemat
  • 4,552
  • 14
  • 29
  • 49
Vikas Jain
  • 11
  • 1
0

The line that performs sending of email is server.sendmail(mail_from, mail_to, mail_message). You are calling it once. You can check it by placing print(mail_to) statement next to it.

You need to call sendmail in loop if you want to send email multiple times.

fdermishin
  • 3,519
  • 3
  • 24
  • 45
  • Now you are creating server and logging in several times. But I think that adding emails to `mail_to` as comma-separated list is a better solution, as Vikas Jain suggets – fdermishin Nov 25 '20 at 10:50