簡體   English   中英

從txt文件中的數組發送單個電子郵件

[英]Sending individual emails from array in txt file

我的代碼獲取txt文件,然后將電子郵件發送到列表中的第一封電子郵件,然后停止並且不對下一封電子郵件進行處理。 我在用這個數組做錯什么?

我嘗試創建一個數組來為target_email每個電子郵件運行該函數,並且它僅將電子郵件發送到數組中的第一封電子郵件。 當我打印數組時,它看起來像這樣[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

該腳本應針對每封電子郵件(而不是BBC)單獨運行### DO NOT EDIT BELOW THIS LINE ### ,而僅發送至一封電子郵件並停止。

您無需發送個人電子郵件地址,而是發送完整列表。

采用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM