【问题标题】:OTRS - REST-API - Send content of ticket including attachment via SMTPLIBOTRS - REST-API - 通过 SMTPLIB 发送票证内容,包括附件
【发布时间】:2019-05-21 00:12:42
【问题描述】:

我正在尝试通过 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()

【问题讨论】:

    标签: python-3.x mime-types smtplib otrs


    【解决方案1】:

    看起来这样可行:

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

    【讨论】:

      猜你喜欢
      • 2017-01-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 2015-05-16
      • 2016-03-23
      相关资源
      最近更新 更多