PHP 實現下載郵件功能,保存為eml格式
阿新 • • 發佈:2018-09-15
eml info html 功能 騰訊企業郵箱 toe 技術 處理 htm
引言:因為業務需求,需要獲取訂單的往來郵件,其中涉及到下載郵件功能。由於騰訊企業郵箱API接口不支持下載郵件功能,只能自己寫。
獲取郵件內容,保存到數據庫
步驟略:網上有很多現成的,利用imap來獲取將獲取到的郵件內容保存為eml格式
如下為從數據庫中取的的數據:
{ "id": "135", "sender": "[email protected]", "sender_name": "我是發件人", "to_name": "收件人1,收件人2,ymy", "to_list": "[email protected],[email protected],[email protected]", "cc_list": "抄送人", "cc_name": "[email protected]", "title": "我是主題", "content": "<p>測試一下!!!{43575}<br></p>", "email_time": "2018-09-15 09:56:05", "create_time": "2018-09-15 09:56:05", "update_time": "2018-09-15 09:56:05" }
代碼實現:
$mailDetail = json_decode($json, true); $str = ""; // 郵件日期(後面的+0800是北京時間的時區) $str .= "Date: {$mailDetail[‘email_time‘]} +0800".PHP_EOL; // 處理收件人 $str .= "To: "; $toName = explode(‘,‘, $mailDetail[‘to_name‘]); $toEmail = explode(‘,‘, $mailDetail[‘to_list‘]); foreach ($toName as $k => $name){ $str .= "$name <$toEmail[$k]>"; if($k+1 != count($toName)){ $str .= ", "; }else{ $str .= PHP_EOL; } } // 處理抄送人 $ccName = explode(‘,‘, $mailDetail[‘cc_name‘]); $ccEmail = explode(‘,‘, $mailDetail[‘cc_list‘]); if(!empty($ccName)){ $str .= "Cc: "; foreach ($ccName as $k => $name){ $str .= "$name <$ccEmail[$k]>"; if($k+1 != count($ccName)){ $str .= ", "; }else{ $str .= PHP_EOL; } } } // 發件人 $str .= "From: {$mailDetail[‘sender_name‘]} <{$mailDetail[‘sender‘]}>".PHP_EOL; // 郵件主題 $str .= "Subject: {$mailDetail[‘title‘]}". PHP_EOL; // 郵件輸出格式 $str .= "MIME-Version: 1.0". PHP_EOL; $str .= "Content-Type: text/html; charset=UTF-8". PHP_EOL; $str .= "Content-Transfer-Encoding: 8bit". PHP_EOL; // 郵件內容 $str .= PHP_EOL . "{$mailDetail[‘content‘]}".PHP_EOL; // 保存 file_put_contents(‘test.eml‘, $str);
- test.eml內容
- 將test.eml用foxmail打開
- 以下問題待修復:
- 郵件附件沒有加進去
PHP 實現下載郵件功能,保存為eml格式