一、需求:
刚入职时,接到研发部的一个需求,需要一个邮件服务,作为研发日常工作中邮件发送的统一入口,于是想到可以基于flask写一个。
二、代码:
具体代码可参考如下git地址:
https://github.com/f1017746640/fmax.git
个人博客站点:
http://www.fengzhihai.cn/
三、接口测试:
接口测试的代码也在git库中。
3.1 没有附件:
#!/usr/bin/env python # encoding: utf-8 """ > FileName: mailapi.py > Author: FZH > Mail: fengzhihai@xxx.cn > CreatedTime: 2020-03-22 13:20 """ import os import requests url = "http://127.0.0.1:5000/fmax" def mail_send(): data = {'mail_to': 'fengzhihai@xxx.cn', 'mail_title': 'TEST_TITLE', 'mail_body': '接口更新完毕'} files = [] response = requests.request("POST", url, data=data, files=files) return response.text.encode('utf8') if __name__ == '__main__': mail_send()
测试结果:
3.2 有附件:
#!/usr/bin/env python # encoding: utf-8 """ > FileName: mailapi.py > Author: FZH > Mail: fengzhihai@xxx.cn > CreatedTime: 2020-03-22 13:20 """ import os import requests url = "http://127.0.0.1:5000/fmax" def mail_send_append(): data = {'mail_to': 'fengzhihai@xxx.cn', 'mail_title': 'TEST_TITLE', 'mail_body': '接口更新完毕'} des_file = os.path.join(os.path.dirname(__file__), 'fmax.xlsx') files = {'file': open(des_file, 'rb')} headers = { 'Content-Type': 'multipart/form-data' } response = requests.request("POST", url, data=data, files=files) return response.text.encode('utf8') if __name__ == '__main__': mail_send_append()
测试结果: