in reply to accessing mailbox files

jc23,
Ok - *nix mailboxes are not multiple messages stored in multiple files scattered in multiple directories. It is a single mbox file (usually /var/mail/<account>). I believe the first thing you need to do is describe the process that gets these messages to these files in these directories.

First you asked how Sendmail used .forward files to forward a user's mail - which I answered. Then you asked , but how do I do that selectively for different directories - I got very confused. So before you describe the process that takes these messages, creates unique file names, and determines which directory to store them in based off some criteria - let me make a few assumptions:

  • The messages are coming from Sendmail and have not been modified in any other way
  • 1 Message per 1 file

    It is then that I would reccommend using Mail::Audit which uses MIME:Entity for MIME (your binary issue I believe) as described in this node where I asked about Email munging. Mail::Audit does not need to read in from a pipe, so it can take any array that looks like a message and re-direct it to any account you want:

    # Set up your loop here next unless open (MESSAGE, "message.file"); my @message = <MESSAGE>; my $mail = Mail::Audit->new(data => \@message); my $recip = 'email@address.com'; $mail->replace_header('To', $recip); $mail->resend($recip); next;

    That should get you started if the assumptions I made were correct.

    Cheers - L~R

  • Replies are listed 'Best First'.
    Re: Re: accessing mailbox files
    by tedrek (Pilgrim) on Jul 28, 2003 at 22:19 UTC

      I just want to point out that depending on your MTA mail can be delivered to a maildir format box instead of mbox format, in which case you would have multiple messages each in their own file inside of one directory.

      of course you could also have your mail delivered to the moon dependant on your MTA :P
        tedrek,
        I will not to pretend to be an expert at Sendmail, but that is the MTA referenced in previous threads. It has been my experience on every *nix box I have been on that it is a single mbox file. I also admit that Sendmail has been around forever and could probably be configured to cook dinner if you wanted to - so point taken. It doesn't change the fact that this isn't the "out of the box" configuration for Sendmail, so it caught myself (as well as others) off guard.

        Cheers - L~R

    Re: Re: accessing mailbox files
    by jc23 (Acolyte) on Jul 28, 2003 at 22:23 UTC
      Hey L~R,

      Thanks for your patience. You are correct in your assumptions. There are messages in a temp folder received via Sendmail, that I would like to forward to another account. The messages have not been modified. They are simply directly forwarded. It's basically like an auto forwarding script out of one directory. I'm not sure what the exact format of email messages are in a folder, so I assume each message was its own file. Please correct me on that if I am wrong. I have integrated your code with a modified version of the script I was writing. I'm pretty sure there are things wrong with it so please let me know. =).

      use strict; use Cwd; use Lite; use Mail:Audit; my $dirpath; my $divider = 0; my $input; my $mailprog = '/usr/lib/sendmail'; my $recipient= 'recip@email.com'; my $sender = 'jc@email.com'; my $dirpath = '/var/mail/user/temp'; unless ( chdir $dirpath ) #change to directory { exit(0); } unless (opendir( CHANGEME, $dirpath )){ # print "\nCan't open $dirpath. "; exit (0); } #find all directory contents OTHER than those that begin in . my @dirContents = grep !/^\./, readdir(CHANGEME); if(!@dirContents){ # print "No files were found inside $dirpath\n"; exit (0); } #forward each email message to recipient foreach my $dirContent ( @dirContents ) { open (MESSAGE, "$dirContent"); my @message = <MESSAGE>; my $mail = Mail::Audit->new(data => \@message); my $recip = $recipient; $mail->replace_header('To', $recip); $mail->resend($recip); `rm $dirContent`; } closedir ( CHANGEME ); exit (0);

      Could this also be done using MIME::Lite? Thanks for the tips!

      jc

        jc23,
        Ok - you look like you are well on your way. I have a few points:
      • my $recip = $recipient; is redundent. Just define $recipient as $recip at the top.
      • `rm $dirContent`; You should never use backticks in a void context. Use system if you are not concerned with capturing the output. In this case, I feel you should use neither. Perl has unlink.
      • I would recommend Perltidy for help in indenting/formatting.
      • A lot of your variables look to be used only once. If this is left over stuff, take the time to clean it up. It can help troubleshooting later.

        Finally - you still haven't described what process gets those messages to the files in the directory in the first place:

      • Is it a special Sendmail configuration?
      • Is it an alias that is actually a pipe to a script?
      • Is it something else?
        It is possible you could solve this problem earlier by putting in a fork: Write to this directory as well as forward a copy to a different recipient. This is probably the best thing to do anyway.

        Cheers - L~R

          L~R,

          Here's the big picture: The user chooses to send a bunch of messages to an alias that will later post everything onto a webboard. Instead of sending each mail separate, the user can drag and drop all da mail files he wants posted or any original email he writes into the folder *temp*. THe script I was asking for help on, will then go through the folder and "forward" each piece of mail to the alias. I wanted this "Forwarding" step to be automatic.

          I'm not sure what you mean by *forks*. If this is a more efficient process than the script, could you please tell me how to set this up?

          Lastly, someone mentioned that all messages in a mailbox is kept in one file. Does the code you posted earlier allow each message to be grabbed from the one index file and sent out separately?

          thanks for the help.

          jc

          PS I will go back and clean up the variables and formatting. I pulled that portion of it out of a larger context.

        Reinventing the wheel and all that. I sound like a broken record, I guess, but I have some questions.

        Q: It looks to me like if the $dirContent file has more than one message in it, you won't forward other than the first messages. Is that the case?

        Q: Are you just forwarding ALL of the mail sent to a given user? If so, how is this superior to a .forward file in the user's home directory that contains: recip@email.com?

        Q: If you are filtering mail to a user based on some criteria, how is this superior to using procmail? e.g. a .procmailrc file that filters for Perlmonks in the subject:

        :0: * ^Subject:.*Perlmonks.* ! recip@email.com
          Saintbrie,

          To clarify, I am forwarding all messages in a single local mail folder, not all contents of inbox. I'm not sure how to do this in procmail, so I haven't used that laternative.

          Secondly, mail in the folder that is being forwarded, isn't incoming mail. It is mail that has been drag and dropped from a separate mail folder using dtmail. In other words, it is mail that the user has chosen to send out to a single alias. By putting all the mail to be sent to one address in a folder and having it automatically send, it is less tedious.

          Hope that clarifies. jc

          saintbrie,

          Regarding the procmailrc file, will it detect keywords from the subject of mail that is not incoming mail, but mail placed into the folder by the user?

          thanks.

          jc

        If all you are trying to do is to take some raw email messages in files and forward them to another account, then look at using formail. It is installed along side sendmail, and is useful for parsing email messages.

        I have used the following to send an entire mbox file to another user:

        cat <mboxfile> | formail +1 -ds sendmail -oem <dest-address>

        I admit that I have not read your other questions, so I may be off base with what you are trying to achieve, but I always think it wise to look at the tools you have available with your mail system before starting to write your own...

        - Cees

          Cees,

          Sending raw email messages to another mbox is exactly what I want to do. However, I want each message in the original mbox to be a separate email in the destination mboxl and not one big file.Will formail allow this?

          thanks

          jc