简体   繁体   中英

Sending individual emails from array in txt file

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM