PHP – 解析 Email 原始檔案(文本)
可使用 EmailParser 套件來分析。我們透過 Composer 安裝,或是去 Github 下載
1 2 3 |
composer require michaelesmith/email-parser |
使用方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
$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