前提代碼編碼格式爲UTF-8 設置方式是在文件第一行或第二行# -- coding:utf-8 --flask
MIME格式服務器
MIME_TYPE = { '.xls': 'application/vnd.ms-excel', '.xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', '.txt': 'text/plain', '.pdf': 'application/pdf', '.doc': 'application/msword', '.docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', '.ppt': 'application/vnd.ms-powerpoint', '.rar': 'application/rar', '.zip': 'application/zip', '.tar': 'application/x-tar', '.gz': 'application/x-gzip' }
根據自身的需求到w3school(http://www.w3school.com.cn/media/media_mimeref.asp) 查找 office 2007以後的版本Excel、word、ppt有的差別app
郵件服務器的配置參考flask-mail標準配置便可編碼
發送郵件excel
from flask.ext.mail import Mail, Message msg = Message(郵件標題, sender='發件人地址', recipients=['多個收件人地址'] ) # 添加多個附件 for document in documents_path: # 文件的文件名 file_name = os.path.basename(document) with current_app.open_resource(document) as fp: # file_name.encode("utf-8") 對文件名進行編碼 # current_app.config['MIME_TYPE'][os.path.splitext(file_name)[1]] 經過後綴名,獲取MIME格式 msg.attach(file_name.encode("utf-8"), current_app.config['MIME_TYPE'][os.path.splitext(file_name)[1]], fp.read()) msg.body = 郵件正文 # 發送郵件 mail.send(msg)