PHP – 解析 Email 原始檔案(文本)

可使用 EmailParser 套件來分析。我們透過 Composer 安裝,或是去 Github 下載

composer require michaelesmith/email-parser

使用方式

$parser = new \MS\Email\Parser\Parser();
$message = $parser->parse($email); // email 原始格式

// 取得從哪裡寄來的 email address
$message->getFrom()->getAddress();

// 取得如果來信的 email 有名字
$message->getFrom()->getName();

// 發信時間
$message->getDate()

// 標題
$message->getSubject();

// HTML 的內容
$message->getHtmlBody();

// 多筆夾帶的檔案物件
$attachments = $message->getAttachments();

// 第一筆檔案
$attachments[0]

// 取得名稱與類型
$attachments[0]->getFilename();
$attachments[0]->getMimeType();

// 檔案內容
$attachments[0]->getContent();

 

繼續閱讀

Comments

發表迴響