1. 程式人生 > 資訊 >本田中國宣佈首款純電動車型 e:NS1 開啟預售:續航超 500km,18~21 萬元

本田中國宣佈首款純電動車型 e:NS1 開啟預售:續航超 500km,18~21 萬元

指令碼(使用python自帶的email模組)

#!/usr/bin/python3
# -*- coding:utf-8 -*-


import smtplib, os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.header import Header
import base64


class SendMail(object):
    def __init__(self, username, passwd, email_host, recv, title, content, file=None, imagefile=None, ssl=False,
                 port
=25, ssl_port=465): ''' :param username: 使用者名稱 :param passwd: 密碼 :param email_host: smtp伺服器地址 :param recv: 收件人,多個要傳list ['[email protected]','[email protected]] :param title: 郵件標題 :param content: 郵件正文 :param file: 附件路徑,如果不在當前目錄下,要寫絕對路徑,預設沒有附件 :param imagefile: 圖片路徑,如果不在當前目錄下,要寫絕對路徑,預設沒有圖片 :param ssl: 是否安全連結,預設為普通 :param port: 非安全連結埠,預設為25 :param ssl_port: 安全連結埠,預設為465
''' self.username = username # 使用者名稱 self.passwd = passwd # 密碼 self.recv = recv # 收件人,多個要傳list ['[email protected]','[email protected]] self.title = title # 郵件標題 self.content = content # 郵件正文 self.file = file # 附件路徑,如果不在當前目錄下,要寫絕對路徑 self.imagefile = imagefile #
圖片路徑,如果不在當前目錄下,要寫絕對路徑 self.email_host = email_host # smtp伺服器地址 self.port = port # 普通埠 self.ssl = ssl # 是否安全連結 self.ssl_port = ssl_port # 安全連結埠 def send_mail(self): # msg = MIMEMultipart() msg = MIMEMultipart('mixed') # 傳送內容的物件 if self.file: # 處理附件的 file_name = os.path.split(self.file)[-1] # 只取檔名,不取路徑 try: f = open(self.file, 'rb').read() except Exception as e: raise Exception('附件打不開!!!!') else: att = MIMEText(f, "base64", "utf-8") att["Content-Type"] = 'application/octet-stream' # base64.b64encode(file_name.encode()).decode() new_file_name = '=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?=' # 這裡是處理檔名為中文名的,必須這麼寫 att["Content-Disposition"] = 'attachment; filename="%s"' % (new_file_name) msg.attach(att) if self.imagefile: try: sendimagefile = open(self.imagefile, 'rb').read() except Exception as e: raise Exception('圖片無法開啟!!!!') else: image = MIMEImage(sendimagefile) image.add_header('Content-ID', '<image1>') msg.attach(image) text_html = MIMEText(self.content, 'html', 'utf-8') msg.attach(text_html) # msg.attach(MIMEText(self.content)) # 郵件正文的內容 msg['Subject'] = self.title # 郵件主題 msg['From'] = self.username # 傳送者賬號 msg['To'] = ','.join(self.recv) # 接收者賬號列表 if self.ssl: self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.ssl_port) else: self.smtp = smtplib.SMTP(self.email_host, port=self.port) # 傳送郵件伺服器的物件 self.smtp.login(self.username, self.passwd) try: self.smtp.sendmail(self.username, self.recv, msg.as_string()) pass except Exception as e: print('出錯了。。', e) else: print('傳送成功!') self.smtp.quit() if __name__ == '__main__': """ 傳送正文中的圖片:由於包含未被許可的資訊,網易郵箱定義為垃圾郵件,報554 DT:SPM :<p><img src="cid:image1"></p> """ m = SendMail( username='[email protected]', passwd='xxxxxxx', email_host='smtp.exmail.qq.com', recv=['[email protected]', '[email protected]'], title='傳送郵件', content=""" <html> <head></head> <body> <p>Hi!<br> How are you?<br> Here is the <a href="http://www.baidu.com">link</a> you wanted.<br> </p> <img src="cid:image1"> </body> </html> """, file=r'測試附件.xls', imagefile=r'test.png', ssl=True, ) m.send_mail()

參考連結:
      https://www.cnblogs.com/yufeihlf/p/5726619.html
      https://www.cnblogs.com/mpp0905/p/8419869.html