今天在shell中使用mail命令發送郵件,希望發送表格,就用了html的格式來發送。但是開始的時候發現Outlook收到的顯示爲html的源碼,
就查閱了下相關資料,問題解決了,記錄下,以備以後再用:
以下內容轉載自:http://blog.csdn.net/chengfei112233/article/details/7288054
linux使用mail函數發送需要添加 header參數,將發送內容指定爲txt/html
解決:
1. 使用命令行發送郵件測試
在linux命令行執行以下代碼即可發送郵件
- 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]
- <?php
- $to = "[email protected], [email protected]";
- $subject = "HTML email";
- $message = "
- <html>
- <head>
- <title>HTML email</title>
- </head>
- <body>
- <p>This email contains HTML Tags!</p>
- <table>
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- </tr>
- <tr>
- <td>John</td>
- <td>Doe</td>
- </tr>
- </table>
- </body>
- </html>
- ";
- // 當發送 HTML 電子郵件時,請始終設置 content-type
- $headers = "MIME-Version: 1.0" . "\r\n";
- $headers .= "Content-type:text/html;charset=utf8" . "\r\n";
- // 更多報頭
- $headers .= 'From: <[email protected]>' . "\r\n";
- $headers .= 'Cc: [email protected]' . "\r\n";
- mail($to,$subject,$message,$headers);
- ?>