I’m trying to build an email message parser for our site. What I’m eventually going to do is iterate through the messages that have attachments and save the attachment if the message comes from a particular email address.
This is just the initial test, however, I am running into problems, see comments below.
<?php
echo "Loading..."."<br />\
";
$mailuser="help@mysite.com";
echo "User=$mailuser"."<br />\
";;
$mailpass="mypassword";
echo "Pass=$mailpass"."<br />\
";
// had to use this because we have SSL on site and regular port 110 didn't work
$mailhost="{localhost:995/pop3/ssl/novalidate-cert}";
echo "Host=$mailhost"."<br />\
";
$mailbox=imap_open($mailhost,$mailuser,$mailpass) or die("<br />\
FAILLED! ".imap_last_error());
$check = imap_check($mailbox);
// last message parsed will be stored in the file msgcounter.dat
$firstmsg = file_get_contents('msgcounter.dat') + 1;
$lastmsg = $firstmsg+$check->Recent; // should be == last msg index + count of latest messages
echo 'First:'.$firstmsg.' - Last:'.$lastmsg."<br>";
$result = imap_fetch_overview($mailbox,"$firstmsg:$lastmsg");
print_r($result);
foreach ($result as $overview) {
echo "#{$overview->msgno} ({$overview->date}) - From: {$overview->from}
{$overview->subject}\
";
}
// the following approach didn't work either, Kept getting warnings about
// Bad message number
//
// Some messages in the sequence HAVE been deleted.
/*
for ($index = $firstmsg-1; $index <= ($lastmsg); $index++ ) {
if (strlen(trim(imap_fetchheader($mailbox, $index))) > 0) {
echo 'in message index loop:'.$index;
}
}
*/
imap_close($mailbox);
echo "completed.". "<br />\
";;
?>
Here is the solution:
<?php
$mailpass="mypassword";
$mailhost="{localhost:995/pop3/ssl/novalidate-cert}";
$mailuser="help@mysite.com";
$mailbox=imap_open($mailhost,$mailuser,$mailpass) or die("<br />\
FAILLED! ".imap_last_error());
// The IMAP.xml file contains the email address and user_id of the users that we accept
// their files via email
$xml = simplexml_load_string(file_get_contents('IMAP.xml'));
$result = $xml->xpath('item');
while(list( , $node) = each($result)) {
$email = $node->LI_email;
$user_id = $node->LI_user_id;
$search = "UNSEEN FROM \\"$email\\"";
$result2 = imap_search($mailbox, $search);
if($result2) {
$index = $result2[0];
$structure = imap_fetchstructure($mailbox, $index);
$attachments = array();
if(isset($structure->parts) && count($structure->parts)) {
for($i = 0; $i < count($structure->parts); $i++) {
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => '');
if($structure->parts[$i]->ifdparameters) {
foreach($structure->parts[$i]->dparameters as $object) {
if(strtolower($object->attribute) == 'filename') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
if($structure->parts[$i]->ifparameters) {
foreach($structure->parts[$i]->parameters as $object) {
if(strtolower($object->attribute) == 'name') {
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
if($attachments[$i]['is_attachment']) {
$attachments[$i]['attachment'] = imap_fetchbody($mailbox, $index, $i+1, FT_PEEK);
if($structure->parts[$i]->encoding == 3) { // 3 = BASE64
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
} // if($attachments[$i]['is_attachment'])
} // for($i = 0; $i < count($structure->parts); $i++)
} // if(isset($structure->parts) && count($structure->parts))
for($i = 0; $i < count($attachments); $i++) {
if (strlen(trim($attachments[$i]['filename'])) > 0) {
$path_parts = pathinfo($attachments[$i]['filename']);
if(strtolower($path_parts['extension']) == 'zip') {
// I am going to do something different with ziped files
} else {
$filename = 'file_uploads/'.$user_id.'_'.$path_parts['filename'].'_'.date('m_d_Y').'.'.$path_parts['extension'];
$fp = fopen($filename, "x");
fwrite($fp, $attachments[$i]['attachment']);
fclose($fp);
} // if(strtolower($path_parts['extension']) == 'zip')
} // if (strlen(trim($attachments['name'])) > 0
} // for($i = 0; $i < count($attachments); $i++)
} // if($result2)
} // while(list( , $node) = each($result))
imap_close($mailbox);
?>