Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help on reading emails with "mail" gem in ruby

Tags:

email

ruby

I am doing automation using Watir that creates an email that I need to check. I was pointed at the email gem as being the easiest way to do this.

I added following code and am able to get first email from my inbox.

require 'mail' 
require 'openssl'

Mail.defaults do 
  retriever_method :pop3, :address    => "email.someemail.com", 
                          :port       => 995, 
                          :user_name  => 'domain/username', 
                          :password   => 'pwd', 
                          :enable_ssl => true 
end 

puts Mail.first 

I am new to this forum and have following questions :

  1. How can I get all the unread emails? I tried Mail.all, Mail.first, Mail.last, but nothing returns unread email.

  2. How can I get all links that are present inside emails? Or mail message body from the specific email? I need to get the email body of first unread email.

  3. How can I get emails from a specific folder, if I have subfolders inside my inbox?

like image 801
user1356838 Avatar asked Apr 25 '12 17:04

user1356838


1 Answers

Section 6.4.4 of the IMAP protocol indicates the different search flags you can use to search for messages.

You can retrieve only new messages by passing the search flags to the find method like so:

new_messages = Mail.find(keys: ['NOT','SEEN'])

This message was also answered in an issue on the Mail GitHub repo.

like image 140
doremi Avatar answered Sep 19 '22 13:09

doremi