我正在尝试将一个充满电子邮件的文件夹上传到 Salesforce,每个案例一封电子邮件。该脚本一切正常,但它上传的电子邮件完全是空白的,并且是 0 字节。
我很确定它与上传附件所需的 base64 编码有关,但我已经尝试了一切以使其正常工作。
我通过 Beatbox, Python阅读了这个Uploading Attachments to Salesforce API ,这很有帮助,但我无法解决问题。
任何帮助将不胜感激。
这是我的代码:
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