Link to home
Start Free TrialLog in
Avatar of sureshp
sureshp

asked on

Separate body part and attachments from mail

Hi all,
Following cgi code is used to display the messages from the INBOX using IMAPClient module. I would like to separate the message part and attachments.

Here is my code:
 #!/usr/bin/perl
use Mail::IMAPClient;
use Getopt::Std;
print "Content-type: text/html\n\n";
      $imaphost='mailc.com';
      $login='testc';
      $pass='testd';
      my $imap = Mail::IMAPClient->new( Server => $imaphost,
                                User   => $login,
                                Password=> $pass,
                                Uid => 1,            # optional
      );



getopts('dv');
my %folders; my %counts;
FOLDER: foreach my $f ($imap->folders) {
      $folders{$f} = 0;
      $counts{$f} = $imap->message_count($f);
      print "Processing folder $f\n" if $opt_v;
      unless ( $imap->select($f)) {
            warn "Error selecting $f: " . $imap->LastError . "\n";
            next FOLDER;
      }
      my @msgs = $imap->search("ALL");
      my %hash = ();
      foreach my $m (@msgs) {
            
            my $to1 = $imap->parse_headers($m,"To")->{'To'}[0];
        my $SUB = $imap->parse_headers($m,"Subject")->{'Subject'}[0];
            my $size = $imap->size($m);
            if ( exists $hash{$mid} and $hash{$mid} == $size ) {
                  $imap->delete_message($m) if $opt_d;
                  $folders{$f}++;
                  print "Found a duplicate in $f\n" if $opt_v;
            } else {
                  $hash{$mid} = $size;
            }
      }
}
foreach my $k (keys %folders) { $total += $folders{$k}}

my $totms;
map { $totms += $_ } values %counts;

#print       "Found <b>$total</b> duplicate messages in $uid's mailbox.<br> The breakdown is:\<br>",
#      "\tFolder - \tNumber of Duplicates - \tNumber of Msgs in Folder\<br>",
#      "\t------\t--------------------\t------------------------\<br>",
#      map { "\t$_ - \t$folders{$_} - \t$counts{$_}  \<br>" } keys %folders,
#      "\tTOTAL - \t$total - \t$totms  \<br><br>";

my $message_count = $imap->message_count("INBOX");

for ($i=1;$i<=$message_count;$i++)
{
$lis = $imap->message_string($i);

my $to1 = $imap->parse_headers($i,"To")->{'To'}[0];
my $SUB = $imap->parse_headers($i,"Subject")->{'Subject'}[0];
print "$i:"; print "$lis<br>";
}



while displaying,I am getting output as:

1: Return-Path: Received: from john (john [192.168.1.43]) by traffic.com (8.9.3/8.8.7) with SMTP id JAA23037 for ; Tue, 4 Sep 2001 09:55:42 +0530 Message-ID: <005301c134f9$21d6f080$2b01a8c0@john> From: "mailcity Test User" To: Subject: Test mail Date: Tue, 4 Sep 2001 09:51:55 +0530 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0050_01C13527.3AF1DB10" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2462.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2462.0000 This is a multi-part message in MIME format. ------=_NextPart_000_0050_01C13527.3AF1DB10 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable This is a test ------=_NextPart_000_0050_01C13527.3AF1DB10 Content-Type: text/html; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable
This is a = test
------=_NextPart_000_0050_01C13527.3AF1DB10-- .....

2: Return-Path: Received: from john (john [192.168.1.43]) by traffic.com (8.9.3/8.8.7) with SMTP id KAA23203 for ; Tue, 4 Sep 2001 10:02:38 +0530 Message-ID: <006d01c134fa$1a086270$2b01a8c0@john> From: "mailcity Test User" To: Subject: Hello Date: Tue, 4 Sep 2001 09:58:51 +0530 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_006A_01C13528.33296780" X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2462.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2462.0000 This is a multi-part message in MIME format. ------=_NextPart_000_006A_01C13528.33296780 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable mailcity.com is good. But requires improvement ------=_NextPart_000_006A_01C13528.33296780 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
mailcity.com is good. But requires=20 improvement

------=_NextPart_000_006A_01C13528.33296780-- .....

