代码之家  ›  专栏  ›  技术社区  ›  Arkh

如何用PHP提取邮件代理?

  •  3
  • Arkh  · 技术社区  · 15 年前

    我正在从数据库中提取电子邮件,它们存储在数据库中作为字符串。我需要解析这些电子邮件以提取它们的附件。我想一定已经有图书馆了,但我找不到。

    6 回复  |  直到 6 年前
        1
  •  3
  •   Mez    15 年前

    PEAR::Mail::mimeDecode 应该做你想做的

        2
  •  3
  •   bucabay    15 年前

    php有一个mailparse扩展,比使用本地php的pear替代方法快得多。

    以下是包装此扩展的库:

    http://code.google.com/p/php-mime-mail-parser/

    例子:

    // require mime parser library
    require_once('MimeMailParser.class.php');
    
    // instantiate the mime parser
    $Parser = new MimeMailParser();
    
    // set the email text for parsing
    $Parser->setText($text);
    
    // get attachments
    $attachments = $Parser->getAttachments();
    
        3
  •  1
  •   André Hoffmann    15 年前

    这可以使用Zend框架的Zend-mail组件完成。

    也许在文档中也可以找到这个例子有助于:

    // get the first none multipart part
    $part = $message;
    while ($part->isMultipart()) {
        $part = $message->getPart(1);
    }
    echo 'Type of this part is ' . strtok($part->contentType, ';') . "\n";
    echo "Content:\n";
    echo $part->getContent();
    

    不过,我不知道怎样才能让Zend Mail从字符串中读取数据,也许需要做一些工作,但是这样你就可以拥有一个完整的库来满足你的需要,还有一些(比如阅读主题等)。

    编辑 :

    我刚刚看了一下它,发现您所要做的就是编写一个自己的存储实现(zend-mail-storage-abstract子类),这并不难做到。

    我认为这是你能得到的最干净的解决方案,尽管要使它工作需要一点努力。

    如果你正在寻找一种更快速、更脏的解决方案,其他人可能会帮你。

    希望有帮助。

        4
  •  1
  •   Kazik    7 年前

    phpmimeparser-分析多部分mime消息(附件、内联图像、base64、带引号的可打印) https://github.com/breakermind/PhpMimeParser 您可以从文件、字符串中剪切MIME消息。

    // Load .eml mime message from file
    $str = file_get_contents('mime-mixed-related-alternative.eml');
    
    // Format output
    echo "<pre>";
    
    // Create object MimeParser
    $m = new PhpMimeParser($str);
    
    // Show Emails
    print_r($m->mTo);
    print_r($m->mFrom);
    print_r($m->mBcc);
    print_r($m->mCc);
    
    // Show Message
    echo $m->mSubject;
    echo $m->mHtml;
    echo $m->mText;
    print_r($m->mInlineList);
    
    // Show Files
    print_r($m->mFiles);
    
        5
  •  0
  •   Al.    15 年前

    电子邮件附件是MIME编码的,并使用邮件头添加到邮件正文中。Pearmime解码包将满足您的需求: http://pear.php.net/package/Mail_mimeDecode

        6
  •  0
  •   GuyPaddock    6 年前

    那里有一个更好的图书馆: https://github.com/php-mime-mail-parser/php-mime-mail-parser

    它可以通过作曲器安装。

    因为这个名字和谷歌代码上的名字一样,在这个帖子的其他地方也有链接,所以我认为它是它的继承者,但我不知道。在谷歌代码上很难找到作者信息,所以我不能确认它是同一位作者。

    一些示例代码(来自项目自述文件):

    // Include the library first
    require_once __DIR__.'/vendor/autoload.php';
    
    $path = 'path/to/mail.txt';
    $Parser = new PhpMimeMailParser\Parser();
    
    $Parser->setStream(fopen($path, "r"));
    
    //  Loop through all the Attachments
    if (count($attachments) > 0) {
      foreach ($attachments as $attachment) {
        echo 'Filename : '.$attachment->getFilename().'<br />'; // logo.jpg
        echo 'Filesize : '.filesize($attach_dir.$attachment->getFilename()).'<br />'; // 1000
        echo 'Filetype : '.$attachment->getContentType().'<br />'; // image/jpeg
        echo 'MIME part string : '.$attachment->getMimePartStr().'<br />'; // (the whole MIME part of the attachment)
      }
    }