用PHP Email發送表單內容(3)-mail()函數的幾個參數詳解

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   使用   for   

這一節主要講的是mail函數。

首先閱讀手冊,看看mail函數的基本形式:http://php.net/manual/en/function.mail.php

mail();函數接受幾個參數:

第一個參數:收件者

$to = ‘[email protected]’;

或者我們有多個收件者:

$to = ‘[email protected]’,’[email protected]’;

或者我們還可以加上收件者的名字:

$ = ‘david <[email protected]>,james <[email protected]>’;

第二個參數:郵件主題

$subject = ‘這是郵件的主題’;

第三個參數:郵件內容

$body = ‘這裡是郵件的內容部分’;

注意:$body必須是一個single string,如果你要用到不同的資料來源,請將他們全部組合成一個single string

 

以上三個參數就是mail();函數所必須的三個參數,剩下的參數是optional:

Mail($to ,$subject ,$body);

剩下的兩個參數不是必須的,但也經常會用到:

第四個參數:headers

$headers 就是email的headers 例如:Bcc,Cc,From等;如果你要加入多餘一個的headers,需要把每一個單獨的headers用\r\n分割開來。

$headers = “From:[email protected]”;(要用雙引號,因為裡面要加入\r\n)有些伺服器自動設定了這個From,但如果伺服器沒有自動化佈建,你需要在這裡進行設定,否則會出現錯誤。

Mail();函數預設使用Western European encoding,如果要使用utf-8編碼,則需要再$headers中加入一句:

$headers .= “Content-Type:text/plain; charset-utf-8\r\n”;

如果要抄送給某人,則:

$headers .=”Cc: [email protected]”;

第五個參數,是伺服器需要的參數,檢測你是否有許可權發郵件,防止垃圾郵件。

‘[email protected]’;(由-f開頭,然後緊跟你的郵箱地址)

 

整個mail();函數返回一個布爾值,如果發送成功了(這裡是指PHP已經將這些資料成功的提交給了mail agent,並不是100%發送成功了),則返回true,否則返回false,我們可以儲存這個資料 ,因為後面要用到。

  1 $sucess = mail($to ,$subject, $body, $heders, ‘[email protected]’); 

 

除了上一節的表格檔案,這一件我們又建立了一個mail.php用來處理郵件有關的邏輯。所以,到了這一節有兩個PHP檔案,代碼分別是:

form.php:

 1 <html> 2     <head> 3         <meta charset="utf-8"> 4         <title>聯絡我們</title> 5         <link href="test.css" rel="stylesheet" type="text/css"> 6         <style> 7             input,label,textarea{ 8                 display:block; 9                 margin:1em;10             }11             textarea{12                 width:400px;13                 height:200px;14             }15         </style>16     </head>17     18     <body>19         <form method="post" action=‘<?php echo($_SERVER[‘PHP_SELF‘]); ?>‘;>20             <label>姓名:</label>21             <input type="text" name="name" id="name"></input>22             23             <label>郵箱地址:</label>24             <input type="text" name="email" id="email"></input>25             26             <label>姓名:</label>27             <textarea name="comment" id="comment"></textarea>28             29             <input type="submit" name="send" id="send" value="提交評論"></submit>30         </form>31         32     <?php33         if($_POST){34             echo ‘我們收到了來自$_POST的資訊‘;35             print_r($_POST);36         }elseif($_GET){37             echo ‘我們收到了來自$_GET的資訊‘;38             print_r($_GET);39         }40     ?>41     </body>42 </html>

mail.php:

1 <?php2 $to =‘Wang Jian <[email protected]>,anothe<[email protected]>‘;3 $subject = ‘這是郵件主題部分‘;4 $body = ‘這是郵件主體部分‘;//郵件主體部分必須是一個single string,所以如果要用到許多不同的資料來源,需要把他們全部組合成一個single string5 $headers = "From:[email protected]\r\n";//因為裡面有逸出字元,所以要用雙引號,不同的headers項目必須要用逸出字元隔開;6 $headers .= "Content-Type:text/plain; charset-utf-8\r\n";7 $headers .= "Cc:[email protected]"8 9 $sucess = mail($to, $subject, $body, $headers, ‘[email protected]‘);

 

用PHP Email發送表單內容(3)-mail()函數的幾個參數詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.