我正在制作python程序,用于自动发送带有文件附件的eamil。
sendHTML是电子邮件内容。在没有附加的情况下,它工作得很好。
但是,添加像下面这样的源文件会导致错误: Data不能是字符串。所以,我把代码改成了像下面这样,但它也不起作用。请救救我:(
===============changed code=================================================
files = {'json' : (None, json.dumps(sMailBody).encode("utf-8"), 'application/json' ),'file': (open('''20180430_'18 Tizen Store Buyer Report v2.4.xlsx''', 'rb'), 'application/octet-stream')}
headers = {'authorization': 'Bearer xx', "Content-Type" : "multipart/form-data"}
res = requests.post(url, headers=headers, files=files)
===================源码========================
sendHTML = str(fullReportFrameSoup) -
url = "the url of samsung"
sMailBody = {
--> this is my company mail system's Rest API
"resourceVO" :
{
"email" : "xxx@samsung.com",
"localeStr" : "ko_KR",
"encoding" : "utf-8",
"timeZone" : "GMT+9",
},
"sendMailVO" :
{
"contentText" : sendHTML,
"subject" : "[Tizen Store] 일일통계",
"docSecuType" : "PERSONAL",
"contentType" : "HTML",
"from" :
{
"email" : "xxx@samsung.com"
},
"attachs" :[],
"recipients" :
[
{
"email" : "xxx@samsung.com",
"recipientType" : "TO"
}
]
}
}
json_data = json.dumps(sMailBody).encode("utf-8")
files = {'file': ('''20180430_'18 Tizen Store Buyer Report v2.4.xlsx''', open('''20180430_'18 Tizen Store Buyer Report v2.4.xlsx''', 'rb') , 'application/vnd.ms-excel', {'Expires': '0'})}-->
headers = {'authorization': 'Bearer xxx', "Content-Type" : "application/json"}
res = requests.post(url, data=json_data, headers=headers, files=files)
发布于 2018-05-31 19:43:47
尝试在JSON参数中将json有效负载(sMailBody)作为dict传递:
headers = {'authorization': 'Bearer d7a71dbb2d2cb6cd9f70126a926f6a4c', "Content-Type" : "multipart/form-data"}
files = {'file': open('1', 'rb')}
res = requests.post(url, json=sMailBody, headers=headers, files=files)
https://stackoverflow.com/questions/50620946
复制相似问题