1

I am fetching emails from Outlook through Microsoft Graph or Rest Api directly from Ruby without using any Microsoft SDK.

The problem I am facing is that I can't get the email message body from the response returned by Microsoft. Using the Ruby Mail gem, I have tried to parse the response by doing:

 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     

which returns:

 "{\"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&nbsp;\\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 &lt; **@outlook.com&gt; </div>\\r\\n<div>Date: 09/01/2018 22:33 (GMT&#43;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\"}"

How do I parse this so I get the email body text, without the html markdown which should return httparty rocks.

For reference, here is a link to Microsoft's documentation for the api and this is the full api response that is returned to me:

- "@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&nbsp;\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 &lt; **@outlook.com&gt;
  </div>\r\n<div>Date: 09/01/2018 22:33 (GMT&#43;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: []
2
  • you mean you want to get email body text, without html markdown? Commented Jan 18, 2018 at 16:07
  • @nattfodd yes. The email body text without the html markdown should return ** httparty rocks**. Commented Jan 18, 2018 at 16:14

1 Answer 1

3

Probably the easiest way to do this is to ask the server to return the plain-text version of the body. From the docs:

Prefer: outlook.body-content-type: The format of the body and uniqueBody properties to be returned in. Values can be "text" or "html". If the header is not specified, the body and uniqueBody properties are returned in HTML format. Optional.

Here's how I specified the header in Postman:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.