python指定csvテーブルの連絡先グループにメール(添付ファイル付きメール)

4738 ワード

以下にpythonを使用して、指定したパスのcsvテーブルの連絡先グループに添付ファイル付きメール(csvテーブルの最初の列は連絡先名、2番目の列は連絡先メールボックスアカウント)を送信するコードを示します.詳細はコメントを参照してください.

import time
import csv #         
import smtplib #      
from email.header import Header #Header       
from email.mime.text import MIMEText #MIMEText        
from email.mime.image import MIMEImage #MIMEImage        
from email.mime.multipart import MIMEMultipart #MIMEMultipart        (      )
#       
def receive_info():
  #        ,            ,           
  to_addrs = []
  #to = input('        :')
  with open('     .csv','r',encoding='utf-8') as f:
    reader = csv.reader(f)
    try:
      for row in reader:
        to_addrs.append(row[1])
      print('         !')
      time.sleep(1)
      print('          :
',to_addrs) receivers = ','.join(to_addrs) except Exception as e: print(' ! :') print(e) return to_addrs,receivers # def send_info(): print(' ...') time.sleep(1) try: sender_mail = input(' :') sender_pass = input(' ( ):')# smtp_server = 'smtp.qq.com' # time.sleep(1) print(' !') except Exception as e: print(' ! :') print(e) return sender_mail,sender_pass,smtp_server # def mail_body(to): # , mixed, msg_root = MIMEMultipart('mixed') # msg_root['From'] = Header(sender_mail) msg_root['To'] = Header(receivers) #Header # , subject = 'python sendemail test successful' msg_root['subject'] = Header(subject, 'utf-8') # , # text_info = 'hello world' text_sub = MIMEText(text_info, 'plain', 'utf-8') # , (‘plain' , ) msg_root.attach(text_sub) # url = "https://blog.csdn.net/m0_45654959" html_info = """

click me

i am very galsses for you

"""% url html_sub = MIMEText(html_info, 'html', 'utf-8') # # , , html_sub["Content-Disposition"] = 'attachment; filename="csdn.html"' # # ( ) msg_root.attach(html_sub) # image_file = open(r'D:\Anaconda\programm_my\little_programm\test.gif', 'rb').read() image = MIMEImage(image_file) # image.add_header('Content-ID', '') # ID , # , bin , image["Content-Disposition"] = 'attachment; filename="hua_ji.gif"' # ( ) msg_root.attach(image) # (base64 ) txt_file = open(r'D:\Anaconda\programm_my\little_programm\hello_word.txt', 'rb').read() txt = MIMEText(txt_file, 'base64', 'utf-8') txt["Content-Type"] = 'application/octet-stream' #Content-Type application/octet-stream, # hello_world.txt txt.add_header('Content-Disposition', 'attachment', filename='hello_world.txt') # ( ) msg_root.attach(txt) return msg_root # def send_mail(): time.sleep(1) print(' ...') try: server =smtplib.SMTP_SSL(smtp_server)#QQ SSL ; python3.7 smtplib.SMPT_SSL() server.connect(smtp_server,465) server.login(sender_mail, sender_pass) server.sendmail(sender_mail, to_addrs, msg.as_string()) server.quit() print(' ! !') except Exception as e: print(' ! :') print(e) # : def main(): send_mail() if __name__ == '__main__': # 、 、 to_addrs,receivers = receive_info() sender_mail,sender_pass,smtp_server = send_info() msg = mail_body(to_addrs) # , main()

まとめ
以上、編集者が紹介したpythonがcsvテーブルの連絡先グループにメールを送ります.