简体   繁体   English

从txt文件中的数组发送单个电子邮件

[英]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. 我的代码获取txt文件,然后将电子邮件发送到列表中的第一封电子邮件,然后停止并且不对下一封电子邮件进行处理。 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. 我尝试创建一个数组来为target_email每个电子邮件运行该函数,并且它仅将电子邮件发送到数组中的第一封电子邮件。 When I print the array it looks like this [123@txt.att.net, 876@txt.att.net] 当我打印数组时,它看起来像这样[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. 该脚本应针对每封电子邮件(而不是BBC)单独运行### DO NOT EDIT BELOW THIS LINE ### ,而仅发送至一封电子邮件并停止。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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