PHP Classes

How to parse this email from gmail

Recommend this page to a friend!

      PHP MIME Email Message Parser  >  All threads  >  How to parse this email from gmail  >  (Un) Subscribe thread alerts  
Subject:How to parse this email from gmail
Summary:Can not parse with mime email parser class
Messages:5
Author:Juan Alvarez
Date:2019-10-01 17:55:32
 

  1. How to parse this email from gmail   Reply   Report abuse  
Picture of Juan Alvarez Juan Alvarez - 2019-10-01 17:55:32
Hi, i have been using this parser without problems, just now i have this email message to parse, but cannot find from, to, subject, body, etc.

This email contains 5 attachments, i need to read the "Body" of each atachment.

And also, how to find for example the contents of: ExtractedAddresses ?

array (
0 =>
array (
'Headers' =>
array (
'return-path:' =>
array (
0 => '<cfe.xxx@gmail.com>',
1 => '<cfe.xxx@gmail.com>',
),
'delivered-to:' => 'myreceivedmail@domain.net',
'received:' =>
array (
0 => 'from server.somedomain.net by server.somedomain.net with LMTP id KB5QGeUNW12ELAAAsUSiDQ (envelope-from <cfe.xxx@gmail.com>) for <myreceivedmail@domain.net>; Mon, 19 Aug 2019 17:00:21 -0400',
1 => 'from mail-io1-f50.google.com ([209.85.166.50]:33050) by server.somedomain.net with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92) (envelope-from <cfe.xxx@gmail.com>) id 1hzolJ-0002zu-BT for myreceivedmail@domain.net; Mon, 19 Aug 2019 17:00:21 -0400',
2 => 'by mail-io1-f50.google.com with SMTP id z3so7446591iog.0 for <myreceivedmail@domain.net>; Mon, 19 Aug 2019 14:00:21 -0700 (PDT)',
),
'envelope-to:' => 'myreceivedmail@domain.net',
'delivery-date:' => 'Mon, 19 Aug 2019 17:00:21 -0400',
'dkim-signature:' => 'v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=2qAzbLlxziEwrUJmCMo65WbWXzyJ6AXCn84Cdv1n2f8=; b=MwPm9k1dmoBy3aqF5XTqjF3YKrM5BCnglqMbKP55egUUJRb9No0YDt+AQoC96kAkQX 02xH1FvbvWydRd+fDlfZLTGMItG5SERoGrmNdOmqDI9lK86LEKzXer2M338ZsgybUHGj CIJDoCJesPKNeRc9NsxPZ9L7DaplycNYj6D0UfE+V+J7StNNGeg4Qh3MNJOR6aubV49Q eLXWvYokVLWpuXhEnQt4jG86wavbaSEejwCU4tqoeouNY5My2gwqyqNaJ3yU0ujx0f2u fUdPB17EkKgjkrWGrM7vcSW2AU98cTDvgOChSnFz5hc/2X/Uv0O1u5/qQCVymyRUmp2Z d4yg==',
'x-google-dkim-signature:' => 'v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=2qAzbLlxziEwrUJmCMo65WbWXzyJ6AXCn84Cdv1n2f8=; b=ceC29ij/BJkcp+zSbnjuPyUYKB+twq8mKJ9GBevnP/yqe6Q6783h0WMC6PzTPKjlEC TNKMj7aajshtv4z/xOaHbX704itKLt4vtCVSRrrTIFsRonqCx0On1+NkVBSS9dqX6LAH NdarzODct9FnQ7veMgRLB2prS2o0XzmAMT0XHS5U915Wo255DMQb+oVU2hAyUl8O7WrL VJ0xk4/SztlAAZG0dawjyc0iA5hB5Y/HkNG7NmF8UfaYQ8JGHHfNJJhcMfYK0oJZxgRL hzccniSq12y0BqkgBdudNFe/thwS0U9Ff6GJraSu6QMRQUaRylfam9XmxeVVAT730V7D ghDA==',
'x-gm-message-state:' => 'APjAAAXYhUAFuKwdFiIMGW/kQwc1x2c2DhVfzh7sxwXum5NdRE8/c5Cz dCjCNVXox9CzjVqgaUocCS9n6C7XBegfWdyb+Js60g==',
'x-google-smtp-source:' => 'APXvYqzZLg1FUNlmCsbDyxv2DdluNizDCy/5wfd4bbUjgaj9xXqFnbdbZA1n/4dlIIG98HjGObRPBIyNhZ3GFK3/ovk=',
'x-received:' => 'by 2002:a6b:4a11:: with SMTP id w17mr25993124iob.21.1566248420652; Mon, 19 Aug 2019 14:00:20 -0700 (PDT)',
'mime-version:' => '1.0',
'from:' => 'FULANITO <cfe.xxx@gmail.com>',
'date:' => 'Mon, 19 Aug 2019 18:00:08 -0300',
'message-id:' => '<CAOTMY3C-Vio4j66OGDe1fSUr=rjV-=WZ5GyN=LxVb3YBcjZZ+g@mail.gmail.com>',
'subject:' => '',
'to:' => 'myreceivedmail@domain.net',
'content-type:' => 'multipart/mixed; boundary="000000000000be73b305907ea02b"',
),
'Parts' =>
array (
0 =>
array (
'Headers' =>
array (
'content-type:' => 'multipart/alternative; boundary="000000000000be73b105907ea029"',
),
'Parts' =>
array (
0 =>
array (
'Headers' =>
array (
'content-type:' => 'text/plain; charset="UTF-8"',
),
'Parts' =>
array (
),
'Position' => 2888,
'Body' => '

',
'BodyPart' => 1,
'BodyLength' => 4,
),
1 =>
array (
'Headers' =>
array (
'content-type:' => 'text/html; charset="UTF-8"',
),
'Parts' =>
array (
),
'Position' => 2969,
'Body' => '<div dir="ltr"><br></div>

',
'BodyPart' => 2,
'BodyLength' => 29,
),
),
'Position' => 2776,
),
1 =>
array (
'Headers' =>
array (
'content-type:' => 'text/xml; charset="UTF-8"; name="Sob_120111XX00XX_20190709_195984.xml"',
'content-disposition:' => 'attachment; filename="Sob_120111XX00XX_20190709_195984.xml"',
'content-transfer-encoding:' => 'base64',
'content-id:' => '<f_jzivuw160>',
'x-attachment-id:' => 'f_jzivuw160',
),
'Parts' =>
array (
),
'Position' => 3108,
'FileName' => 'Sob_120111XX00XX_20190709_195984.xml',
'FileDisposition' => 'attachment',
'Body' => '<DGICFE:EnvioCFE_entreEmpresas version="1.0">...</DGICFE:EnvioCFE_entreEmpresas>',
'BodyPart' => 3,
'BodyLength' => 6874,
),
2 =>
array (
'Headers' =>
array (
'content-type:' => 'text/xml; charset="UTF-8"; name="Sob_120111XX00XX_20190709_195974.xml"',
'content-disposition:' => 'attachment; filename="Sob_120111XX00XX_20190709_195974.xml"',
'content-transfer-encoding:' => 'base64',
'content-id:' => '<f_jzivuw5e4>',
'x-attachment-id:' => 'f_jzivuw5e4',
),
'Parts' =>
array (
),
'Position' => 12815,
'FileName' => 'Sob_120111XX00XX_20190709_195974.xml',
'FileDisposition' => 'attachment',
'Body' => '<DGICFE:EnvioCFE_entreEmpresas version="1.0">...</DGICFE:EnvioCFE_entreEmpresas>',
'BodyPart' => 4,
'BodyLength' => 6869,
),
3 =>
array (
'Headers' =>
array (
'content-type:' => 'text/xml; charset="UTF-8"; name="Sob_120111XX00XX_20190812_199116.xml"',
'content-disposition:' => 'attachment; filename="Sob_120111XX00XX_20190812_199116.xml"',
'content-transfer-encoding:' => 'base64',
'content-id:' => '<f_jzivuw593>',
'x-attachment-id:' => 'f_jzivuw593',
),
'Parts' =>
array (
),
'Position' => 22514,
'FileName' => 'Sob_120111XX00XX_20190812_199116.xml',
'FileDisposition' => 'attachment',
'Body' => '<DGICFE:EnvioCFE_entreEmpresas version="1.0">...</DGICFE:EnvioCFE_entreEmpresas>',
'BodyPart' => 5,
'BodyLength' => 6461,
),
4 =>
array (
'Headers' =>
array (
'content-type:' => 'text/xml; charset="UTF-8"; name="Sob_120111XX00XX_20190715_196599.xml"',
'content-disposition:' => 'attachment; filename="Sob_120111XX00XX_20190715_196599.xml"',
'content-transfer-encoding:' => 'base64',
'content-id:' => '<f_jzivuw542>',
'x-attachment-id:' => 'f_jzivuw542',
),
'Parts' =>
array (
),
'Position' => 31655,
'FileName' => 'Sob_120111XX00XX_20190715_196599.xml',
'FileDisposition' => 'attachment',
'Body' => '<DGICFE:EnvioCFE_entreEmpresas version="1.0">...</DGICFE:EnvioCFE_entreEmpresas>',
'BodyPart' => 6,
'BodyLength' => 7905,
),
5 =>
array (
'Headers' =>
array (
'content-type:' => 'text/xml; charset="UTF-8"; name="Sob_120111XX00XX_20190709_196002.xml"',
'content-disposition:' => 'attachment; filename="Sob_120111XX00XX_20190709_196002.xml"',
'content-transfer-encoding:' => 'base64',
'content-id:' => '<f_jzivuw4x1>',
'x-attachment-id:' => 'f_jzivuw4x1',
),
'Parts' =>
array (
),
'Position' => 42770,
'FileName' => 'Sob_120111XX00XX_20190709_196002.xml',
'FileDisposition' => 'attachment',
'Body' => '<DGICFE:EnvioCFE_entreEmpresas version="1.0">...</DGICFE:EnvioCFE_entreEmpresas>',
'BodyPart' => 7,
'BodyLength' => 7320,
),
),
'Position' => 0,
'ExtractedAddresses' =>
array (
'return-path:' =>
array (
0 =>
array (
'address' => 'cfe.xxx@gmail.com',
),
1 =>
array (
'address' => 'cfe.xxx@gmail.com',
),
),
'from:' =>
array (
0 =>
array (
'address' => 'cfe.xxx@gmail.com',
'name' => 'FULANITO',
),
),
'to:' =>
array (
0 =>
array (
'address' => 'myreceivedmail@domain.net',
),
),
),
),
)

  2. Re: How to parse this email from gmail   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2019-10-02 03:59:50 - In reply to message 1 from Juan Alvarez
