Question

I have problem with parsing email message with PHP IMAP. Problem is that I have message signed with pkcs#7 signature. Mail contains some text and 2 attachments first one is smime.p7s and second one is message.htm which is html attachment I would like to parse.

To be honest I have no idea how can I access content of this file.

    $hostname = '{host}INBOX';
    $username = 'name';
    $password = 'pass';
    /* try to connect */
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
    /* grab emails */
    $emails = imap_search($inbox,'UNSEEN');
    $msg = Array();
    if($emails) {
        /* begin output var */
        $output = '';

        /* put the newest emails on top */
        rsort($emails);
        /* for every email... */
        foreach($emails as $email_number) {
            $overview = imap_fetch_overview($inbox,$email_number,0);
            $message = imap_fetchbody($inbox,$email_number,2);
            $structure = imap_fetchstructure ( $inbox,$email_number,FT_UID);
            echo "<pre>";
            var_dump($structure);
            echo "</pre>";
            break;
        }
    }

I get full structure and I can find there part:

       object(stdClass)#16 (14) {
          ["type"]=>
          int(0)
          ["encoding"]=>
          int(4)
          ["ifsubtype"]=>
          int(1)
          ["subtype"]=>
          string(4) "HTML"
          ["ifdescription"]=>
          int(0)
          ["ifid"]=>
          int(0)
          ["lines"]=>
          int(123)
          ["bytes"]=>
          int(4473)
          ["ifdisposition"]=>
          int(1)
          ["disposition"]=>
          string(10) "attachment"
          ["ifdparameters"]=>
          int(1)
          ["dparameters"]=>
          array(1) {
            [0]=>
            object(stdClass)#17 (2) {
              ["attribute"]=>
              string(8) "filename"
              ["value"]=>
              string(37) "message.htm"
            }
          }
          ["ifparameters"]=>
          int(1)
          ["parameters"]=>
          array(2) {
            [0]=>
            object(stdClass)#18 (2) {
              ["attribute"]=>
              string(4) "name"
              ["value"]=>
              string(37) "message.htm"
            }
            [1]=>
            object(stdClass)#19 (2) {
              ["attribute"]=>
              string(7) "charset"
              ["value"]=>
              string(8) "us-ascii"
            }
          }
        }

Can anyone give me a hint how can I access content of message.htm ?

Was it helpful?

Solution

Since the structure does not have any parts defined, then the message is "simple".

Try using:

$message = imap_fetchbody($inbox,$email_number,0);

This will fetch the "0th" part of the message, which should be the body.

Check out the docs here: http://www.php.net/manual/en/function.imap-fetchstructure.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top