PHP IMAP电子邮件附件无法访问某些电子邮件
作者:互联网
我正在尝试使用imap从电子邮件中获取电子邮件附件,
该脚本适用于大多数电子邮件,但一个发件人附件似乎有问题,无法检索PDF附件.
我的代码在这里
$hostname = '{mail.testserver.com:993/tls}INBOX';
$username = 'test@testserver.nl';
$password = 's1M@F1225224';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Email Server: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox,'FROM "info@test.com"');
count($emails);
/* if emails are returned, cycle through each... */
if($emails) {
/* begin output var */
$output = '';
$io=0;
/* put the newest emails on top */
rsort($emails);
/* for every email... */
foreach($emails as $email_number){
echo $io++;
echo '<br/>';
$int='';
/* get information specific to this email */
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = imap_fetchbody($inbox,$email_number,1);
$headerx = imap_headerinfo($inbox,$email_number,0);
// get first mails header
$structure = imap_fetchstructure($inbox, $email_number);
//var_dump( $structure );
$attachments = array();
/* if any attachments found... */
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)
{
//dparameters
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);
/* 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']);
}
}
}
}
echo '<pre>';
var_dump($attachments);
echo '<pre>';
break;
}
var dump of structure 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 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) ""
}
}
有人知道这个电子邮件内容或脚本有什么问题,是否有任何获得附件的解决方案,非常感谢
我认为附件对象结构与其他邮件不同,但我不知道如何获取它
解决方法:
检查您尝试附加的文件的pdf附件的大小,它可能超过php.ini允许的限制
标签:php,imap,email-attachments 来源: https://codeday.me/bug/20190710/1425899.html