我正在使用PHP来计算单个电子邮件中有多少个附件的值。
当我尝试这样做时:
echo count($structure->parts);
输出将显示如下:
3
我在一封电子邮件中有2个附件,因此输出返回不正确。所以当我尝试这个:
for($i = 0; $i < count($structure->parts); $i++) {
echo $i;
}
输出返回如下:
012
这是完整的代码:
<?php
require_once "Mail.php";
require_once('Mail/IMAPv2.php');
$username = 'myusername';
$password = 'mypassword';
$mailserver = '{imap.domain.com:993/imap/ssl/novalidate-cert}INBOX';
$mailbox = imap_open($mailserver, $username, $password) or die("Can't connect: " . imap_last_error());
$key = "mykey";
$email_number = openssl_decrypt(hex2bin('274'),'AES-128-CBC', $key);
$attach_id = $_GET['attid'];
/* get information specific to this email */
$overview = imap_fetch_overview($mailbox, $email_number, 0);
$message = imap_fetchbody($mailbox, $email_number, 2);
/* get mail structure */
$structure = imap_fetchstructure($mailbox, $email_number);
$attachments = array();
for($i = 0; $i < count($structure->parts); $i++) {
echo $i;
}
?>
我在一封电子邮件中有2个附件,所以返回的输出应显示2个而不是3
或012
,但仍显示3个。我试图在google上找到如何计数的答案使用imap的一封电子邮件中附件的值,但我无法这样做。
您能给我一个例子来说明如何计算一封电子邮件中有多少个附件吗?
谢谢。
编辑:这是$ structure-> 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] => 00000000000014af61058c780612 ) ) [parts] => Array ( [0] => stdClass Object ( [type] => 0
[encoding] => 0 [ifsubtype] => 1 [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 [lines] => 3
[bytes] => 42 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] =>
Array ( [0] => stdClass Object ( [attribute] => charset [value] => UTF-8 ) ) ) [1] =>
stdClass Object ( [type] => 0 [encoding] => 4 [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0
[ifid] => 0 [lines] => 4
[bytes] => 307 [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 [parameters] =>
Array ( [0] => stdClass Object ( [attribute] => charset [value] => UTF-8 ) ) ) ) ) [1] =>
stdClass Object ( [type] => 3 [encoding] => 3 [ifsubtype] => 1 [subtype] => OCTET-STREAM
[ifdescription] => 0 [ifid] => 1 [id] => [bytes] => 99376 [ifdisposition] => 1 [disposition] =>
attachment [ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] =>
filename [value] => 2019-01-23 (1).rar ) ) [ifparameters] => 1 [parameters] => Array ( [0] =>
stdClass Object ( [attribute] => name [value] => 2019-01-23 (1).rar ) ) ) [2] => stdClass Object
( [type] => 3 [encoding] => 3 [ifsubtype] => 1 [subtype] => X-ZIP-COMPRESSED [ifdescription] => 0
[ifid] => 1 [id] => [bytes] => 250846 [ifdisposition] => 1 [disposition] => attachment
[ifdparameters] => 1 [dparameters] => Array ( [0] => stdClass Object ( [attribute] => filename
[value] => email.zip ) ) [ifparameters] => 1 [parameters] => Array ( [0] =>
stdClass Object ( [attribute] => name [value] => email.zip ) ) ) )
答案 0 :(得分:2)
请仔细查看$structure->parts
的内容。您有3个部分组成的数组,其中最后两个部分包含以下属性:
[ifdisposition] => 1
[disposition] => attachment
由于第一部分包含[ifdisposition] => 0
并且[disposition]
属性不存在,我们可以推断出ifdisposition
是一个布尔值,它告诉您disposition
是否可用。我们可以通过检查PHP documentation for imap_fetchstructure()
来验证这一点。
要查找所有附件,您应遍历各部分并匹配ifdisposition === true
和disposition === 'attachment'
的所有情况。如果要快速提取所有这些内容,可以执行以下操作:
$attachments = array_filter($structure->parts, function($part) {
return $part->ifdisposition && $part->disposition === 'attachment';
});