Ниже кода, который я использовал для извлечения вложений из Gmail, но некоторые из вложений не могут быть извлечены, потому что я видел, что иногда мы получаем вложения с разными индексами.
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' .
imap_last_error());
foreach ($vendor_list as $data) {
$search = "UNSEEN FROM '".$data['vendor_email']."' ";
$emails = imap_search($inbox, $search);
if($emails) {
$available = 1;
$count = 1;
// put the newest emails on top
rsort($emails);
$max_emails = 1;
foreach($emails as $email_number)
{
$filename = '';
// get information specific to this email
$overview = imap_fetch_overview($inbox,$email_number,0); //$email[0]; for latest email
// get mail message, not actually used here.
// for details on the third parameter.
$message = imap_fetchbody($inbox,$email_number,2);
// get mail structure
$structure = imap_fetchstructure($inbox, $email_number);
$attachments = array();
// if any attachments found
if(isset($structure->parts) && count($structure->parts))
{
$has_attachment = 'no';
for($i = 0; $i < count($structure->parts); $i++)
{
$attachments[$i] = array(
'is_attachment' => false,
'filename' => '',
'name' => '',
'attachment' => ''
);
if($structure->parts[$i]->ifdparameters == '1')
{
foreach($structure->parts[$i]->dparameters as $object)
{
if(strtolower($object->attribute) == 'filename')
{
$allowedExts = array();
##$allowedExts = array("csv","pdf","xlsx","xls","txt");
$check = strtolower($data['file_type']);
$allowedExts=explode(",",$check);
$temp = explode(".",$object->value);
$ext = strtolower(end($temp));
if(in_array($ext, $allowedExts))
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['filename'] = $object->value;
}
}
}
}
if($structure->parts[$i]->ifparameters == '1')
{
foreach($structure->parts[$i]->parameters as $object)
{
if(strtolower($object->attribute) == 'name')
{
//$allowedExts = array("xls", "xlsx", "csv", "pdf");
$allowedExts = array();
$check = strtolower($data['file_type']);
$allowedExts=explode(",",$check);
$temp = explode(".",$object->value);
$ext = strtolower(end($temp));
if(in_array($ext, $allowedExts))
{
$attachments[$i]['is_attachment'] = true;
$attachments[$i]['name'] = $object->value;
}
}
}
}
if($attachments[$i]['is_attachment'])
{
$has_attachment = 'yes';
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
// 3 = BASE64 encoding
if($structure->parts[$i]->encoding == 3)
{
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
}
// 4 = QUOTED-PRINTABLE encoding
elseif($structure->parts[$i]->encoding == 4)
{
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
}
}
}
}
if(@$has_attachment == 'yes')
{
// iterate through each attachment and save it
foreach($attachments as $attachment)
{
if($attachment['is_attachment'] == 1)
{
$filename = $attachment['name'];
if(empty($filename)) $filename = $attachment['filename'];
if(empty($filename)) $filename = time() . ".dat";
##code to upload file to my FTP
}
}
}
}
}
}
if($available == 0){
$msg = "error found....";
}else if($available == 1){
$msg = "Attachment Fetch Successfully....";
}
// close the connection
imap_close($inbox);
echo $msg;
Приведенный выше код может обрабатывать некоторые выборки вложений, но вложение, которое мы видим в массиве ниже, не может извлечь из кода. Пожалуйста, предложите, что должно быть сделано.
stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => MIXED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => BOUNDARY
[value] => --boundary_218_fb91cf9e-3676-4726-ba4e-8f7db87ea2f1
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => ALTERNATIVE
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => BOUNDARY
[value] => --boundary_216_75ad7b71-fb81-4d07-bf12-61b7ccffb173
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 0
[encoding] => 4
[ifsubtype] => 1
[subtype] => PLAIN
[ifdescription] => 0
[ifid] => 0
[lines] => 171
[bytes] => 8526
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => CHARSET
[value] => us-ascii
)
)
)
[1] => stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => RELATED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => BOUNDARY
[value] => --boundary_217_b946fdf6-32e3-405e-93fe-158278a4a3a2
)
[1] => stdClass Object
(
[attribute] => TYPE
[value] => text/html
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 0
[encoding] => 3
[ifsubtype] => 1
[subtype] => HTML
[ifdescription] => 0
[ifid] => 0
[lines] => 212
[bytes] => 10596
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => CHARSET
[value] => utf-8
)
)
)
[1] => stdClass Object
(
[type] => 5
[encoding] => 3
[ifsubtype] => 1
[subtype] => JPEG
[ifdescription] => 0
[ifid] => 1
[id] =>
[bytes] => 3978
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 0
[parameters] => stdClass Object
(
)
)
)
)
)
)
[1] => stdClass Object
(
[type] => 1
[encoding] => 0
[ifsubtype] => 1
[subtype] => MIXED
[ifdescription] => 0
[ifid] => 0
[ifdisposition] => 0
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => BOUNDARY
[value] => --boundary_219_d6174fa9-5360-4539-af82-3a9a3ded60e7
)
)
[parts] => Array
(
[0] => stdClass Object
(
[type] => 3
[encoding] => 3
[ifsubtype] => 1
[subtype] => OCTET-STREAM
[ifdescription] => 0
[ifid] => 0
[bytes] => 191122
[ifdisposition] => 1
[disposition] => ATTACHMENT
[ifdparameters] => 0
[ifparameters] => 1
[parameters] => Array
(
[0] => stdClass Object
(
[attribute] => NAME
[value] => 20INV-00568 - Wildlife Invoice.pdf
)
)
)
)
)
)
)