使用linux的mail命令發送html格式的郵件

今天在shell中使用mail命令發送郵件,希望發送表格,就用了html的格式來發送。但是開始的時候發現Outlook收到的顯示爲html的源碼,

就查閱了下相關資料,問題解決了,記錄下,以備以後再用:


以下內容轉載自:http://blog.csdn.net/chengfei112233/article/details/7288054


linux使用mail函數發送需要添加 header參數,將發送內容指定爲txt/html


解決:

1. 使用命令行發送郵件測試

在linux命令行執行以下代碼即可發送郵件

  1. echo "<b><div style='color:red'>HTML Message goes here</div></b>" | mail -s "$(echo -e "This is the subject\nContent-Type: text/html")" [email protected]  


2. 在php函數中使用mail方法發送時,需要指定發送的header參數

  1. <?php  
  2.   
  3. $to = "[email protected][email protected]";  
  4. $subject = "HTML email";  
  5.   
  6. $message = "  
  7. <html>  
  8. <head>  
  9. <title>HTML email</title>  
  10. </head>  
  11. <body>  
  12. <p>This email contains HTML Tags!</p>  
  13. <table>  
  14. <tr>  
  15. <th>Firstname</th>  
  16. <th>Lastname</th>  
  17. </tr>  
  18. <tr>  
  19. <td>John</td>  
  20. <td>Doe</td>  
  21. </tr>  
  22. </table>  
  23. </body>  
  24. </html>  
  25. ";  
  26.   
  27. // 當發送 HTML 電子郵件時,請始終設置 content-type  
  28. $headers = "MIME-Version: 1.0" . "\r\n";  
  29. $headers .= "Content-type:text/html;charset=utf8" . "\r\n";  
  30.   
  31. // 更多報頭  
  32. $headers .= 'From: <[email protected]>' . "\r\n";  
  33. $headers .= 'Cc: [email protected]' . "\r\n";  
  34.   
  35. mail($to,$subject,$message,$headers);  
  36. ?>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章