【发布时间】:2019-11-11 03:12:20
【问题描述】:
我正在使用我的 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。我试图在谷歌上找到答案如何计算附件的值在使用 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 ) ) ) )
【问题讨论】:
-
问题:如果您没有附件,
count($structure->parts)是否返回 1。在这种情况下,它可能意味着“电子邮件”== 1 部分 2 附件 == 2 部分,因此 3 部分 我当然是在这里猜测 -
var_dump($structure->parts);的输出是什么? -
@HTMHell 哦,对不起,我忘了包含 $structure->parts 的输出。请参阅我更新的问题。谢谢。