Home > OS >  Using Ruby to get email attachment ID
Using Ruby to get email attachment ID

Time:09-17

I am trying to loop through a set of emails, and get their email attachment ID. I can't seem to get down to the ID, though.

I've gotten this far:

class AccessEmailController < ApplicationController

def access_email

    client = Signet::OAuth2::Client.new(access_token: session[:access_token])
    service = Google::Apis::GmailV1::GmailService.new
    service.authorization = client

    user_id = 'me'

    email_list = service.list_user_messages('me', q: "subject:(CCC Data)")
    
    message = 'No Emails Found' if email_list.messages.empty?
   
    email_list.messages.each do |message|
        message_id = message.id
        @subject = service.get_user_message(user_id, message_id).payload.parts

    end
end

end

And, if I display @subject in a view, I get:

[#<Google::Apis::GmailV1::MessagePart:0x00007f8630c397a0 @headers=[#<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c344a8 @name="Content-Type", @value="multipart/alternative; boundary=\"00000000000020396d05b7ceda38\"">], @mime_type="multipart/alternative", @body=#<Google::Apis::GmailV1::MessagePartBody:0x00007f8630c39250 @size=0>, @filename="", @part_id="0", @parts=[#<Google::Apis::GmailV1::MessagePart:0x00007f8630c30588 @headers=[#<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c2d2e8 @name="Content-Type", @value="text/plain; charset=\"UTF-8\"">], @mime_type="text/plain", @body=#<Google::Apis::GmailV1::MessagePartBody:0x00007f8630c2ed50 @size=91, @data="-- \r\nKim Floyd (Davis)\r\n\r\nWeb Developer, EvenVision\r\n707.845.3073\r\[email protected]\r\n">, @filename="", @part_id="0.0">, #<Google::Apis::GmailV1::MessagePart:0x00007f8630c2be98 @headers=[#<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c2a840 @name="Content-Type", @value="text/html; charset=\"UTF-8\"">, #<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c29ff8 @name="Content-Transfer-Encoding", @value="quoted-printable">], @mime_type="text/html", @body=#<Google::Apis::GmailV1::MessagePartBody:0x00007f8630c2b858 @size=344, @data="<div dir=\"ltr\"><br clear=\"all\"><div><br></div>-- <br><div dir=\"ltr\" class=\"gmail_signature\" data-smartmail=\"gmail_signature\"><div dir=\"ltr\">Kim Floyd (Davis)<div><br><div>Web Developer, EvenVision</div><div>707.845.3073</div><div><a href=\"mailto:[email protected]\" target=\"_blank\">[email protected]</a></div></div></div></div></div>\r\n">, @filename="", @part_id="0.1">]>, #<Google::Apis::GmailV1::MessagePart:0x00007f8630c28f68 @headers=[#<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c27b90 @name="Content-Type", @value="image/png; name=\"image.png\"">, #<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c26fd8 @name="Content-Disposition", @value="attachment; filename=\"image.png\"">, #<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c266f0 @name="Content-Transfer-Encoding", @value="base64">, #<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c25d40 @name="X-Attachment-Id", @value="f_kjdr80n00">, #<Google::Apis::GmailV1::MessagePartHeader:0x00007f8630c25250 @name="Content-ID", @value="<f_kjdr80n00>">], @mime_type="image/png", @body=#<Google::Apis::GmailV1::MessagePartBody:0x00007f8630c28a90 @size=459005, @attachment_id="ANGjdJ9LKnYW-GChtR3_oaBWnwi0DjHG4I8ZeDuMskuJ6htxLGmwNaB-v3x0k_cP94ybJFg0HLPO0AkSLWb-zXvF25qaGyhA20R89kwPSC9j3ttVEsBhByEAxrPkJQVwKuf3H8EblPhNNfPA7iYEJ7zT56do8-_UbMbDuXo-fVU445mtPHSYHWOEX_4z8aUyNTU3MCzNY8EHz90mJRpJnseelK3whaZdcJ2E98XmxQ">, @filename="image.png", @part_id="1">]

Which, does contain the attachment I am looking for. How can I get down to that attachment, though, and get it's ID?

CodePudding user response:

email_list.messages.each do |msg|
  message = gmail.get_user_message(user_id, msg.id)
  parts = message.payload.parts || []
  parts.each do |part|
    p part.body.attachment_id
  end
end
  • Related