Search code examples
perlcpanpostfix-mtaemail-bounces

How can I delete a message without mailbox file lock? I'm using Perl's Mail::Box


I run Postfix on Ubuntu 16.04 server to send "internal email" messages and a crontab Perl job to parse the related bounce messages (delivered to local mailbox /var/mail/bounceparser). The Perl code basically checks the bounceparser mailbox, parse all the messages and take some actions (delete bounced addresses, etc).

The problem is that when I try to delete those already-parsed messages using Mail::Box library, the mailbox gets locked and if a new message arrives the postfix daemon throws an exception trying to deliver the message: "cannot update mailbox /var/mail/bounceparser for user bounceparser. cannot open file: Permission denied".

Is there a way to delete a message without locking the mailbox file? If it's not possible, any other suggested strategy?

The code I use to delete the messages:

my $mbox = Mail::Box::Mbox->new(folder =>'/var/mail/bounceparser', access => 'rw');

# @mailbox_pending_deletes contains the list of message ids to delete
for my $message_id (@mailbox_pending_deletes){
   $message = $mbox->find($message_id);
   $message->delete;    
}    
my $delete_result = $mbox->close(write=>'MODIFIED');

Thank you!


Solution

  • As suggested by @SteffenUllrich using mailbox single file box it's not a good idea (sincerely I was using it just because it's the default Postfix configured value ^_^).

    So, if you have a similar issue 1.- Configure Postfix to use Maildir instead of Mailbox for messages delivery (main.cf file):

    # Set Postfix to deliver messages to Maildir user folder
    home_mailbox = Maildir/
    

    and 2.- use Mail::Box:Maildir and not the Mail::Box:Mbox I was using to find-delete the messages.

    my $mbox = Mail::Box::Maildir->new(folder =>'/home/bounceparser/Maildir', access => 'rw');
    
    # @mailbox_pending_deletes contains the list of message ids to delete
    for my $message_id (@mailbox_pending_deletes){
       $message = $mbox->find($message_id);
       $message->delete;    
    }    
    my $delete_result = $mbox->close(write=>'MODIFIED');
    

    Fortunately the Sisimai library I use to parse the bounce/delivery/etc messages also accepts a Maildir path to go for the messages:

    my $v = Sisimai->make('/home/bounceparser/Maildir/new','hook'=>$x);
    

    Thanks for helping!