php 计算一封电子邮件中的附件数量
php counts on how many attachments in a single email
我正在 PHP 计算我在一封电子邮件中收到的附件数量的价值。
当我尝试这个时:
echo count($structure->parts);
输出将如下所示:
3
我在一封电子邮件中有 2 个附件,所以输出 return 不正确。所以当我尝试这个时:
for($i = 0; $i < count($structure->parts); $i++) {
echo $i;
}
输出return像这样:
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 个附件,因此 return 输出应该显示 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 ) ) ) )
请仔细阅读$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';
});
我正在 PHP 计算我在一封电子邮件中收到的附件数量的价值。
当我尝试这个时:
echo count($structure->parts);
输出将如下所示:
3
我在一封电子邮件中有 2 个附件,所以输出 return 不正确。所以当我尝试这个时:
for($i = 0; $i < count($structure->parts); $i++) {
echo $i;
}
输出return像这样:
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 个附件,因此 return 输出应该显示 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 ) ) ) )
请仔细阅读$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';
});