The information you need is all there.

The result array contains 1 message at position 0 .

Let's say you assigned the variable $message to this array. The message is at $message[0].

The Headers entry has all details that are in the message $headers:

subject -> $message[0]['Headers']['subject:']
from -> $message[0]['Headers']['from:']
to -> $message[0]['Headers']['to:']

The message body is in the Parts entry. Each part may contain one or more sub-parts, so you need to traverse the the Parts entry accessing $message[0]['Parts'] array and see if it contains more parts.

Each part is like a sub-message, so it is all nested.

You probably need to use recursive functions to process the parts you want.

ExtractedAddresses is a special entry that extracts the values of all headers that contain email addresses.

  3. Re: How to parse this email from gmail   Reply   Report abuse  
Picture of Juan Alvarez Juan Alvarez - 2019-10-02 14:28:05 - In reply to message 2 from Manuel Lemos
The contents in first thread is in a file called as $rutaArchivo:

Here is my code:

$myMailReceivedasString = file_get_contents($rutaArchivo, FILE_USE_INCLUDE_PATH);
$mime=new mime_parser_class;

$mime->mbox = 0;

$mime->decode_bodies = 1;

$mime->ignore_syntax_errors = 1;

$mime->track_lines = 1;

$mime->use_part_file_names = 0;

