Hi, I am trying to read the body of the email after using new method of gmail API which is
email = service.get_user_message user_id, id
But when I try to fetch body using
email.payload.body.data. It gave me nil and even when I try to do the same thing in parts I am not getting the desired results.
I have wasted a lot of time searching for the exact answer and also in the documentation but couldn't find any right answer. Please help.
The MessagePart#body attribute is used when the message uses a singular content type, and MessagePart#parts is used when the message uses a multipart content type. Using one or the other should allow you to assemble the message body.
If you are still having troubles, I recommend trying the following to help you diagnose what the API is returning:
First, simply inspect the email by converting the data to a Ruby Hash and printing it:
require "pp"
pp email
To diagnose further, you can try passing the skip_deserialization option to see the JSON response that the API returns. Perhaps by inspecting the JSON you can infer how to pull the data out of the object.
email_string = service.get_user_message user_id, id, options: { skip_deserialization: true }
require "json"
email_hash = JSON.parse email_string
require "pp"
pp email_hash
FWIW, here is some code I've used to print out messages before. It checks for MessagePart#parts and prints that, or else it prints MessagePart#body.
results = service.list_user_messages "me", max_results: 5
results.messages.map do |message|
email = service.get_user_message "me", message.id
subject = email.payload.headers.find { |header| header.name == "Subject" }
puts subject.value if subject
if email.payload.parts
# always print the first content type body, usually plain text
puts email.payload.parts.first.body.data
else
puts email.payload.body.data
end
puts "-"*42
end
FWIW, here is some code I've used to print out messages before. It checks for
MessagePart#partsand prints that, or else it printsMessagePart#body.results = service.list_user_messages "me", max_results: 5 results.messages.map do |message| email = service.get_user_message "me", message.id subject = email.payload.headers.find { |header| header.name == "Subject" } puts subject.value if subject if email.payload.parts # always print the first content type body, usually plain text puts email.payload.parts.first.body.data else puts email.payload.body.data end puts "-"*42 end
I have tried this one, but the issue I am facing is the images or the styling of the message gets lost.. I could show a styled email as it is being shown inside our gmail inbox.
Most emails contain both a plain text and a HTML version. I would look at the mine_type of the different parts to find the one that has the styling you are looking for.
Thanks @blowmage that snippet was exactly what I was looking for. I don't know why I didn't see .parts as an instance method to .payload. Thanks!
Greetings, we're closing this. Looks like the issue got resolved. Please let us know if the issue needs to be reopened.
Most helpful comment
FWIW, here is some code I've used to print out messages before. It checks for
MessagePart#partsand prints that, or else it printsMessagePart#body.