我正在尝试通过OTRS v4中的REST-API获取票证的内容,并将其发送到外部邮件地址。
一切正常,除了附件部分。它发送附件,但文件不可读,甚至与源文件大小不同。
OTRS REST-API的文档(TicketGet):http://doc.otrs.com/doc/api/otrs/5.0/Perl/Kernel/GenericInterface/Operation/Ticket/TicketGet.pm.html
# get the data vie REST from OTRS
TicketID = "12345"
headers = {
"Content-Type": 'application/json'
}
payload = {
'UserLogin': 'user',
"Password": 'pass',
"Extended": '1',
'AllArticles': '1',
'Attachments': '1',
}
url = 'https://somedomain.com/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/TicketGet/' + TicketID
result = requests.get(url, headers=headers, data=json.dumps(payload))
data = result.json()
# send content of ticket as an email to "soemone@somedmain.com"
email_subject = data['Ticket'][0]['Article'][0]['Subject']
email_body = data['Ticket'][0]['Article'][0]['Body']
msg = MIMEMultipart()
msg['From'] = "noreply@somedomain.com"
msg['To'] = "soemone@somedmain.com"
msg['Subject'] = email_subject
# text
msg.attach(MIMEText(email_body, 'plain'))
# attachment
attachment_content_type = data['Ticket'][0]['Article'][0]['Attachment'][0]['ContentType'] #outputs for example:
attachment_content = data['Ticket'][0]['Article'][0]['Attachment'][0]['Content'] #base64 ?
attachment_filename = data['Ticket'][0]['Article'][0]['Attachment'][0]['Filename'] #outputs for example:
mimetype = attachment_content_type.split(';')[0]
basetype, subtype = mimetype.split('/', 1)
att = MIMEBase(basetype, subtype)
att.set_payload(attachment_file)
att.add_header('Content-Disposition', 'attachment', filename=attachment_filename)
msg.attach(att)
# send msg
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
答案 0 :(得分:0)
看起来像这样:
# attachment
attachment_content = data['Ticket'][0]['Article'][0]['Attachment'][0]['Content']
attachment_filename = data['Ticket'][0]['Article'][0]['Attachment'][0]['Filename']
filedata = base64.b64decode(attachment_content)
att = MIMEBase('application', 'octet-stream')
att.set_payload(filedata)
encoders.encode_base64(att)
att.add_header('Content-Disposition', 'attachment', filename=attachment_filename)
msg.attach(att)
# send msg
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()