簡體   English   中英

如何恢復使用 smtplib 和 multipe smtps 發送電子郵件?

[英]How to resume sending emails with smtplib and multipe smtps?

你好美麗的人,我希望你今天過得很棒

  • 我有一個文本文件,我的營銷電子郵件列表用這樣的行分隔:example1@gmail.com example2@yahoo.com example@hotmail.com

    我有 4 個 smtp 服務器鏈接到我的 cpanel 從我發送我的營銷電子郵件!

    好吧,我可以將 smtps 從文件導入我的代碼,它連接到列表中的第一個 smtp 並開始發送,當一個 smtp 關閉或超時時,它會轉到下一個 smtp再次從郵件列表的頂部開始,它確實不要從郵件列表中第一個 smtp 停止的地方繼續。

這是來自我的代碼:

Function 從文本文件中獲取我的 smtps:

def checker(file):
    with open(file, "r") as f:
         lines = f.readlines()
         for line in lines:
             smtp_server, smtp_port, smtp_user, smtp_pass = line.rstrip('\n').split("|")

Function 為每個 email 生成消息:

def generate_messages(recipients):
    with open(letter_path, 'r', encoding='utf-8') as myfile:
         data = myfile.read()
         for recipient in recipients:
             message = EmailMessage()
             message['Subject'] = letter_subject
             message['From'] = Address(letter_From, *smtp_user.split("@"))
             message['To'] = recipient
             message.set_content(data, 'html')
             yield message

發送 Function

def smtp(smtp_server, port, user, password, messages):
    with smtplib.SMTP(smtp_server, port) as server:
         try:
            server.ehlo()
            server.starttls()
            server.ehlo()
            server.login(user, password)
            print(crayons.green(f'Connected to smtp : {smtp_server}\n'''))
            for message in messages:
                server.send_message(message)
                print(Fore.GREEN +'\n[+]', message['To'] + f''' SENT!{time.strftime('%X')}''')
                time.sleep(10)
          except smtplib.SMTPException:
                 print(crayons.red(f'''smtp died \nSERVER : {smtp_server}\n'''))

我想了很多,我仍然找不到如何讓下一個 smtp 從第一個停止的地方繼續!

提前感謝您的幫助

  • 答案顯然很簡單!!!
  • 我們只需要添加

recipients.remove(recipient)

message['To'] = recipient

現在我們在發送消息時從存在中刪除收件人!

暫無
暫無

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

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