3: Return-Path: Received: from britek.tv (traffic.com [120.161.101.222]) by traffic.com (8.9.3/8.8.7) with SMTP id KAA23322 for ; Tue, 4 Sep 2001 10:05:06 +0530 Date: Tue, 4 Sep 2001 10:05:06 +0530 Message-Id: <200109040435.KAA23322@traffic.com> To: support@mailcity.com From: john@traffic.com X-Mailer: Perl Mail::Sender 0.7.06 Jan Krynicky http://jenda.krynicky.cz/ MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 7bit Subject: Feedback about mailcity Service The message is---- Testing Feedback Details of the sender---- Name : john Company : Test Inc Email Id : john@traffic.com Telephone : 11223344 Fax : 44223311 .....

4: Return-Path: Received: from XAVIER.traffic.com (xAVIER [192.168.1.11]) by traffic.com (8.9.3/8.8.7) with ESMTP id KAA24189 for ; Tue, 4 Sep 2001 10:36:59 +0530 Message-Id: <5.0.0.25.0.20010904103943.00a630b8@207.230.127.5> X-Sender: xAVIER@207.230.127.5 (Unverified) X-Mailer: QUALCOMM Windows Eudora Version 5.0 Date: Tue, 04 Sep 2001 10:40:02 +0530 To: xAVIER@mailcity.com From: xAVIER Subject: test mail Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed .....

To: xAVIER@mailcity.com
5: Return-Path: Received: from xAVIER.traffic.com (xAVIER [192.168.1.11]) by traffic.com (8.9.3/8.8.7) with ESMTP id KAA24294 for ; Tue, 4 Sep 2001 10:43:35 +0530 Message-Id: <5.0.0.25.0.20010904104608.049a3440@207.230.127.5> X-Sender: xAVIER@207.230.127.5 X-Mailer: QUALCOMM Windows Eudora Version 5.0 Date: Tue, 04 Sep 2001 10:46:40 +0530 To: xAVIER@mailcity.com From: xAVIER Subject: second test Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed test mail. .....

6: Return-Path: Received: from britek.tv (traffic.com [120.161.101.222]) by traffic.com (8.9.3/8.8.7) with SMTP id RAA03157 for ; Tue, 4 Sep 2001 17:23:47 +0530 Date: Tue, 4 Sep 2001 17:23:47 +0530 From: ewr@ff.com Message-Id: <200109041153.RAA03157@traffic.com> To: support@mailcity.com X-Mailer: Perl Mail::Sender 0.7.06 Jan Krynicky http://jenda.krynicky.cz/ MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 7bit Subject: Feedback about mailcity Service The message is---- qwf Details of the sender---- Name : er Company : wer Email Id : ewr@ff.com Telephone : er Fax : ewr .....

7: Return-Path: Received: from britek.tv (traffic.com [120.161.101.222]) by traffic.com (8.9.3/8.8.7) with SMTP id RAA03201 for ; Tue, 4 Sep 2001 17:25:23 +0530 Date: Tue, 4 Sep 2001 17:25:23 +0530 From: r@p.jj Message-Id: <200109041155.RAA03201@traffic.com> To: support@mailcity.com X-Mailer: Perl Mail::Sender 0.7.06 Jan Krynicky http://jenda.krynicky.cz/ MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 7bit Subject: Feedback about mailcity Service The message is---- qwf Details of the sender---- Name : 44 Company : wer Email Id : r@p.jj Telephone : Fax : .....

8: Return-Path: Received: from britek.tv (traffic.com [120.161.101.222]) by traffic.com (8.9.3/8.8.7) with SMTP id EAA09594 for webmaster@mailcity.com; Wed, 5 Sep 2001 04:02:00 +0530 Date: Wed, 5 Sep 2001 04:02:00 +0530 Message-Id: <200109042232.EAA09594@traffic.com> To: Webmaster From: mailcity Report X-Mailer: Perl Mail::Sender 0.7.06 Jan Krynicky http://jenda.krynicky.cz/ MIME-Version: 1.0 Content-type: text/html Content-Transfer-Encoding: 7bit Subject: Cron.daily report on users in mailcity.com The following were the rows deleted today from table 'reserved':
temp_id
userid
name
email
passwd
passquestion
passanswer
spacequota
dtcreation
createflag
user_ip
999577257
jaja
Me & Phil
xAVIER@traffic.com
xAVIER
q
a
5MB
04-Sep-2001
t
192.168.1.11
999581645
anotherone
My name's xAVIER
xAVIER@traffic.com
who'stheboss
Q's
A's
5MB
04-Sep-2001
t
192.168.1.11
999603771
challenger
challenger
subin@traffic.com
willpower
what makes a successfull man
where there is a will there is a way
5MB
04-Sep-2001
t
192.168.1.7


The following are the users registered today:
name
user ID
Email
dtcreation
user ip
9: Return-Path: Received: from britek.tv (sumugha.com [192.168.1.1]) by traffic.com (8.9.3/8.8.7) with SMTP id EAA10174 for webmaster@mailcity.com; Wed, 5 Sep 2001 04:07:24 +0530 Date: Wed, 5 Sep 2001 04:07:24 +0530 Message-Id: <200109042237.EAA10174@traffic.com> To: Webmaster From: mailcity Report X-Mailer: Perl Mail::Sender 0.7.06 Jan Krynicky http://jenda.krynicky.cz/ MIME-Version: 1.0 Content-type: text/html Content-Transfer-Encoding: 7bit Subject: Cron.daily report on users in mailcity.com The following were the rows deleted today from table 'reserved':
temp_id
userid
name
email
passwd
passquestion
passanswer
spacequota
dtcreation
createflag
user_ip


The following are the users registered today:
name
user ID
Email
dtcreation
user ip

10: Return-Path: Received: from xAVIER.traffic.com (xAVIER [192.168.1.11]) by traffic.com (8.9.3/8.8.7) with ESMTP id MAA17161 for ; Wed, 5 Sep 2001 12:07:24 +0530 Message-Id: <5.0.0.25.0.20010905120952.00a63c78@207.230.127.5> X-Sender: xAVIER@120.161.101.222 X-Mailer: QUALCOMM Windows Eudora Version 5.0 Date: Wed, 05 Sep 2001 12:11:18 +0530 To: xAVIER@mailcity.com From: xAVIER Subject: test mail Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=====================_10548868==_" --=====================_10548868==_ Content-Type: text/plain; charset="us-ascii"; format=flowed This is a test mail having an attachment... i want to check how this mail is received using my IMAP Client. - xAVIER - --=====================_10548868==_ Content-Type: image/gif; name="uuc_logo_new.gif"; x-mac-type="47494666"; x-mac-creator="4A565752" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="uuc_logo_new.gif" R0lGODlhHAAcANUAAP////f39+/v9+/v7+fn59/f7t7e3v/AwM/P5szMzP+vr8PDw7+/3dS1yry8 vMKz0P+Zmba2tq+v1aysrKWlpf+AgJmZzP9wcJmZmZCQxY+PxP9mZo2NjYCAvH9/vISEhP9QUHt7 e3BwtHV1dWBgq2ZmZv8gIFpaWlBQo/8QEFNTU/8AAEpKSkBAm0JCQjMzmTMzMyAgiigoKBAQgRsb GwAAeQ0NDQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAUUAAMALAAAAAAcABwAAAb/wIFw SDREMBwOxkEgOp/DicpVGnE+IxbsNIFCI6pSJAAomwMT7cI7DMEc5ricwsKwSyx5GcFAyA0sJVAh LANxDCQzNTUoAnosHE4LMnECKIuLFnplBioORJ5mAi+YM36bZREuUSVxpJgSKysHALIVcQEunwMq EWYdmDUZtbPEt3EYKgMGLmRliostZbK0tmYeAAM0AxGtZRLBBdPF1gAIJGUuCxgjv5jo49UrxyQo ZScUSWYimOLxxgAK1JAGoMqVfYtexKEGsMXAMh8wsHNXo8PCFRsUyIKQYZE9ACMmSDEDrgaDOBdk yTLxANMwACwSEKBkRtHJOBVSrADRAFoNg3EElA3g4k4CKgk+4VEQNCCNK3hyPASL4QjmrgEwKIh6 oUGUhRjBTJWhcILIAhYGRKGI0cJhsIFVCdhI4IRDnkOJgqEwWobACTtP8KSVw+eUGQIuQrDh0MsZ qgAUXERiM8CBCxd29AzAUELFGspDHKig4eJEiBCdZYgB7WVBPiWf2QQBADs= --=====================_10548868==_-- .....

