使用linux的mail命令发送html格式的邮件

来源:互联网 发布:化妆品网络推广方案 编辑:程序博客网 时间:2024/05/20 02:24

今天在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")" 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. ?>  

1 0