我试图将一个充满电子邮件的文件夹上传到Salesforce,每个案例一封电子邮件。该脚本可以很好地完成所有操作,但它上传的电子邮件完全空白,并且是0字节。
我非常确定它与上传附件所需的base64编码有关,但我已经尝试了一切以使其正常工作。
我读了这篇Uploading Attachments to Salesforce API via Beatbox, Python,这很有帮助,但我无法解决问题。
非常感谢任何帮助。
这是我的代码:
import wx
import email.parser
import os
import re
#import time
import beatbox
import base64
def browse(parent = None, message = 'Browse for the dealsheet folder'):
app = wx.App()
dialog = wx.DirDialog(None, "Choose a directory:",style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
if dialog.ShowModal() == wx.ID_OK:
return dialog.GetPath()
dialog.Destroy()
service = beatbox.PythonClient()
service.serverUrl = 'https://login.salesforce.com/services/Soap/u/20.0'
loginresponse = service.login('USERNAME', 'PASSWORD')
filespath = browse()
files = os.listdir(filespath)
global bodytext
global subject
global toAdd
global uploadfile
uploadfile = ''
for f in files:
with open(filespath + '\\' + f, 'rb') as emailfile:
message = email.message_from_file(emailfile)
uploadfile = base64.b64encode(emailfile.read())
subject = str(message.get('subject'))
toAdd = str(message.get('to'))
toAdd = re.findall(r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}', toAdd)[0]
toAdd = toAdd[:-10] # cut the email bits
toAdd = toAdd.replace('.', ' ')
toAdd = toAdd.title()
print toAdd
for part in message.walk():
if part.get_content_type() == 'text/plain':
bodytext = part.get_payload()
bodytext = str(bodytext)
bodytext = unicode(bodytext, errors = 'replace')
# print bodytext
# LOG CASE
details = {'type': 'Case', 'Reason': 'SFDC Admin', 'Origin': 'Email', 'Status': 'Awaiting Analysis', 'Description': ''}
result = service.create(details)
print 'log result = ', result[0]['success']
#TRIAGE CASE
details_triage = {'type': 'Case', 'id': result[0]['id'], 'Case_Triaged__c': 'True'}
triage_result = service.update(details_triage)
print 'triage result = ', triage_result[0]['success']
url = 'https://naX.salesforce.com/{}'.format(triage_result[0]['id'])
print url
attachement_dict = {'type': 'Attachment', 'ParentId': triage_result[0]['id'], 'name': f, 'Body': uploadfile }
if result[0]['success'] == True:
res = service.create(attachement_dict)
print res