$mime->custom_mime_types = array(
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'=>array(
'Type' => 'ms-word',
'Description' => 'Word processing document in Microsoft Office OpenXML format'
)
);

$parameters=array(
'Data'=>$myMailReceivedasString,
);

if(defined('__TEST'))
{
if(IsSet($__test_options['parameters']))
$parameters=$__test_options['parameters'];
if(IsSet($__test_options['mbox']))
$mime->mbox=$__test_options['mbox'];
if(IsSet($__test_options['decode_bodies']))
$mime->decode_bodies=$__test_options['decode_bodies'];
if(IsSet($__test_options['use_part_file_names']))
$mime->use_part_file_names=$__test_options['use_part_file_names'];
}

if(!$mime->Decode($parameters, $decoded))
{
echo 'MIME message decoding error: '.$mime->error.' at position '.$mime->error_position;
if($mime->track_lines
&& $mime->GetPositionLine($mime->error_position, $line, $column))
echo ' line '.$line.' column '.$column;
echo "\n";
}
else
{
echo 'MIME message decoding successful.'."\n";
echo (count($decoded)==1 ? '1 message was found.' : count($decoded).' messages were found.'),"\n";
for($message = 0; $message < count($decoded); $message++)
{
echo 'Message ',($message+1),':',"\n";
var_dump($decoded[$message]);
if($mime->decode_bodies)
{
if($mime->Analyze($decoded[$message], $results))
var_dump($results);
else
echo 'MIME message analyse error: '.$mime->error."\n";
}
}
for($warning = 0, Reset($mime->warnings); $warning < count($mime->warnings); Next($mime->warnings), $warning++)
{
$w = Key($mime->warnings);
echo 'Warning: ', $mime->warnings[$w], ' at position ', $w;
if($mime->track_lines
&& $mime->GetPositionLine($w, $line, $column))
echo ' line '.$line.' column '.$column;
echo "\n";
}

$subject = $decoded[0]['Headers']['subject:'];
echo $subject;


}

