Re: accessing mailbox files
by Limbic~Region (Chancellor) on Jul 28, 2003 at 21:50 UTC
|
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 | [reply] [d/l] |
|
|
| [reply] |
|
|
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
| [reply] |
|
|
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
| [reply] [d/l] |
|
|
| [reply] |
|
|
|
|
|
|
|
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
| [reply] [d/l] |
|
|
|
|
|
|
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 | [reply] [d/l] |
|
|
|
|
|
Re: accessing mailbox files
by pzbagel (Chaplain) on Jul 28, 2003 at 21:38 UTC
|
You need to explain what format your messages are actually in. First you mention that the mail files are binary, then you state that the files are in "mail format". To me, "mail format" implies Unix mbox format which is a simple text-based format to store mail on a Unix system. What program created these files? Outlook or Outlook Express? Try looking on CPAN for Outlook. If you tell us what mail program created these files, we could be more help.
HTH
| [reply] |
Re: accessing mailbox files
by Anonymous Monk on Jul 29, 2003 at 18:18 UTC
|
Hey everyone,
Finally got it to work. Thank you everyone for your helpfulness. I ended up using MIME::Lite. It was actually less complicated. I just had to open each file and parse the body. Thanks L~R for the pointers and your patience.
Sincerely,
jc | [reply] |