OTRS - REST-API - 通过 SMTPLIB 发送包含附件的工单内容

OTRS - REST-API - Send content of ticket including attachment via SMTPLIB

我正在尝试通过 REST-API 从 OTRS v4 获取工单的内容并将内容发送到外部邮件地址。

一切正常,除了附件部分。它发送了一个附件,但文件不可读,甚至与源文件大小不同。

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()

看起来像这样:

# 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()