The subject is not in $subject, the error is:
Notice: Undefined index: subject: in ....

This content is empty array: $decoded[0]['Headers']

All the content is in $decoded[0]['Body'] as string (not as array).

  4. Re: How to parse this email from gmail   Reply   Report abuse  
Picture of Juan Alvarez Juan Alvarez - 2019-10-02 18:19:46 - In reply to message 3 from Juan Alvarez
Sorry, i did this for save the attachments from email to a file:

ob_start();
var_export($decoded);
$decoded_debug = b_get_contents();
ob_end_clean();
$fichero=fopen("_myFile_" . $unaParte['FileName']),'w');
fwrite($fichero, $decoded_debug);
fclose($fichero);

but then i read the contents from file, but did:

$myMailReceivedasString = file_get_contents($rutaArchivo, FILE_USE_INCLUDE_PATH);

$mime=new mime_parser_class;
....

and the line who fix everything is:

$BodyArr = $decoded[0]['Body'];
eval('$myDecoded = ' . $BodyArr.';');

then use it:

//---------------------- GET EMAIL HEADER INFO -----------------------//

//get the name and email of the sender
$fromName = $myDecoded[0]['ExtractedAddresses']['from:'][0]['name'];
$fromEmail = $myDecoded[0]['ExtractedAddresses']['from:'][0]['address'];

//get the name and email of the recipient
$toEmail = $myDecoded[0]['ExtractedAddresses']['to:'][0]['address'];
$toName = $myDecoded[0]['ExtractedAddresses']['to:'][0]['name'];

//get the subject
$subject = $myDecoded[0]['Headers']['subject:'];

$removeChars = array('<','>');

//get the message id
$messageID = str_replace($removeChars,'',$myDecoded[0]['Headers']['message-id:']);

//get the reply id
$replyToID = str_replace($removeChars,'',$myDecoded[0]['Headers']['in-reply-to:']);


//---------------------- FIND THE BODY -----------------------//

//get the message body
if(substr($myDecoded[0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($myDecoded[0]['Body'])){

$body = $myDecoded[0]['Body'];

} elseif(substr($myDecoded[0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($myDecoded[0]['Parts'][0]['Body'])) {

$body = $myDecoded[0]['Parts'][0]['Body'];

} elseif(substr($myDecoded[0]['Parts'][0]['Parts'][0]['Headers']['content-type:'],0,strlen('text/plain')) == 'text/plain' && isset($myDecoded[0]['Parts'][0]['Parts'][0]['Body'])) {

$body = $myDecoded[0]['Parts'][0]['Parts'][0]['Body'];

}

//print out our data
echo "

Message ID: $messageID

Reply ID: $replyToID

Subject: $subject

To: $toName $toEmail

From: $fromName $fromEmail

Body: $body

";

//show all the decoded email info
//print_r($decoded);

//------------------------ ATTACHMENTS ------------------------------------//

//loop through email parts
foreach($myDecoded[0]['Parts'] as $part){

//check for attachments
if($part['FileDisposition'] == 'attachment'){

//format file name (change spaces to underscore then remove anything that isn't a letter, number or underscore)
$filename = preg_replace('/[^0-9,a-z,\.,_]*/i','',str_replace(' ','_', $part['FileName']));

//write the data to the file
//$fp = fopen('save_dir/' . $filename, 'w');
//$written = fwrite($fp,$part['Body']);
//fclose($fp);
echo '------- INICIO ---------------';
echo "<br>";
echo "<br>";
echo '<pre>' . htmlspecialchars($part['Body']) . '</pre>';
echo "<br>";
echo '------- FIN ---------------';
echo "<br>";
echo "<br>";
echo $part['FileName'];
//add file to attachments array
$attachments[] = $part['FileName'];

}

}

//print out the attachments for debug
print_r($attachments);

  5. Re: How to parse this email from gmail   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2019-10-02 19:24:02 - In reply to message 3 from Juan Alvarez
So it is working well for you now?