Project

General

Profile

Actions

Patch #14101

closed

Receive IMAP by uid's

Added by Pierre Pretorius over 11 years ago. Updated about 11 years ago.

Status:
Closed
Priority:
Normal
Category:
Email receiving
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

This is not a new bug, it has been reported in the forums by other users here and here, no solution was proposed so here is mine:

When running the redmine:email:receive_imap with the folder, move_on_success and move_on_failure parameter I randomly get the error below, although it seems like it never occurs when there is only a single or few unread mails. It happens with a wide variety of Redmine versions although I'm running 2.3.1 with all the latest stable gem versions.

rake aborted!
undefined method `[]' for nil:NilClass
/opt/bitnami/apps/redmine/htdocs/lib/redmine/imap.rb:33:in `block in check'
/opt/bitnami/apps/redmine/htdocs/lib/redmine/imap.rb:32:in `each'
/opt/bitnami/apps/redmine/htdocs/lib/redmine/imap.rb:32:in `check'
/opt/bitnami/apps/redmine/htdocs/lib/tasks/email.rake:133:in `block (3 levels) in <top (required)>'
/opt/bitnami/ruby/lib/ruby/gems/1.9.1/gems/rake-10.0.3/lib/rake/task.rb:228:in `call'

The problem comes down to this line of code in lib/redmine/imap.rb:

imap.fetch(message_id,'RFC822')[0].attr['RFC822']

After debugging I can confirm that the error occurs when calling [0] which means that imap.fetch returns nil. If I add a simple nil check and do not process the mail further if it is nil, then the mail is still left unread in the gmail mailbox, but the label (folder the imap task reads from) has been removed which stil means a mail will be missed even if the task runs again.

When looking at online examples, I see most use the uid methods instead of the standard one. I was able to solve this problem by changing lib/redmine/imap.rb to use the uid methods as listed below:

        imap.uid_search(['NOT', 'SEEN']).each do |uid|
          msg = imap.uid_fetch(uid,'RFC822')[0].attr['RFC822']
          logger.debug "Receiving message #{uid}" if logger && logger.debug?
          if MailHandler.receive(msg, options)
            logger.debug "Message #{uid} successfully received" if logger && logger.debug?
            if imap_options[:move_on_success]
              imap.uid_copy(uid, imap_options[:move_on_success])
            end
            imap.uid_store(uid, "+FLAGS", [:Seen, :Deleted])
          else
            logger.debug "Message #{uid} can not be processed" if logger && logger.debug?
            imap.uid_store(uid, "+FLAGS", [:Seen])
            if imap_options[:move_on_failure]
              imap.uid_copy(uid, imap_options[:move_on_failure])
              imap.uid_store(uid, "+FLAGS", [:Deleted])
            end
          end
        end

Even when the nil pointer is not thrown with the original Redmine code, the move_on_succes and move_on_failure doesn't work correctly every time. In the mailbox I see some messages are left with only the success label and some with the original and succes label. Moving to uid's solved this problem as well.


Files

receive_imap_by_uid.patch (2.04 KB) receive_imap_by_uid.patch Pierre Pretorius, 2013-05-20 15:18

Related issues

Related to Redmine - Patch #14103: Disconnect and logout from IMAP after mail receiveClosedJean-Philippe Lang

Actions
Actions

Also available in: Atom PDF