message.eml path for mail gem ruby on rails

2019-09-02 03:46发布

问题:

I am trying to read my gmail inbox using the mail gem. I am able to get the message array using Mail.last.

Now I want to READ this message. The documentation says--

ail = Mail.read('/path/to/message.eml')

mail.envelope.from   #=> 'mikel@test.lindsaar.net'
mail.from.addresses  #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
mail.sender.address  #=> 'mikel@test.lindsaar.net'
mail.to              #=> 'bob@test.lindsaar.net' 
mail.cc              #=> 'sam@test.lindsaar.net'
mail.subject         #=> "This is the subject"
mail.date.to_s       #=> '21 Nov 1997 09:55:06 -0600'
mail.message_id      #=> '<4D6AA7EB.6490534@xxx.xxx>'
mail.body.decoded    #=> 'This is the body of the email...

Now, the problem remailns - what is /path/to/message/eml ? How do i create/locate this EML file?

Thanks.

回答1:

I've not used this gem, but looking at the README, should be that Mail.last returns an instance of Mail.

You say Mail.lasts works, i.e. it retrieves the email from gmail given your settings https://github.com/mikel/mail#getting-emails-from-a-pop-server

What happens when you do:

mail = Mail.last
mail.body.decoded


回答2:

I know it's a bit late to answer, to say the least.. But hey if anyone will be able to use it:

Once you iterate over the mails, you can just use something like this.

mails = Mail.all

mails.each do |current_mail|
    mail_object = Mail.read_from_string(current_mail)
    puts mail_object.to # Outputs the To address 
end

I also suggest checking the full documentation here: http://www.rubydoc.info/github/mikel/mail/Mail

Cheers :)