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

16 篇文章 0 订阅
14 篇文章 0 订阅

今天在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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值