smtplib發送郵件最後一篇,發送帶圖片的郵件:html
你們能夠去廖雪峯的網站看一下,下面的代碼就是我跟着博客寫的,哈哈,你們即便不明白爲何,也要多寫兩遍,記在內心,若是有不明白的地方能夠留言,船長會第一時間給你們解決;今天有朋友讓我建一個QQ羣,我想如今還不是建羣的時候,由於你們基本上沒有什麼問題,若是之後問題多了,我會建一個羣,來集中解決你們的問題,謝謝你們的支持!開擼吧~~~markdown
# coding: utf-8 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders mail_host = 'smtp.163.com' receivers = ['123@qq.com','456@qq.com'] receiver = ','.join(receivers) sender = 'warrior_meng08@163.com' password = '受權碼' msg = MIMEMultipart('alternative') msg['From'] = sender msg['To'] = receiver msg['Subject'] = '主題' msg.attach(MIMEText('<html><body><h1>Hello</h1>' + '<p><img src="cid:0"></p>' + '</body></html>', 'html', 'utf-8')) file_path = r'C:\Users\weiming\Desktop\b.jpg' with open(file_path, 'rb') as f: mm = MIMEBase('image', 'jpg', filename='b.jpg') mm.add_header('Content-Disposition', 'attachment', filename='b.jpg') mm.add_header('Content-ID', '<0>') mm.add_header('X-Attachment-Id', '0') mm.set_payload(f.read()) encoders.encode_base64(mm) msg.attach(mm) try: server = smtplib.SMTP() # 加上這句就能夠打印出全部的log,更方便咱們定位問題 server.set_debuglevel(1) server.connect(mail_host, 25) server.login(sender, password) server.sendmail(sender, receiver,msg.as_string()) server.close() print 'Success' except smtplib.SMTPException: print 'Error'