PHP allows you to send e-mails directly from a script.
PHP允許你通過腳本直接發送e-mail。
The PHP mail() Function
PHP mail()函數
The PHP mail() function is used to send emails from inside a script.
PHP mail()函數可以從腳本內發送email。
Syntax
語法
mail(to,subject,message,headers,parameters)
Parameter
參數
Description
描述
to
Required. Specifies the receiver / receivers of the email
必要參數。指定email的接收者
subject
Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters
必要參數。指出email的主題。注意:這個參數包含的字符不能換行(只能顯示一行)
message
Required. Defines the message to be sent. Each line should be separated with a LF (n). Lines should not exceed 70 characters
必要參數。定義發送的信息。每行都必須用(n)分隔開。每行不能超過70個字符
headers
Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (rn)
可選參數。指定附加標題,如:form、Cc及Bcc。附加標題通過(rn)進行分隔
parameters
Optional. Specifies an additional parameter to the sendmail program
可選參數。給sendmail程序指定一個附加參數
Note: For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. Read more in our PHP Mail reference.
注意:PHP需要安裝、運行email系統才可以使用mail函數。所使用的程序是通過php.ini文件中的配置屬性來確定的。閱讀更多的關於PHP Mail參數。
PHP Simple E-Mail
簡易的E-mail
The simplest way to send an email with PHP is to send a text email.
使用PHP發送郵件的最簡單的方法就是發送一個文本格式的email。
In the example below we first declare the variables ($to, $subject, $message, $from, $headers), then we use the variables in the mail() function to send an e-mail:
在下面的案例當中,我們首先聲明變量($to, $subject, $message, $from, $headers)。接下來我們使用mail()函數中的變量來發送一封e-mail:
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
PHP Mail Form
PHP 郵件[mail]格式
With PHP, you can create a feedback-form on your website. The example below sends a text message to a specified e-mail address:
你可以使用PHP在你的網站上建立一個反饋表單[feedback-form]。我們將通過下面的案例教你如何向一個指定的e-mail地址發送文本郵件:
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "
Email:
Subject:
Message:
";}
?>
This is how the example above works:
下面我們具體的講一下上述代碼的工作原理:
First, check if the email input field is filled out
首先,檢查email輸入框是否已被填充信息
If it is not set (like when the page is first visited); output the HTML form
如果沒有填充信息(比如:第一次訪問頁面);輸出HTML表單
If it is set (after the form is filled out); send the email from the form
如果已填充信息(表但已被填寫);則發送表單中的email
When submit is pressed after the form is filled out, the page reloads, sees that the email input is set, and sends the email
當表單填寫完畢,確認提交之後,頁面將被重新裝載;此時,你可以看見email設置完畢,並被發送出去
PHP Mail Reference
PHP 郵件[mail]參數
For more information about the PHP mail() function, visit our PHP Mail Reference.
如果你想了解更多關於PHP mail()函數,請訪問我們的PHP Mail 參數。