Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

Using Ruby to get email attachment ID

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="-- 
Kim Floyd (Davis)

Web Developer, EvenVision
707.845.3073
[email protected]
">, @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>
">, @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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...