Python 批量發送郵件指令碼

來源:互聯網
上載者:User
查看源碼

 1 #!/usr/bin/env python 2 #-*- coding: utf-8 -*- 3  4 import email 5 import smtplib 6 import mimetypes 7 from email.MIMEMultipart import MIMEMultipart 8 from email.MIMEText import MIMEText 9 10 # 郵件清單檔案(每行一個郵件地址)11 MAIL_FILE_PATH = './emails.txt'12 13 # 郵件內容檔案14 MAIL_CONTENT_PATH = './page_kfc.html'15 16 # 寄件者名稱17 SENDER_NAME = 'Company Inc.'18 19 # 寄件者郵箱20 SENDER_MAIL = 'noreply@yourmailhost.com'21 22 # 寄件者郵箱密碼23 SENDER_PSWD = 'yourpassword'24 25 # SMTP 伺服器26 SMTP_SERVER = 'smtp.yourmailhost.com'27 28 # SMTP 連接埠29 SMTP_PORT = '25'30 31 # 每次發送給幾人32 RECEIVER_LIMIT_PER_TIME = 1033 34 # ##################################################################35 #                                                                  #36 #                       以下部分請勿修改                           #37 #                                                                  #38 # ##################################################################39 40 # 擷取收件者清單41 def GetReceivers(limit = 10):42     f = open(MAIL_FILE_PATH, 'r+')43 44     try:45         lines = f.readlines()46     finally:47         f.close()48 49     receivers = lines[:RECEIVER_LIMIT_PER_TIME]50     lines     = lines[RECEIVER_LIMIT_PER_TIME:]51 52     f = open(MAIL_FILE_PATH, 'w+')53     f.writelines(lines)54     f.close()55 56     return receivers57 58 # 批量發送郵件59 def SendEmail(sender, senderName, receivers, subject, body):60     smtp = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)61     smtp.login(SENDER_MAIL, SENDER_PSWD)62 63     if(senderName != ''):64         sender = senderName + '<' + sender + '>'65 66     for receiver in receivers:67         receiver = receiver.strip()68 69         msg = MIMEMultipart('alternative')70         msg['Subject'] = subject71         msg['From'] = sender72         msg['To'] = receiver73         msg.attach(MIMEText(body, 'html', 'utf-8'))74 75         smtp.sendmail(sender, receiver, msg.as_string())76 77     smtp.quit()78 79 if __name__ == '__main__':80     '''81     發送郵件開始82     '''83 84     # 擷取本次要發送的郵件地址85     receivers = GetReceivers(RECEIVER_LIMIT_PER_TIME)86 87     # 擷取郵件標題和內容88     f = open(MAIL_CONTENT_PATH, 'r');89     lines = f.readlines()90     f.close()91 92     subject = lines[0].strip()93     body = ''.join(lines[1:])94 95     # 發送96     SendEmail(SENDER_MAIL, SENDER_NAME, receivers, subject, body)

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.