0

My code takes the txt file and sends an email to the first email in the list and then stops and doesn't do it for the next. What am I doing wrong with this array?

I have tried creating an array to run the function for each email in target_email and it only sends an email to the first email in the array. When I print the array it looks like this [123@txt.att.net, 876@txt.att.net]

####the py script

import time
import smtplib

#CONFIG. You can change any of the values on the right.
email_provider = '' #server for your email- see ReadMe on github
email_address = "" #your email
email_port = 587 #port for email server- see ReadMe on github
password = "" #your email password
msg = "Meow" #your txt message
text_amount = 1 #amount sent

#Gets phone number emails from txt file
text_file = open("mails.txt", "r")
target_email = text_file.readlines()

wait = 15 #seconds in between messages
#END CONFIG

#Loops for each phone email in list text_amount of times
for emails in target_email:
     server = smtplib.SMTP(email_provider, email_port)
     server.starttls()
     server.login(email_address, password)
        for _ in range(0,text_amount):
         server.sendmail(email_address,target_email,msg)
         print("sent")
         time.sleep(wait)
print("{} texts were sent.".format(text_amount))


###the txt file contents

123@txt.att.net, 876@txt.att.net

The script should run the ### DO NOT EDIT BELOW THIS LINE ### for each email individually and not a BBC or just send to one email and stop.

Cody Krecicki
  • 67
  • 1
  • 7
  • Have you tried this: [How to send email to multiple recipients using python smtplib?](https://stackoverflow.com/questions/8856117/how-to-send-email-to-multiple-recipients-using-python-smtplib) – Georgy Feb 05 '19 at 09:10
  • @Georgy I am trying to send an email to each individual individually -- we figured out a way to get it to BCC in the whole array but I want the script to email the first email, then run again and email the second email, not all the emails at the same time in the same email. – Cody Krecicki Feb 05 '19 at 09:28
  • Check these posts, maybe there is something useful: [How to send different content email to different recipient in Python?](https://stackoverflow.com/questions/48521036/how-to-send-different-content-email-to-different-recipient-in-python), [Send individual emails to multiple recipients](https://stackoverflow.com/questions/50774697/send-individual-emails-to-multiple-recipients), [Python smtplib: only the first email out of several arrives to destination](https://stackoverflow.com/questions/24550891/python-smtplib-only-the-first-email-out-of-several-arrives-to-destination) – Georgy Feb 05 '19 at 10:40
  • [Email goes to first recipient only smtp mail python](https://stackoverflow.com/questions/27074175/email-goes-to-first-recipient-only-smtp-mail-python), [Python email - can only sendmail to one address](https://stackoverflow.com/questions/24536029/python-email-can-only-sendmail-to-one-address), [Python Not Sending Email To Multiple Addresses](https://stackoverflow.com/questions/20509427/python-not-sending-email-to-multiple-addresses) – Georgy Feb 05 '19 at 10:42

1 Answers1

1

Instead of sending individual email address you are send the full list.

Use:

#Loops for each phone email in list text_amount of times
for emails in target_email:
    ### DO NOT EDIT BELOW THIS LINE ###
    server = smtplib.SMTP(email_provider, email_port)
    server.starttls()
    server.login(email_address, password)
    for _ in range(0,text_amount):
        server.sendmail(email_address,emails,msg)        #Update!!
        print("sent")
        time.sleep(wait)
print("{} texts were sent.".format(text_amount))

Edit as per comments.

server = smtplib.SMTP(email_provider, email_port)
server.starttls()
server.login(email_address, password)

with open("mails.txt") as infile:
    for line in infile:
        line = line.strip()
        if "," in line:
            emails = line.split(",")
        else:
            emails = line
        for _ in range(0,text_amount):
            server.sendmail(email_address,emails,msg)
            print("sent")
            time.sleep(wait)
        print("{} texts were sent.".format(text_amount))
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • I tried this and it is still only sending the message to the first item in the array and not the second and so on. – Cody Krecicki Feb 05 '19 at 07:58
  • what does `target_email ` print? – Rakesh Feb 05 '19 at 08:02
  • When I try to print(emails) it prints nothing when I print(target_email) it prints the items in the txt file ---- ['000@txt.att.net, 001@txt.att.net'] – Cody Krecicki Feb 05 '19 at 08:12
  • Do you have the emails in a single line? – Rakesh Feb 05 '19 at 08:31
  • yes they are currently and I've tried putting them line by line that didn't work either. – Cody Krecicki Feb 05 '19 at 08:33
  • when I run this script and check my sent mail box on my server the email sends two emails but to everyone in the array in a bcc: 123@txt.att.net, bcc: 876@txt.att.net they are phone numbers because we're using the sms gateway email. its just sending one email to everyone and BCCing them in. – Cody Krecicki Feb 05 '19 at 08:46