I am trying to get email attachments from email using imap,
The script works for most emails, but one of the sender attachments has a problem and cannot receive the PDF attachment.
My code is here
$hostname = '{mail.testserver.com:993/tls}INBOX';
$username = 'test@testserver.nl';
$password = 's1M@F1225224';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Email Server: ' . imap_last_error());
$emails = imap_search($inbox,'FROM "info@test.com"');
count($emails);
if($emails) {
$output = '';
$io=0;
rsort($emails);
foreach($emails as $email_number){
echo $io++;
echo '<br/>';
$int='';
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1);
$headerx = imap_headerinfo($inbox,$email_number,0);
$structure = imap_fetchstructure($inbox, $email_number);
$attachments = array();
if(isset($structure->parts) && count($structure->parts))
{
for($i = 0; $i < count($structure->parts); $i++)
{
echo 'dump of mail structure <pre>';
var_dump($structure->parts[$i]);
echo '<pre>';
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters)
{
foreach($structure->parts[$i]->ifdparameters as $object)
{
echo $object->attribute;
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($inbox, $email_number, $i+1);
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
elseif($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
echo '<pre>';
var_dump($attachments);
echo '<pre>';
break;
}
var dump of the structural part $ structure-> parts [$ i]
object(stdClass)#36 (11) {
["type"]=>
int(1)
["encoding"]=>
int(0)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(11) "ALTERNATIVE"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#78 (2) {
["attribute"]=>
string(8) "boundary"
["value"]=>
string(51) "--boundary_278_245b5f5d-6ca6-49f9-adc5-9723088add21"
}
}
["parts"]=>
array(2) {
[0]=>
object(stdClass)#74 (12) {
["type"]=>
int(0)
["encoding"]=>
int(4)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(5) "PLAIN"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["lines"]=>
int(14)
["bytes"]=>
int(891)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#32 (2) {
["attribute"]=>
string(7) "charset"
["value"]=>
string(5) "utf-8"
}
}
}
[1]=>
object(stdClass)#49 (11) {
["type"]=>
int(1)
["encoding"]=>
int(0)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(7) "RELATED"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(2) {
[0]=>
object(stdClass)#38 (2) {
["attribute"]=>
string(4) "type"
["value"]=>
string(9) "text/html"
}
[1]=>
object(stdClass)#67 (2) {
["attribute"]=>
string(8) "boundary"
["value"]=>
string(51) "--boundary_279_c12a0e86-9df1-42b4-a007-c75d5904de9f"
}
}
["parts"]=>
array(2) {
[0]=>
object(stdClass)#58 (12) {
["type"]=>
int(0)
["encoding"]=>
int(4)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(4) "HTML"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["lines"]=>
int(43)
["bytes"]=>
int(3263)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#25 (2) {
["attribute"]=>
string(7) "charset"
["value"]=>
string(5) "utf-8"
}
}
}
[1]=>
object(stdClass)#69 (12) {
["type"]=>
int(5)
["encoding"]=>
int(3)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(3) "PNG"
["ifdescription"]=>
int(0)
["ifid"]=>
int(1)
["id"]=>
string(15) ""
["bytes"]=>
int(16262)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(0)
["parameters"]=>
object(stdClass)#62 (0) {
}
}
}
}
}
}
object(stdClass)#66 (11) {
["type"]=>
int(1)
["encoding"]=>
int(0)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(5) "MIXED"
["ifdescription"]=>
int(0)
["ifid"]=>
int(0)
["ifdisposition"]=>
int(0)
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#22 (2) {
["attribute"]=>
string(8) "boundary"
["value"]=>
string(51) "--boundary_281_2ef5384d-4be2-4276-b5be-0f73bcc0811d"
}
}
["parts"]=>
array(2) {
[0]=>
object(stdClass)#70 (13) {
["type"]=>
int(3)
["encoding"]=>
int(3)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(3) "PDF"
["ifdescription"]=>
int(0)
["ifid"]=>
int(1)
["id"]=>
string(6) ""
["bytes"]=>
int(277524)
["ifdisposition"]=>
int(1)
["disposition"]=>
string(10) "attachment"
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#80 (2) {
["attribute"]=>
string(4) "name"
["value"]=>
string(15) "inv18701288.PDF"
}
}
}
[1]=>
object(stdClass)#63 (13) {
["type"]=>
int(3)
["encoding"]=>
int(3)
["ifsubtype"]=>
int(1)
["subtype"]=>
string(12) "OCTET-STREAM"
["ifdescription"]=>
int(0)
["ifid"]=>
int(1)
["id"]=>
string(6) ""
["bytes"]=>
int(77098)
["ifdisposition"]=>
int(1)
["disposition"]=>
string(10) "attachment"
["ifdparameters"]=>
int(0)
["ifparameters"]=>
int(1)
["parameters"]=>
array(1) {
[0]=>
object(stdClass)#55 (2) {
["attribute"]=>
string(4) "name"
["value"]=>
string(33) "Algemene leveringsvoorwaarden.pdf"
}
}
}
}
}
var dump array of attachments
array(2) {
[0]=>
array(4) {
["is_attachment"]=>
bool(false)
["filename"]=>
string(0) ""
["name"]=>
string(0) ""
["attachment"]=>
string(0) ""
}
[1]=>
array(4) {
["is_attachment"]=>
bool(false)
["filename"]=>
string(0) ""
["name"]=>
string(0) ""
["attachment"]=>
string(0) ""
}
}
Does anyone know what happened to this email or script content, are there any solutions for receiving attachments, thanks a lot
I think the structure of attachment objects is different from other letters, but I'm not sure how to get this