使用 ruby 从 Microsoft graph rest api outlook 消息中提取电子邮件正文
using ruby to extract email body from Microsoft graph rest api outlook message
我正在通过 Microsoft Graph 从 Outlook 或 Rest Api 直接从 Ruby 获取电子邮件,而不使用任何 Microsoft SDK。
我面临的问题是无法从 Microsoft returned 的响应中获取电子邮件正文。使用 Ruby 邮件 gem,我尝试通过以下方式解析响应:
response = HTTParty.get("https://graph.microsoft.com/v1.0/me/messages", headers: headers)
response_body = response.body
parsed_body = JSON.parse(response_body)
returned_email_values = parsed_body.dig('value')
first_email = returned_email_values.first
#I tried mail = Mail.read_from_string(first_email ) and also, mail = Mail.new(first_email )
mail = Mail.read_from_string(first_email )
mail.body.raw_source
其中 return 个:
"{\"contentType\"=>\"html\", \"content\"=>\"<html>\r\n<head>\r\n<meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=utf-8\\">\r\n<meta content=\\"text/html; charset=utf-8\\">\r\n</head>\r\n<body>\r\nLet's go \r\n<div><br>\r\n</div>\r\n<div style=\\"font-size:100%; color:#000000\\">\r\n<div>-------- Original message --------</div>\r\n<div>From: Well < **@outlook.com> </div>\r\n<div>Date: 09/01/2018 22:33 (GMT+00:00) </div>\r\n<div>To: **@yahoo.com </div>\r\n<div>Subject: it is httparty </div>\r\n<div><br>\r\n</div>\r\n</div>\r\nhttparty rocks\r\n</body>\r\n</html>\r\n\"}"
我该如何解析它才能得到电子邮件正文,没有 html markdown 应该 return httparty rocks.
作为参考,这里是 link 到 Microsoft's documentation for the api,这是 return 发给我的完整 api 回复:
- "@odata.etag": W/"CQAAABYAA***hjCagexQJczqiL***dRAAAxqsAe"
id: ****TAwCgBGAAAD2TG**i9L9AAAgE2n4YwmoHsUCXM6***
createdDateTime: '2018-01-09T23:07:52Z'
lastModifiedDateTime: '2018-01-09T23:07:52Z'
changeKey: **AAADafhjCag**JczqiL0v2dRAAA**
categories: []
receivedDateTime: '2018-01-09T23:07:52Z'
sentDateTime: '2018-01-09T23:08:36Z'
hasAttachments: false
internetMessageId: "<**FT028.enam01.prod.protection.outlook.com>"
subject: 'Re: it is httparty'
bodyPreview: "Let's go\r\n\r\n---- Original message -----\r\nFrom: Well <***@outlook.com>\r\nDate: 09/01/2018 22:33 (GMT+00:00)\r\nTo: ***@yahoo.com\r\nSubject: it is httparty\r\n\r\nhttparty rocks"
importance: normal
parentFolderId: **1LTgyOTYtMDACLTAwCA**0aVdJhhKHRI1AEA2**
conversationId: **NiZmYAZC0wYTc**gyOtMDACLTAwCg**
isDeliveryReceiptRequested:
isReadReceiptRequested: false
isRead: false
isDraft: false
webLink: https://outlook.live.com/owa/?ItemID=****1AcA2n4Yw**%3D&exvsurl=1&viewmodel=ReadMessageItem
inferenceClassification: focused
body:
contentType: html
content: "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=utf-8\">\r\n<meta content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>\r\nLet's
go \r\n<div><br>\r\n</div>\r\n<div style=\"font-size:100%; color:#000000\">\r\n<div>--------
Original message --------</div>\r\n<div>From: Well < **@outlook.com>
</div>\r\n<div>Date: 09/01/2018 22:33 (GMT+00:00) </div>\r\n<div>To: ***@yahoo.com
</div>\r\n<div>Subject: it is httparty </div>\r\n<div><br>\r\n</div>\r\n</div>\r\nhttparty
rocks\r\n</body>\r\n</html>\r\n"
sender:
emailAddress:
name: well
address: ***@yahoo.com
from:
emailAddress:
name: well
address:***@yahoo.com
toRecipients:
- emailAddress:
name: bee
address: **@outlook.com
ccRecipients: []
bccRecipients: []
replyTo: []
可能最简单的方法是要求服务器 return plain-text 版本的 body。来自 the docs:
Prefer: outlook.body-content-type
:body
和 uniqueBody
属性的格式 return。值可以是 "text" 或 "html".如果未指定 header,则 body
和 uniqueBody
属性以 HTML 格式 returned。可选。
这是我在 Postman 中指定 header 的方式:
我正在通过 Microsoft Graph 从 Outlook 或 Rest Api 直接从 Ruby 获取电子邮件,而不使用任何 Microsoft SDK。
我面临的问题是无法从 Microsoft returned 的响应中获取电子邮件正文。使用 Ruby 邮件 gem,我尝试通过以下方式解析响应:
response = HTTParty.get("https://graph.microsoft.com/v1.0/me/messages", headers: headers)
response_body = response.body
parsed_body = JSON.parse(response_body)
returned_email_values = parsed_body.dig('value')
first_email = returned_email_values.first
#I tried mail = Mail.read_from_string(first_email ) and also, mail = Mail.new(first_email )
mail = Mail.read_from_string(first_email )
mail.body.raw_source
其中 return 个:
"{\"contentType\"=>\"html\", \"content\"=>\"<html>\r\n<head>\r\n<meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=utf-8\\">\r\n<meta content=\\"text/html; charset=utf-8\\">\r\n</head>\r\n<body>\r\nLet's go \r\n<div><br>\r\n</div>\r\n<div style=\\"font-size:100%; color:#000000\\">\r\n<div>-------- Original message --------</div>\r\n<div>From: Well < **@outlook.com> </div>\r\n<div>Date: 09/01/2018 22:33 (GMT+00:00) </div>\r\n<div>To: **@yahoo.com </div>\r\n<div>Subject: it is httparty </div>\r\n<div><br>\r\n</div>\r\n</div>\r\nhttparty rocks\r\n</body>\r\n</html>\r\n\"}"
我该如何解析它才能得到电子邮件正文,没有 html markdown 应该 return httparty rocks.
作为参考,这里是 link 到 Microsoft's documentation for the api,这是 return 发给我的完整 api 回复:
- "@odata.etag": W/"CQAAABYAA***hjCagexQJczqiL***dRAAAxqsAe"
id: ****TAwCgBGAAAD2TG**i9L9AAAgE2n4YwmoHsUCXM6***
createdDateTime: '2018-01-09T23:07:52Z'
lastModifiedDateTime: '2018-01-09T23:07:52Z'
changeKey: **AAADafhjCag**JczqiL0v2dRAAA**
categories: []
receivedDateTime: '2018-01-09T23:07:52Z'
sentDateTime: '2018-01-09T23:08:36Z'
hasAttachments: false
internetMessageId: "<**FT028.enam01.prod.protection.outlook.com>"
subject: 'Re: it is httparty'
bodyPreview: "Let's go\r\n\r\n---- Original message -----\r\nFrom: Well <***@outlook.com>\r\nDate: 09/01/2018 22:33 (GMT+00:00)\r\nTo: ***@yahoo.com\r\nSubject: it is httparty\r\n\r\nhttparty rocks"
importance: normal
parentFolderId: **1LTgyOTYtMDACLTAwCA**0aVdJhhKHRI1AEA2**
conversationId: **NiZmYAZC0wYTc**gyOtMDACLTAwCg**
isDeliveryReceiptRequested:
isReadReceiptRequested: false
isRead: false
isDraft: false
webLink: https://outlook.live.com/owa/?ItemID=****1AcA2n4Yw**%3D&exvsurl=1&viewmodel=ReadMessageItem
inferenceClassification: focused
body:
contentType: html
content: "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html;
charset=utf-8\">\r\n<meta content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>\r\nLet's
go \r\n<div><br>\r\n</div>\r\n<div style=\"font-size:100%; color:#000000\">\r\n<div>--------
Original message --------</div>\r\n<div>From: Well < **@outlook.com>
</div>\r\n<div>Date: 09/01/2018 22:33 (GMT+00:00) </div>\r\n<div>To: ***@yahoo.com
</div>\r\n<div>Subject: it is httparty </div>\r\n<div><br>\r\n</div>\r\n</div>\r\nhttparty
rocks\r\n</body>\r\n</html>\r\n"
sender:
emailAddress:
name: well
address: ***@yahoo.com
from:
emailAddress:
name: well
address:***@yahoo.com
toRecipients:
- emailAddress:
name: bee
address: **@outlook.com
ccRecipients: []
bccRecipients: []
replyTo: []
可能最简单的方法是要求服务器 return plain-text 版本的 body。来自 the docs:
Prefer: outlook.body-content-type
:body
和 uniqueBody
属性的格式 return。值可以是 "text" 或 "html".如果未指定 header,则 body
和 uniqueBody
属性以 HTML 格式 returned。可选。
这是我在 Postman 中指定 header 的方式: