Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:22:37+00:00 2026-06-07T04:22:37+00:00

I can connect to the IMAP mail server easy enough: use Mail::IMAPClient; use MIME::Base64;

  • 0

I can connect to the IMAP mail server easy enough:

 use Mail::IMAPClient;
 use MIME::Base64;
 use MIME::Parser;

  my $imap = Mail::IMAPClient->new(
    Server   => '192.168.2.2',
    User     => 'xxxxxx',
    Password => 'yyyyyy',
    Ssl      => 1,
    Uid      => 1,
  );

  my $folders = $imap->folders
    or die "List folders error: ", $imap->LastError, "\n";
  print "Folders: @$folders\n";

$sfolder="INBOX.2012";

$imap->select( $sfolder )
    or die "Select '$Opt{sfolder}' error: ", $imap->LastError, "\n";

my @msgs = $imap->messages or die "Could not messages: $@\n";

However, the text and html I want is not easily parsed due to codes like this:

Content-Transfer-Encoding:base64
Content-Type:text/html; charset=utf-8
Content-Transfer-Encoding:base64
Content-Type:text/html; charset=utf-8
Content-Transfer-Encoding:
Content-Type:multipart/mixed; boundary="----------=_4F0F4830.7079357A"
Multipart
Content-Transfer-Encoding:
Content-Type:multipart/mixed;   boundary="----=_Part_4487195_1184536749.1326753403034"
Multipart
Content-Transfer-Encoding:
Content-Type:multipart/alternative; boundary=--boundary_164442_d184e417-739f-
46d6-824a-6ea1846e79de
Multipart
Content-Transfer-Encoding:
Content-Type:multipart/mixed;   boundary="----=_Part_3882878_23916831.1326509484032"
Multipart
Content-Transfer-Encoding:

I tried this but it only works on a tiny number of different encodings.

if  ($imap->get_header($msg,"Content-Transfer-Encoding")=~ /base64/i) {
print "\nMatch base64";
        if    ($imap->get_header($msg,"Content-Type")=~m/text/i )   {
         push(@mail,decode_base64($imap->body_string($msg)));
        }
        elsif ($imap->get_header($msg,"Content-Type")=~m/image/i )
        { print "\nImage detected"; }
        elsif ($imap->get_header($msg,"Content-Type")=~m/application/i )
        { print "\nApplication detected"; }

There are 7bit and 8bit variants and other encoding methods that contain the html or text I want for later use. I successfully use decode_base64() to decode base64. The worse ones to decode are the ones that contain multi-part codes. I feel like I am re-inventing the wheel and there must be a library or module that can do all the heavy lifting for me.
Other content types such as .jpg,.gif, and .pdf should simply be ignored. The multi-part emails contain at least 1 part that I an interested but many that are useless to me.

After further research this structure has some of the information I need but don’t know how to get it out efficiently is another matter.

Dumping:$VAR1 = bless( {
                 'bodyparms' => {
                                  'boundary' =>     '----=_NextPart_002_BC64_7D688C1F.A2FF9BE0'
                                },
                 'bodyextra' => undef,
                 '_top' => 1,
             'bodydisp' => 'NIL',
             '_id' => 'HEAD',
             'bodysubtype' => 'mixed',
             'PartsIndex' => {
                               '1.3' => bless( {
                                                 'bodyparms' => 'NIL',
                                                 'bodyid' => '<d9e26cc0-019c-4ac0-9b1e-9c9ac8424f52>',
                                                 'bodyextra' => 'NIL',
                                                 'bodydisp' => 'NIL',
                                                 '_id' => '1.3',
                                                 'bodysubtype' => 'jpeg',
                                                 '_prefix' => '1.3',
                                                 'bodysize' => '4808',
                                                 'bodytype' => 'image',
                                                 'bodyMD5' => 'NIL',
                                                 'bodylang' => 'NIL',
                                                 'bodydesc' => 'NIL',
                                                 'bodyenc' => 'base64'
                                               }, 'Mail::IMAPClient::BodyStructure' ),
                               '1.1' => bless( {
                                                 'bodyparms' => {
                                                                  'boundary' => '----=_NextPart_000_36AE_880DDD08.0A776E35'
                                                                },
                                                 'bodyextra' => undef,
                                                 'bodydisp' => 'NIL',
                                                 '_id' => '1.1',
                                                 'bodysubtype' => 'alternative',
                                                 '_prefix' => '1.1',
                                                 'bodytype' => 'MULTIPART',
                                                 'bodystructure' => [
                                                                      bless( {
                                                                               'bodyparms' => {
                                                                                                'charset' => 'utf-8'
                                                                                              },
                                                                               'bodyextra' => 'NIL',
                                                                               'bodyid' => 'NIL',
                                                                               'bodydisp' => 'NIL',
                                                                               '_id' => '1.1.1',
                                                                               'bodysubtype' => 'PLAIN',
                                                                               '_prefix' => '1.1.1',
                                                                               'bodysize' => '1971',
                                                                               'bodytype' => 'TEXT',
                                                                               'bodyMD5' => 'NIL',
                                                                               'textlines' => '74',
                                                                               'bodylang' => 'NIL',
                                                                               'bodydesc' => 'NIL',
                                                                               'bodyenc' => 'quoted-printable'
                                                                             }, 'Mail::IMAPClient::BodyStructure' ),
                                                                      bless( {
                                                                               'bodyparms' => {
                                                                                                'charset' => 'utf-8'
                                                                                              },
                                                                               'bodyextra' => 'NIL',
                                                                               'bodyid' => 'NIL',
                                                                               'bodydisp' => 'NIL',
                                                                               '_id' => '1.1.2',
                                                                               'bodysubtype' => 'HTML',
                                                                               '_prefix' => '1.1.2',
                                                                               'bodysize' => '23364',
                                                                               'bodytype' => 'TEXT',
                                                                               'bodyMD5' => 'NIL',
                                                                               'textlines' => '331',
                                                                               'bodylang' => 'NIL',
                                                                               'bodydesc' => 'NIL',
                                                                               'bodyenc' => 'quoted-printable'
                                                                             }, 'Mail::IMAPClient::BodyStructure' )
                                                                    ],
                                                 'bodyloc' => 'NIL',
                                                 'bodylang' => 'NIL'
                                               }, 'Mail::IMAPClient::BodyStructure' ),
                               '1' => bless( {
                                               'bodyparms' => {
                                                                'boundary' => '----=_NextPart_001_EA96_2BF8DEDE.32622D51'
                                                              },
                                               'bodyextra' => undef,
                                               'bodydisp' => 'NIL',
                                               '_id' => 1,
                                               'bodysubtype' => 'related',
                                               '_prefix' => 1,
                                               'bodytype' => 'MULTIPART',
                                               'bodystructure' => [
                                                                    $VAR1->{'PartsIndex'}{'1.1'},
                                                                    bless( {
                                                                             'bodyparms' => 'NIL',
                                                                             'bodyid' => '<5dff39db-e81c-4410-be75-8662564fd328>',
                                                                             'bodyextra' => 'NIL',
                                                                             'bodydisp' => 'NIL',
                                                                             '_id' => '1.2',
                                                                             'bodysubtype' => 'jpeg',
                                                                             '_prefix' => '1.2',
                                                                             'bodysize' => '14406',
                                                                             'bodytype' => 'image',
                                                                             'bodyMD5' => 'NIL',
                                                                             'bodylang' => 'NIL',
                                                                             'bodydesc' => 'NIL',
                                                                             'bodyenc' => 'base64'
                                                                           }, 'Mail::IMAPClient::BodyStructure' ),
                                                                    $VAR1->{'PartsIndex'}{'1.3'},
                                                                    bless( {
                                                                             'bodyparms' => 'NIL',
                                                                             'bodyid' => '<717f2ef4-f795-4d1c-87cc-283c9b0a59b0>',
                                                                             'bodyextra' => 'NIL',
                                                                             'bodydisp' => 'NIL',
                                                                             '_id' => '1.4',
                                                                             'bodysubtype' => 'gif',
                                                                             '_prefix' => '1.4',
                                                                             'bodysize' => '2912',
                                                                             'bodytype' => 'image',
                                                                             'bodyMD5' => 'NIL',
                                                                             'bodylang' => 'NIL',
                                                                             'bodydesc' => 'NIL',
                                                                             'bodyenc' => 'base64'
                                                                           }, 'Mail::IMAPClient::BodyStructure' )
                                                                  ],
                                               'bodyloc' => 'NIL',
                                               'bodylang' => 'NIL'
                                             }, 'Mail::IMAPClient::BodyStructure' ),
                               '1.2' => $VAR1->{'PartsIndex'}{'1'}{'bodystructure'}[1],
                               '1.1.2' => $VAR1->{'PartsIndex'}{'1.1'}{'bodystructure'}[1],
                               '2' => bless( {
                                               'bodyparms' => {
                                                                'name' => 'BKD-7361945220.pdf'
                                                              },
                                               'bodyid' => 'NIL',
                                               'bodyextra' => 'NIL',
                                               'bodydisp' => {
                                                               'attachment' => {
                                                                                 'filename' => 'BKD-7361945220.pdf'
                                                                               }
                                                             },
                                               '_id' => 2,
                                               'bodysubtype' => 'octetstream',
                                               '_prefix' => 2,
                                               'bodysize' => '47540',
                                               'bodytype' => 'application',
                                               'bodyMD5' => 'NIL',
                                               'bodystructure' => [],
                                               'bodylang' => 'NIL',
                                               'bodydesc' => 'NIL',
                                               'bodyenc' => 'base64'
                                             }, 'Mail::IMAPClient::BodyStructure' ),
                               '1.4' => $VAR1->{'PartsIndex'}{'1'}{'bodystructure'}[3],
                               '1.1.1' => $VAR1->{'PartsIndex'}{'1.1'}{'bodystructure'}[0]
                             },
             '_prefix' => 'HEAD',
             'PartsList' => [
                              1,
                              '1.1',
                              '1.1.1',
                              '1.1.2',
                              '1.2',
                              '1.3',
                              '1.4',
                              2
                            ],
             'bodytype' => 'MULTIPART',
             'bodystructure' => [
                                  $VAR1->{'PartsIndex'}{'1'},
                                  $VAR1->{'PartsIndex'}{'2'}
                                ],
             'bodyloc' => 'NIL',
             'bodylang' => 'NIL'
           }, 'Mail::IMAPClient::BodyStructure' );

As you can see none of the values are guaranteed to be part of every part on the PartsIndex and some them are nested.

variable of interest for each PartsIndex item:
bodytype
bodysubtype
bodyenc

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:22:39+00:00Added an answer on June 7, 2026 at 4:22 am

    I tried using a couple of prebuilt modules but they had too many dependencies and was hard to work with. This solution adds no dependencies beyond the original. I also had issues with the dependencies for libMagic, see above, and I did not want anyone who uses my program to have to deal with that issue either.

    You have to call decode twice once for the main parent, and again for each child. Since this $imap->get_bodystructure($msg); contains all the information you need why add dependencies where none are needed. It took many many hours to figure out how to decode it manually, but it was worth it.

    You can add whatever decoders you want to the decode() subroutine. I only need to decode the text/html and base64 encoded versions there of. The IMAPClient functions give you a list of all parents and children so you don’t have to go making a list by yourself. The tricky part is you can have any number of parent each with any number of children, but only the children contain useful data. The parents can be ignored, since many of their values are blank,undef, or ‘NIL’ (literally). In fact a vast number of variables have the value of ‘NIL’. Even ones that the email client could have answered for the user like bodyMD5 and bodylang are USUALLY equal ‘NIL’. Due to the overwhelming use of ‘NIL’ parsing and using other fields may prove futile. Depend on your imap server and the people you recieve email from you mileage may vary.

    If you have further questions leave a comment.

     use Mail::IMAPClient;
     use MIME::Base64;
     use MIME::Parser;
    
    sub decode  {
    ($process, $imap) =@_;
    if ($process->bodytype eq "TEXT") {
       print "\n Text SubType:".$process->bodysubtype;
       if ($process->bodyenc eq "base64") {
            return decode_base64($imap->bodypart_string($msg,$process->id));
            }
       elsif (index(" -7bit- -8bit- -quoted-printable- ",lc($process->bodyenc)) !=-1  ) {
            return $imap->bodypart_string($msg,$process->id);
            }
    print "\n==========Insert new decoder here============";
    print "\n".$imap->bodypart_string($msg,$process->id);
    print "\n=================================================";
    
    }
    
    return "";
    }
    
    #insert your login code with credentials here
    
    
    $imap->select( $sfolder )
    or die "Select '$Opt{sfolder}' error: ", $imap->LastError, "\n";
    
    my @msgs = $imap->messages or die "Could not messages: $@\n";
    
    
    foreach $msg (@msgs) {
    
    
    my $raw = $imap->message_string($msg)
        or die $imap->LastError;
    
    $struct = $imap->get_bodystructure($msg);
    
    
    #MULTIPART is a container designation and does not contain anything useful by itself.
    #However it will still process all of the children that have content
    if ($struct->bodytype ne "MULTIPART") { print "\n BodyEnc:".$struct->bodyenc();}
    
    $rDecode=decode($struct,$imap);
    #do not insert blanks.
    if ($rDecode ne "" && (length($rDecode)>2)) {push(@mail,$rDecode); }
    
    
    foreach $dumpme ($struct->bodystructure()) {
    if ($dumpme->bodytype() eq "MULTIPART") {next;}
    
    $rDecode="";
    $rDecode=decode($dumpme,$imap);
    #do not insert blanks.
    if (($rDecode ne "") && (length($rDecode)>2) ) {
    push(@mail,$rDecode); }
    
    
    }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can connect to a remote X11 server to use them like
I can connect to server and get json data. it is in this format:
I have XAMPP running on my desktop and can connect to the MySQL server
I can connect to remote SQL Server database and I want to create a
In my application people can connect to a remote computer with the use of
Is there a way that I can connect to a TCP server using a
i need to connect an imap email server with below given -(BOOL) method how
I can connect to my sql server 2008 developer server using this in a
I have a server to which multiple clients can connect to. The client is
I have a mail server based on Postfix + Dovecot (IMAP) on a CentOS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.