从阵列中的txt文件发送单个电子邮件

问题描述 投票:0回答:1

我的代码使用txt文件,并发送电子邮件到列表中的第一封电子邮件,然后停止并为下一个不这样做。什么是我错了这阵在做什么?

我曾尝试创建一个数组运行在target_email每封电子邮件的功能,而且只发送电子邮件到第一封电子邮件在数组中。当我打印阵列它看起来像这样[[email protected], [email protected]]

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

[email protected], [email protected]

该脚本应单独和不运行的每封电子邮件的### DO NOT EDIT BELOW THIS LINE ###一个BBC或只需发送一个电子邮件和停止。

python smtplib
1个回答
1
投票

相反,发送个人电子邮件地址,你是送的完整列表。

采用:

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

编辑按评论。

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))
© www.soinside.com 2019 - 2024. All rights reserved.