【问题标题】:php imap check if email has attachmentphp imap 检查电子邮件是否有附件
【发布时间】:2012-04-13 22:21:09
【问题描述】:

我正在尝试构建一个小型网络邮件应用程序。当我阅读收件箱中的所有电子邮件时,我想为每封邮件显示是否有附件。这行得通,但问题是这样做需要很长时间,1Mb 电子邮件附件大约需要 0.5 秒。将其与收件箱中具有大附件文件的所有电子邮件相乘:| 我的问题是:如何在不加载整个电子邮件的情况下检查电子邮件是否已附加?那可能吗 ? 下面是我现在使用的代码:

function existAttachment($part)
 { 
  if (isset($part->parts))
  { 
   foreach ($part->parts as $partOfPart)
   { 
    $this->existAttachment($partOfPart); 
   } 
  } 
  else
  { 
   if (isset($part->disposition))
   { 
    if ($part->disposition == 'attachment')
    { 
     echo '<p>' . $part->dparameters[0]->value . '</p>'; 
     // here you can create a link to the file whose name is  $part->dparameters[0]->value to download it 
     return true; 
    }   
   } 
  } 
  return false;
 }

 function hasAttachments($msgno)
 {
  $struct = imap_fetchstructure($this->_connection,$msgno,FT_UID); 
  $existAttachments = $this->existAttachment($struct);

  return $existAttachments;
 }

【问题讨论】:

    标签: php email imap


    【解决方案1】:

    imap_fetchstructure 确实会获取整个电子邮件内容以进行分析。遗憾的是,没有其他方法可以检查附件。

    也许您可以使用来自imap_headerinfo 的邮件大小信息来预测邮件是否包含附件。

    另一种方法是在后台定期获取电子邮件并将其与其内容和 UID 一起存储,以供以后在数据库中查找。无论如何,您需要在以后搜索特定消息时执行此操作。 (搜索“晚餐”时不要扫描while imap帐户)

    【讨论】:

      【解决方案2】:

      要检查电子邮件是否有附件,请使用 $structure->parts[0]->parts。

      $inbox = imap_open($mailserver,$username, $password, null, 1, ['DISABLE_AUTHENTICATOR' => 'PLAIN']) or die(var_dump(imap_errors()));
      
      $unreadEmails = imap_search($inbox, 'UNSEEN');
      
      $email_number = $unreadEmails[0];
      
      $structure = imap_fetchstructure($inbox, $email_number);
      
      if(isset($structure->parts[0]->parts))
      {
         // has attachment
      }else{
         // no attachment
      }
      

      【讨论】:

      • 下载整个电子邮件,这是我想要避免的
      猜你喜欢
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      • 2013-04-11
      • 2011-04-10
      • 2011-10-07
      • 2016-05-09
      • 2018-02-25
      相关资源
      最近更新 更多