6

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?

Dave McNulla
  • 2,006
  • 16
  • 23
Bhaveshsharma
  • 131
  • 2
  • 4
  • Watir does not have anything to do with your question. I am not sure how this works, but I think you should try to pick the right tags. – Dave McNulla Apr 26 '12 at 16:37

3 Answers3

14

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.

doremi
  • 14,921
  • 30
  • 93
  • 148
1

It looks like you read some of the documentation. Mail.all returns all the emails including read emails. Mail.first returns the first unread. I would imagine doing that again will return the next unread. If your system is controlled, you should not have to worry about so many unread emails. If I were you, I would try it out in IRB until you get a handle on it, maybe even build a class that makes it easier to use for you. Good luck.

Update: The documentation from the Mail gem. If the Mail.first doesn't return an unread email (all the time) then the author should know. You could submit an issue. I don't use that gem myself. I use a gmail gem for testing.

Mail.all    #=> Returns an array of all emails
Mail.first  #=> Returns the first unread email
Mail.last   #=> Returns the first unread email
Dave McNulla
  • 2,006
  • 16
  • 23
  • with Mail.First, it always retrive the first email of inbox. it never retrive first unread email from the mailbox. – Bhaveshsharma Apr 28 '12 at 19:17
  • I also want to only get all the unread mails. But I have the same problem: `Mail.all` retrieves all mails, `Mail.first` the first one (regardless whether it is read or not). – Joshua Muheim Aug 01 '12 at 15:31
0

how about saving the time when you read mails and then next time when you read all mails process mails only later than that date? i know itz a workaround but i believe it is not supported with the mail gem. Mailman gem does support that but it deletes the mail once you process it.

Manish Puri
  • 422
  • 4
  • 6