【问题标题】:php counts on how many attachments in a single emailphp 计算一封电子邮件中有多少附件
【发布时间】: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 而不是 3012,这仍然是显示 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-&gt;parts) 是否返回 1。在这种情况下,它可能意味着“电子邮件”== 1 部分 2 附件 == 2 部分,因此 3 部分 我当然是在这里猜测
  • var_dump($structure-&gt;parts); 的输出是什么?
  • @HTMHell 哦,对不起,我忘了包含 $structure->parts 的输出。请参阅我更新的问题。谢谢。

标签: php email imap


【解决方案1】:

请仔细查看$structure-&gt;parts的内容。您有一个包含 3 个部分的数组,其中最后两个包含以下属性:

[ifdisposition] => 1 
[disposition] => attachment

由于第一部分包含[ifdisposition] =&gt; 0 并且[disposition] 属性不存在,我们可以推断ifdisposition 是一个布尔值,它告诉您disposition 是否可用。我们可以通过检查PHP documentation for imap_fetchstructure() 来验证这一点。

要查找所有附件,您应该遍历部分并匹配ifdisposition === truedisposition === 'attachment' 的所有情况。如果您想快速提取所有这些内容,可以执行以下操作:

$attachments = array_filter($structure->parts, function($part) {
    return $part->ifdisposition && $part->disposition === 'attachment';
});

【讨论】:

  • 非常感谢您的帮助,因为我可以看到它运行良好。我使用if (($structure-&gt;parts[$i]-&gt;ifdisposition) &amp;&amp; ($structure-&gt;parts[$i]-&gt;disposition == 'attachment')) {,效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
相关资源
最近更新 更多