Re: newbie question for parsing incoming mails

2017-02-16 Thread Jim Gibson

> On Feb 15, 2017, at 9:56 PM, Eko Budiharto  wrote:
> 
> Jim,
> I have one a couple more questions.
> -. For the header, what if, I just need the subject, the from, and the 
> recipient, what is the command? I read the manual in the 
> https://metacpan.org/pod/Email::MIME#header, it does not tell me how to 
> extract all of those.
> -. And then, for the email contents of the body, it just shows Content-Type: 
> multipart/alternative; boundary=f403045de9521d20cc054874ce1a 
> instead of the contents of the email body. How to extract it into string 
> because it is under html format.

Please post all questions to the list. There are other people who can answer 
your questions better than I can.

I just parse the text of the email using regular expressions, line by line:

my @lines = split(/[\n\r]+/,$text);

for( my $i = 0; $i <= $#lines; $i++ ) {
my $line = $lines[$i];
print "$i. $line\n" if $xdebug;

if( $line =~ m{ \A Subject:\ (.*) \z }x ) {
$subject = $1;

etc.

You can also try to use the Email::MIME::header method:

my $from = $mime->header(‘From’):

I haven’t used that, but it might be worth trying. Email::MIME is an extension 
of the Email::Simple class, so you can look at the documentation for that 
module as well. 
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question for parsing incoming mails

2017-02-15 Thread Eko Budiharto
dear all,
I have one a couple questions.
-. For the header, what if, I just need the subject, the from, and the 
recipient, what is the command? I read the manual in the 
https://metacpan.org/pod/Email::MIME#header 
, it does not tell me how to 
extract all of those.
-. And then, for the email contents of the body, it just shows Content-Type: 
multipart/alternative; boundary=f403045de9521d20cc054874ce1a 
instead of the contents of the email body. How to extract it into string 
because it is under html format.

> On Feb 16, 2017, at 11:45, Jim Gibson  wrote:
> 
> 
>> On Feb 15, 2017, at 8:10 PM, Eko Budiharto  wrote:
>> 
>> dear Jim,
>> I tried to add lines to read file like this:
>> 
>> use Email::MIME;
>> 
>> my $file = '/var/qmail/mailnames/> name>/support/Maildir/cur/1487041394.M984019P23084V0803I00E03878.ABCD.NET,S=3987:2,';
>> open my $ifh, '<', $file
>>  or die "Cannot open '$file' for reading: $!";
>> local $/ = '';
>> my $contents = <$ifh>;
>> close( $ifh );
>> 
>> my $mime = Email::MIME->($contents);  —> line 13
>> 
>> my @parts = $mime->parts();
>> 
>> for my $npart ( 0..$#parts ) {
>>  my $part = $parts[$npart];
>>  my $header = $part->header_obj();
>>  my $htext = $header->as_string();
>>  my $body = $part->body();
>> 
>>  print $header;
>> }
>> 
>> after I ran its from CLI, I got an error message Undefined subroutine 
>> ::MIME called at line 13.
>> 
>> what does the error mean?
> 
> That means I mistyped the line. It should be this:
> 
>   my $mime = Email::MIME->new($contents);
> 
> 



Re: newbie question for parsing incoming mails

2017-02-15 Thread Jim Gibson
On Feb 15, 2017, at 7:10 PM, Eko Budiharto  wrote:
> 
> Jim, 
> if I want to extract all incoming emails from my qmail emails, how can 
> specify the folder location and specify the file name since the file name 
> always different?
> 
> Thx.

Use File::Find or opendir and readdir to find all of the emails in a directory 
tree. You need to figure out the parent directory, if there is one, of the 
local storage for your email client.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question for parsing incoming mails

2017-02-15 Thread Jim Gibson

> On Feb 15, 2017, at 8:10 PM, Eko Budiharto  wrote:
> 
> dear Jim,
> I tried to add lines to read file like this:
> 
> use Email::MIME;
> 
> my $file = '/var/qmail/mailnames/ name>/support/Maildir/cur/1487041394.M984019P23084V0803I00E03878.ABCD.NET,S=3987:2,';
> open my $ifh, '<', $file
>   or die "Cannot open '$file' for reading: $!";
> local $/ = '';
> my $contents = <$ifh>;
> close( $ifh );
> 
> my $mime = Email::MIME->($contents);  —> line 13
> 
> my @parts = $mime->parts();
> 
> for my $npart ( 0..$#parts ) {
>   my $part = $parts[$npart];
>   my $header = $part->header_obj();
>   my $htext = $header->as_string();
>   my $body = $part->body();
> 
>   print $header;
> }
> 
> after I ran its from CLI, I got an error message Undefined subroutine 
> ::MIME called at line 13.
> 
> what does the error mean?

That means I mistyped the line. It should be this:

my $mime = Email::MIME->new($contents);

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: newbie question for parsing incoming mails

2017-02-15 Thread Eko Budiharto
dear Jim,
I tried to add lines to read file like this:

use Email::MIME;

my $file = '/var/qmail/mailnames//support/Maildir/cur/1487041394.M984019P23084V0803I00E03878.ABCD.NET,S=3987:2,';
open my $ifh, '<', $file
  or die "Cannot open '$file' for reading: $!";
local $/ = '';
my $contents = <$ifh>;
close( $ifh );

my $mime = Email::MIME->($contents);  —> line 13

my @parts = $mime->parts();

for my $npart ( 0..$#parts ) {
  my $part = $parts[$npart];
  my $header = $part->header_obj();
  my $htext = $header->as_string();
  my $body = $part->body();

  print $header;
}

after I ran its from CLI, I got an error message Undefined subroutine 
::MIME called at line 13.

what does the error mean?



> On Feb 15, 2017, at 22:56, Jim Gibson  wrote:
> 
> I use Email::MIME to parse email messages. After reading the email file into 
> the variable $text I do this:
> 
> my $mime = Email::MIME->($text);
> my @parts = $mime->parts();
> 
> for my $npart ( 0..$#parts ) {
>  my $part = $parts[$npart];
>  my $header = $part->header_obj();
>  my $htext = $header->as_string();
>  my $body = $part->body();
>  …
> }
> 
> 
> 
> 
> 
> Jim Gibson



Re: newbie question for parsing incoming mails

2017-02-15 Thread Eko Budiharto
Jim, 
if I want to extract all incoming emails from my qmail emails, how can specify 
the folder location and specify the file name since the file name always 
different?

Thx.
> On Feb 15, 2017, at 22:56, Jim Gibson  wrote:
> 
>> 
>> On Feb 14, 2017, at 10:38 PM, Eko Budiharto  wrote:
>> 
>> dear all,
>> I have a question. 
>> If I would like to parse all incoming mails from my qmail, which perl module 
>> is easy to use?
>> my qmail emails incoming is /var/qmail/mailnames//support. In 
>> this folder I already have preline in .qmail for piping emails to my perl 
>> script.
>> And, my perl script what I already have is like this:
>> 
>> #!/usr/bin/perl -w
>> 
>> use Mail::Internet;
>> 
>> my $mail = Mail::Internet->new( [  ] );
>> my $headers = $mail->head->header_hashref;
>> my @subject = @{${$headers}{'Subject'}}; # or otherwsie do it in list 
>> context it works
>> 
>> print @subject;
>> 
>> 
>> when I run it, I do not get anything.
>> 
>> please help.
> 
> I use Email::MIME to parse email messages. After reading the email file into 
> the variable $text I do this:
> 
> my $mime = Email::MIME->($text);
> my @parts = $mime->parts();
> 
> for my $npart ( 0..$#parts ) {
>  my $part = $parts[$npart];
>  my $header = $part->header_obj();
>  my $htext = $header->as_string();
>  my $body = $part->body();
>  …
> }
> 
> 
> 
> 
> 
> Jim Gibson



Re: newbie question for parsing incoming mails

2017-02-15 Thread Andy Bach
> when I run it, I do not get anything.
Hmm, how are you testing it. I put a single header and msg in /tmp/ml.txt
(Subject: training.error Trouble wiwb) and:
$ parse_email_simple.pl < /tmp/ml.txt
training.error Trouble wiwb (8 lines)
$ cat /tmp/ml.txt | parse_email_simple.pl
training.error Trouble wiwb (8 lines)

In addition, if I take out STDOUT
my $mail = Mail::Internet->new( [ <> ] );

I can do:
$ parse_email_simple.pl  /tmp/ml.txt

as Perl'll open the command line file name argument

Note, these'll treat all input as a single msg.

On Wed, Feb 15, 2017 at 12:38 AM, Eko Budiharto 
wrote:

> dear all,
> I have a question.
> If I would like to parse all incoming mails from my qmail, which perl
> module is easy to use?
> my qmail emails incoming is /var/qmail/mailnames//support.
> In this folder I already have preline in .qmail for piping emails to my
> perl script.
> And, my perl script what I already have is like this:
>
> #!/usr/bin/perl -w
>
> use Mail::Internet;
>
> my $mail = Mail::Internet->new( [  ] );
> my $headers = $mail->head->header_hashref;
> my @subject = @{${$headers}{'Subject'}}; # or otherwsie do it in list
> context it works
>
> print @subject;
>
>
> when I run it, I do not get anything.
>
> please help.
>
> regards,
> Eko
>



-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: newbie question for parsing incoming mails

2017-02-15 Thread Jim Gibson

> On Feb 14, 2017, at 10:38 PM, Eko Budiharto  wrote:
> 
> dear all,
> I have a question. 
> If I would like to parse all incoming mails from my qmail, which perl module 
> is easy to use?
> my qmail emails incoming is /var/qmail/mailnames//support. In 
> this folder I already have preline in .qmail for piping emails to my perl 
> script.
> And, my perl script what I already have is like this:
>  
> #!/usr/bin/perl -w
>  
> use Mail::Internet;
> 
> my $mail = Mail::Internet->new( [  ] );
> my $headers = $mail->head->header_hashref;
> my @subject = @{${$headers}{'Subject'}}; # or otherwsie do it in list context 
> it works
> 
> print @subject;
> 
> 
> when I run it, I do not get anything.
> 
> please help.

I use Email::MIME to parse email messages. After reading the email file into 
the variable $text I do this:

my $mime = Email::MIME->($text);
my @parts = $mime->parts();

for my $npart ( 0..$#parts ) {
  my $part = $parts[$npart];
  my $header = $part->header_obj();
  my $htext = $header->as_string();
  my $body = $part->body();
  …
}





Jim Gibson

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/