?11: Return-Path: Received: from britek.tv (sumugha.com [192.168.1.1]) by traffic.com (8.9.3/8.8.7) with SMTP id OAA20185 for webmaster@mailcity.com; Wed, 5 Sep 2001 14:48:20 +0530 Date: Wed, 5 Sep 2001 14:48:20 +0530 Message-Id: <200109050918.OAA20185@traffic.com> To: Webmaster From: mailcity Report X-Mailer: Perl Mail::Sender 0.7.06 Jan Krynicky http://jenda.krynicky.cz/ MIME-Version: 1.0 Content-type: text/html Content-Transfer-Encoding: 7bit Subject: Cron.daily report on users in mailcity.com The following were the rows deleted today from table 'reserved':
temp_id
userid
name
email
passwd
passquestion
passanswer
spacequota
dtcreation
createflag
user_ip
999680827
florentino
Florentino Ariza
xAVIER@traffic.com
flas
q'a and a's
answer's
5MB
05-Sep-2001
t
192.168.1.11
999680993
fermina
Fermina Daza
xAVIER@traffic.com
xAVIER
q
a
5MB
05-Sep-2001
t
192.168.1.11


The following are the users registered yesterday:
name
user ID
Email
dtcreation
user ip
xAVIER
jee98
xAVIER@traffic.com
04-Sep-2001
192.168.1.11
xAVIER
november
xAVIER@traffic.com
04-Sep-2001
192.168.1.11

i can send the output as html file(because it contains html file format.). Pls post yout mail id here.


Pls try to solve my problem.
I am ready to increase the ee point to 250 (if any is ready to provide soln).

Thanks in advance.
Avatar of sureshp
sureshp

ASKER

pls help
I have not done to much with this kinda stuff but as you have the mails in a string could you not strip it out by doing something like this;

@mailarray = split(/\n/,$mailstring);
for (my $i = 0;$i < @mailarray; $i++)
{
  my $line = $mailarry[$i];
  my $is_in_attachment = 0;
  if ($line =~ m/Content-Disposition: attachment/)
  {
    $is_in_attachment = 1;
  }
  if ($is_in_attachment == 1)
  {
    $i++;
    var next_line = $mailarry[$i];
    while ($next_line !~ m/s=/)  #Guessed end of attachment
    {
      $i++;
      var next_line = $mailarry[$i];
    }
    $is_in_attachment = 0;
    $i++;
  }else{
    print ("$line\n");
  }
}

Ok, this is a bit rough as I am unable to check it but the logic should be right and it may help you along, oh I guessed the end of attachment so you may need to change this once you can confirm with more test data.

Good Luck!
Avatar of sureshp

ASKER

rpimmer,

Thanks for your comment . I will go thru your code. Hope this may help to solve.

Thanks
Avatar of sureshp

ASKER

rpimmer,

still i couldn't solve this problem.
Avatar of sureshp

ASKER

no response from experts.
You have pended this question for deletion, however, the deletion did not complete.

Comment here please as to why you want to delete this rather than award points.  Until the autodelete feature and other programming changes that are in process are completed, it'll require our intervention to help you with this.  It is always recommended that you let the experts know by posting comments first, of your intention to delete a question where there has been activity.

Listening further.

Moondancer
Community Support Moderator @ Experts Exchange
Please return and update/finalize all your open questions.  If you need our help, let us know.  If the questions remain unresolved next week, we will need to take the actions to close them.

Your cooperation in managing your questions at this site is not only appreciated but necessary.

Topic Area              URL              Date              
Community Support (General)   https://www.experts-exchange.com/jsp/qShow.jsp?ta=commspt&qid=20252119  01/06/02  
JavaScript   https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20069836  02/02/01  
JavaScript   https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20076966  02/13/01  
JavaScript   https://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20227582  11/06/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=11326038  09/20/00  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20182722  09/13/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20182782  09/14/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20247168  12/18/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20247820  12/19/01  
Perl   https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20253473  01/10/02  

Moondancer
Community Support Moderator @ Experts Exchange
ASKER CERTIFIED SOLUTION
Avatar of Moondancer
Moondancer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial