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

zh515858237發表於2015-05-27

在shell中使用mail命令傳送郵件,傳送表格需要用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")" 332800462@qq.com  


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

  1. <?php  
  2.   
  3. $to = "somebody@example.com, somebodyelse@example.com";  
  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: <webmaster@example.com>' . "\r\n";  
  33. $headers .= 'Cc: myboss@example.com' . "\r\n";  
  34.   
  35. mail($to,$subject,$message,$headers);  
  36. ?>  

相關文章