|
|
 |
 |
 |
 |
Perl Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Email::Folder->messages returns only one element
I'm working on parsing multiple messages out of one msf file(mbox folder type). I took the module of Email::Folder to construct with msf file, and then get the list of all messages contained in the msf file. But, the msf file is considered as a wholly one message, not multiple messages (actually I have multiple messages within the msf file) Here is my code snippet: my $folder = Email::Folder->new($file); //$file is a valid mbox folder type foreach ($folder->messages){ print $_->header("Subject"), "\n"; print $_->as_string(), "\n"; //$imapserver->append($box, $_->as_string()); // i want to import this message to my $imapserver, you can comment this one. }
The result is: The first print prints only the first message's subject. The second print prints all the messages(all of the other messages) in the msf file as a whole one. The Email::Folder parses this file as one mail message. Doesn't $folder->messages return a list of messages? Is there any where I missed to set the preference of parsing rule? Thanks.
Michael Yang wrote: > I'm working on parsing multiple messages out of one msf file(mbox > folder type). > I took the module of Email::Folder to construct with msf file, and > then get the list of all messages contained in the msf file. > But, the msf file is considered as a wholly one message, not multiple > messages (actually I have multiple messages within the msf file)
Does the file have proper message separators? -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
On May 31, 1:44 pm, Gunnar Hjalmarsson <nore@gunnar.cc> wrote: > Michael Yang wrote: > > I'm working on parsing multiple messages out of one msf file(mbox > > folder type). > > I took the module of Email::Folder to construct with msf file, and > > then get the list of all messages contained in the msf file. > > But, the msf file is considered as a wholly one message, not multiple > > messages (actually I have multiple messages within the msf file) > Does the file have proper message separators?
Actually I am not sure about what is the message separators the parser uses. The msf file and its mail content is generated by Thunderbird mail client locally. Where can I get the rules on valid format of MBox file, so that I can check if the file generated by Thunderbird is a valid one for Email::Folder to parse. Thanks.
On 05/30/2007 11:11 PM, Michael Yang wrote:
> I'm working on parsing multiple messages out of one msf file(mbox > folder type). > I took the module of Email::Folder to construct with msf file, and > then get the list of all messages contained in the msf file. > But, the msf file is considered as a wholly one message, not multiple > messages (actually I have multiple messages within the msf file) > Here is my code snippet: > my $folder = Email::Folder->new($file); //$file is a valid mbox > folder type > foreach ($folder->messages){ > print $_->header("Subject"), "\n"; > print $_->as_string(), "\n"; > //$imapserver->append($box, $_->as_string()); // i want to import > this message to my $imapserver, you can comment this one. > } > The result is: > The first print prints only the first message's subject. > The second print prints all the messages(all of the other messages) in > the msf file as a whole one. > The Email::Folder parses this file as one mail message. > Doesn't $folder->messages return a list of messages? > Is there any where I missed to set the preference of parsing rule? > Thanks.
Your code, with minor modifications, works for me. Hint: C++ comments are not Perl comments ;-) This is obviously not your real code, and you typed it in by hand without using cut-and-paste. http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html When I read the POD for Email::Folder, I see this helpful message: > messages > Returns a list containing all of the messages in the folder. Can only > be called once as it drains the iterator.
Maybe in your real code, you're doing a while or something rather than a foreach. Anyway, a code snippet is inadequate. You should create the smallest program that demonstrates your problem and ask for help with that.
On May 31, 2:33 pm, "Mumia W." <paduille.4061.mumia.w
+nos @earthlink.net> wrote: > On 05/30/2007 11:11 PM, Michael Yang wrote: > > I'm working on parsing multiple messages out of one msf file(mbox > > folder type). > > I took the module of Email::Folder to construct with msf file, and > > then get the list of all messages contained in the msf file. > > But, the msf file is considered as a wholly one message, not multiple > > messages (actually I have multiple messages within the msf file) > > Here is my code snippet: > > my $folder = Email::Folder->new($file); //$file is a valid mbox > > folder type > > foreach ($folder->messages){ > > print $_->header("Subject"), "\n"; > > print $_->as_string(), "\n"; > > //$imapserver->append($box, $_->as_string()); // i want to import > > this message to my $imapserver, you can comment this one. > > } > > The result is: > > The first print prints only the first message's subject. > > The second print prints all the messages(all of the other messages) in > > the msf file as a whole one. > > The Email::Folder parses this file as one mail message. > > Doesn't $folder->messages return a list of messages? > > Is there any where I missed to set the preference of parsing rule? > > Thanks. > Your code, with minor modifications, works for me. Hint: C++ comments > are not Perl comments ;-)
Is it because of the module version? I tried Mail::Box::Manager to parse the same file, it works. Because Mail::Box::Manager module has too many dependency modules to attach, I want to try Email::Folder instead to parse the message data. I need to wrap this harness as a package in the end.
> This is obviously not your real code, and you typed it in by hand > without using cut-and-paste. > http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html > When I read the POD for Email::Folder, I see this helpful message: > > messages > > Returns a list containing all of the messages in the folder. Can only > > be called once as it drains the iterator. > Maybe in your real code, you're doing a while or something rather than a > foreach.
The purpose of my script is quite simple. Parse the message data out of the local msf file, and then import each messages into mail server by Email::IMAPClient->append("INBOX", $msg); This snippet only deals with parsing the message data out of the file. There isn't anywhere Email::Folder module required anymore. I typed this snippet to show the problems that Email::Folder->messages doesn't function as it should do. In my snippet, sorry for that I paste it again: eval { require Email::Folder; }; die "The module Email::Folder is required for this script.\n" if $!; my $file = "mail"; //$file is a valid mbox folder,with "mail.msf" my $folder = Email::Folder->new($file); foreach ($folder->messages){ print $_->header("Subject"), "\n"; print $_->as_string(), "\n"; }
what I expected is that multiple messages contained in the "mail" file will be outputed. But only one is returned by this snippet. It's RHEL AS4 distribution with Perl 5.8.5 installed on my pc. I am wondering it...
> Anyway, a code snippet is inadequate. You should create the smallest > program that demonstrates your problem and ask for help with that.
On May 31, 3:11 pm, Michael Yang <Michael.@gmail.com> wrote:
> On May 31, 2:33 pm, "Mumia W." <paduille.4061.mumia.w > +nos@earthlink.net> wrote: > > On 05/30/2007 11:11 PM, Michael Yang wrote: > > > I'm working on parsing multiple messages out of one msf file(mbox > > > folder type). > > > I took the module of Email::Folder to construct with msf file, and > > > then get the list of all messages contained in the msf file. > > > But, the msf file is considered as a wholly one message, not multiple > > > messages (actually I have multiple messages within the msf file) > > > Here is my code snippet: > > > my $folder = Email::Folder->new($file); //$file is a valid mbox > > > folder type > > > foreach ($folder->messages){ > > > print $_->header("Subject"), "\n"; > > > print $_->as_string(), "\n"; > > > //$imapserver->append($box, $_->as_string()); // i want to import > > > this message to my $imapserver, you can comment this one. > > > } > > > The result is: > > > The first print prints only the first message's subject. > > > The second print prints all the messages(all of the other messages) in > > > the msf file as a whole one. > > > The Email::Folder parses this file as one mail message. > > > Doesn't $folder->messages return a list of messages? > > > Is there any where I missed to set the preference of parsing rule? > > > Thanks. > > Your code, with minor modifications, works for me. Hint: C++ comments > > are not Perl comments ;-) > Is it because of the module version? > I tried Mail::Box::Manager to parse the same file, it works. > Because Mail::Box::Manager module has too many dependency modules to > attach, I want to try Email::Folder instead to parse the message data. > I need to wrap this harness as a package in the end. > > This is obviously not your real code, and you typed it in by hand > > without using cut-and-paste. > >http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html > > When I read the POD for Email::Folder, I see this helpful message: > > > messages > > > Returns a list containing all of the messages in the folder. Can only > > > be called once as it drains the iterator. > > Maybe in your real code, you're doing a while or something rather than a > > foreach. > The purpose of my script is quite simple. Parse the message data out > of the local msf file, and then import each messages into mail server > by Email::IMAPClient->append("INBOX", $msg); > This snippet only deals with parsing the message data out of the file. > There isn't anywhere Email::Folder module required anymore. > I typed this snippet to show the problems that Email::Folder->messages > doesn't function as it should do. > In my snippet, sorry for that I paste it again: > eval { require Email::Folder; }; > die "The module Email::Folder is required for this script.\n" if $!; > my $file = "mail"; //$file is a valid mbox folder,with > "mail.msf" > my $folder = Email::Folder->new($file); > foreach ($folder->messages){ > print $_->header("Subject"), "\n"; > print $_->as_string(), "\n"; > } > what I expected is that multiple messages contained in the "mail" file > will be outputed. > But only one is returned by this snippet. > It's RHEL AS4 distribution with Perl 5.8.5 installed on my pc. > I am wondering it... > > Anyway, a code snippet is inadequate. You should create the smallest > > program that demonstrates your problem and ask for help with that.
I wrote another script to demonstrate my problems: 1 #!/usr/bin/perl -w 2 # Author: Michael Yang 3 # Date: May/31/2007 4 # Purpose: To test the function of Email::Folder module. 5 6 use strict; 7 use warnings; 8 9 my @mail_list = @ARGV; 10 11 die "Usage: $0 <.msf file> \n" if not @mail_list; 12 13 eval { require Email::Folder; }; 14 die "The module Email::Folder is required for this script.\n" if $!; 15 16 foreach my $file (@mail_list){ 17 if ($file =~ m/^(.*)\.msf$/i){ 18 $file = $1; 19 print "$file MSF file detected.\n" 20 } 21 die "can't access import file $file" unless -r $file; 22 23 my $folder = Email::Folder->new($file); 24 25 my @msg_list = $folder->messages(); 26 foreach(@msg_list){ 27 print $_->header("Subject"), "\n"; 28 # print $_->as_string(), "\n"; 29 } 30 } To run this script, one valid *.msf file is required to pass in, which contains multiple messages. In this script, it will print out each message's subject data. The result on my pc is that there is only one subject is printed out. Thanks all!!
On 05/31/2007 02:22 AM, Michael Yang wrote: > [ program demonstrating Email::Folder snipped ]
I ran your program. The only things I changed were lines 9 and 14: 9: my @mail_list = glob('~/tmp/mozmail/*.msf'); 14: die "The module Email::Folder is required for this script.\n" if $@; I had to change $! to $@. Read "perldoc -f eval." Your modified program ran properly; all of the message subjects were listed separately on my system: Debian 3.1 (Linux). Perl 5.8.4 Email::Folder 0.852 Email::Simple 1.9 Email::FolderType 0.7
On 05/31/2007 03:44 AM, Mumia W. wrote: > Email::Folder 0.852 > Email::Simple 1.9 > Email::FolderType 0.7
I forgot to mention that I installed the Debian versions of these modules. That's probably not important, but Debian sometimes fixes bugs in software when the upstream authors have not fixed them.
Michael Yang wrote: > I wrote another script to demonstrate my problems: > 1 #!/usr/bin/perl -w > 2 # Author: Michael Yang > 3 # Date: May/31/2007 > 4 # Purpose: To test the function of Email::Folder module. > 5 > 6 use strict; > 7 use warnings; > 8 > 9 my @mail_list = @ARGV; > 10 > 11 die "Usage: $0 <.msf file> \n" if not @mail_list; > 12 > 13 eval { require Email::Folder; }; > 14 die "The module Email::Folder is required for this script.\n" > if $!; > 15 > 16 foreach my $file (@mail_list){ > 17 if ($file =~ m/^(.*)\.msf$/i){ > 18 $file = $1; > 19 print "$file MSF file detected.\n" > 20 } > 21 die "can't access import file $file" unless -r $file; > 22 > 23 my $folder = Email::Folder->new($file); > 24 > 25 my @msg_list = $folder->messages(); > 26 foreach(@msg_list){ > 27 print $_->header("Subject"), "\n"; > 28 # print $_->as_string(), "\n"; > 29 } > 30 } > To run this script, one valid *.msf file is required to pass in, which > contains multiple messages. > In this script, it will print out each message's subject data. > The result on my pc is that there is only one subject is printed out.
A possible cause could be recognition of line endings. Does the mbox file stem from a different platform, was it transported via ASCII-FTP? To check that, you can test with different values for the "eol" parameter to Email::Folder's constructor:. # Windows-Format: my $folder = Email::Folder->new( $file, "eol" => "\r\n" ); # Mac-Format: # my $folder = Email::Folder->new( $file, "eol" => "\r" ); # Unix-Format: # my $folder = Email::Folder->new( $file, "eol" => "\n" ); -Chris
Michael Yang wrote: > On May 31, 1:44 pm, Gunnar Hjalmarsson <nore @gunnar.cc> wrote: >> Michael Yang wrote: >>> I'm working on parsing multiple messages out of one msf file(mbox >>> folder type). >>> I took the module of Email::Folder to construct with msf file, and >>> then get the list of all messages contained in the msf file. >>> But, the msf file is considered as a wholly one message, not multiple >>> messages (actually I have multiple messages within the msf file) >> Does the file have proper message separators? > Actually I am not sure about what is the message separators the parser > uses. The msf file and its mail content is generated by Thunderbird > mail client locally.
I run your code successfully on a Thunderbird generated mbox file. > Where can I get the rules on valid format of MBox file, so that I can > check if the file generated by Thunderbird is a valid one for > Email::Folder to parse.
Note that you stated in the OP that you used a valid mbox folder type... -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
On May 31, 5:41 pm, "Mumia W." <paduille.4061.mumia.w +nos @earthlink.net> wrote: > On 05/31/2007 03:44 AM, Mumia W. wrote: > > Email::Folder 0.852 > > Email::Simple 1.9 > > Email::FolderType 0.7 > I forgot to mention that I installed the Debian versions of these > modules. That's probably not important, but Debian sometimes fixes bugs > in software when the upstream authors have not fixed them.
I think I found the possible causes. The *.msf file I used to parse was generated on Window platform. After "dos2unix" to convert its format, it works. I then tried the file generated by thunderbird client on Linux platform, it is working well.
On May 31, 8:19 pm, Gunnar Hjalmarsson <nore@gunnar.cc> wrote:
> Michael Yang wrote: > > On May 31, 1:44 pm, Gunnar Hjalmarsson <nore @gunnar.cc> wrote: > >> Michael Yang wrote: > >>> I'm working on parsing multiple messages out of one msf file(mbox > >>> folder type). > >>> I took the module of Email::Folder to construct with msf file, and > >>> then get the list of all messages contained in the msf file. > >>> But, the msf file is considered as a wholly one message, not multiple > >>> messages (actually I have multiple messages within the msf file) > >> Does the file have proper message separators? > > Actually I am not sure about what is the message separators the parser > > uses. The msf file and its mail content is generated by Thunderbird > > mail client locally. > I run your code successfully on a Thunderbird generated mbox file. > > Where can I get the rules on valid format of MBox file, so that I can > > check if the file generated by Thunderbird is a valid one for > > Email::Folder to parse. > Note that you stated in the OP that you used a valid mbox folder type...
yes, I used the file generated thunderbird, so that .... I assumed it is a valid one. :) Finally it turned out I was wrong. I forgot about that it was generated on Window platform as I mentioned in the above replies. Thanks to all!
On May 31, 7:09 pm, Christian Winter <thepoet_nos@arcor.de> wrote:
> Michael Yang wrote: > > I wrote another script to demonstrate my problems: > > 1 #!/usr/bin/perl -w > > 2 # Author: Michael Yang > > 3 # Date: May/31/2007 > > 4 # Purpose: To test the function of Email::Folder module. > > 5 > > 6 use strict; > > 7 use warnings; > > 8 > > 9 my @mail_list = @ARGV; > > 10 > > 11 die "Usage: $0 <.msf file> \n" if not @mail_list; > > 12 > > 13 eval { require Email::Folder; }; > > 14 die "The module Email::Folder is required for this script.\n" > > if $!; > > 15 > > 16 foreach my $file (@mail_list){ > > 17 if ($file =~ m/^(.*)\.msf$/i){ > > 18 $file = $1; > > 19 print "$file MSF file detected.\n" > > 20 } > > 21 die "can't access import file $file" unless -r $file; > > 22 > > 23 my $folder = Email::Folder->new($file); > > 24 > > 25 my @msg_list = $folder->messages(); > > 26 foreach(@msg_list){ > > 27 print $_->header("Subject"), "\n"; > > 28 # print $_->as_string(), "\n"; > > 29 } > > 30 } > > To run this script, one valid *.msf file is required to pass in, which > > contains multiple messages. > > In this script, it will print out each message's subject data. > > The result on my pc is that there is only one subject is printed out. > A possible cause could be recognition of line endings. Does the mbox > file stem from a different platform, was it transported via ASCII-FTP? > To check that, you can test with different values for the "eol" > parameter to Email::Folder's constructor:. > # Windows-Format: > my $folder = Email::Folder->new( $file, "eol" => "\r\n" ); > # Mac-Format: > # my $folder = Email::Folder->new( $file, "eol" => "\r" ); > # Unix-Format: > # my $folder = Email::Folder->new( $file, "eol" => "\n" ); > -Chris- Hide quoted text - >
Yeah, I think that's the problem. I need to add the OS check at the beginning for different construction. Thanks to all of your guys. Really appreciated your helps!! Cheers! Michael
On May 31, 7:09 pm, Christian Winter <thepoet_nos@arcor.de> wrote:
> Michael Yang wrote: > > I wrote another script to demonstrate my problems: > > 1 #!/usr/bin/perl -w > > 2 # Author: Michael Yang > > 3 # Date: May/31/2007 > > 4 # Purpose: To test the function of Email::Folder module. > > 5 > > 6 use strict; > > 7 use warnings; > > 8 > > 9 my @mail_list = @ARGV; > > 10 > > 11 die "Usage: $0 <.msf file> \n" if not @mail_list; > > 12 > > 13 eval { require Email::Folder; }; > > 14 die "The module Email::Folder is required for this script.\n" > > if $!; > > 15 > > 16 foreach my $file (@mail_list){ > > 17 if ($file =~ m/^(.*)\.msf$/i){ > > 18 $file = $1; > > 19 print "$file MSF file detected.\n" > > 20 } > > 21 die "can't access import file $file" unless -r $file; > > 22 > > 23 my $folder = Email::Folder->new($file); > > 24 > > 25 my @msg_list = $folder->messages(); > > 26 foreach(@msg_list){ > > 27 print $_->header("Subject"), "\n"; > > 28 # print $_->as_string(), "\n"; > > 29 } > > 30 } > > To run this script, one valid *.msf file is required to pass in, which > > contains multiple messages. > > In this script, it will print out each message's subject data. > > The result on my pc is that there is only one subject is printed out. > A possible cause could be recognition of line endings. Does the mbox > file stem from a different platform, was it transported via ASCII-FTP? > To check that, you can test with different values for the "eol" > parameter to Email::Folder's constructor:. > # Windows-Format: > my $folder = Email::Folder->new( $file, "eol" => "\r\n" ); > # Mac-Format: > # my $folder = Email::Folder->new( $file, "eol" => "\r" ); > # Unix-Format: > # my $folder = Email::Folder->new( $file, "eol" => "\n" ); > -Chris- Hide quoted text - >
Yeah, I think that's the problem. I need to add the OS check at the beginning for different construction. Thanks to all of your guys. Really appreciated your helps!! Cheers! Michael
|
 |
 |
 |
 |
|