PHP mail() 函數用於從腳本中發送電子郵件。php
PHP 須要一個已安裝且正在運行的郵件系統,以便使郵件函數可用。所用的程序經過在 php.ini 文件中的配置設置進行定義。html
mail(to,subject,message,[headers],[parameters])
參數 | 描述 |
---|---|
to | 必需。規定 email 接收者。 |
subject | 必需。規定 email 的主題。註釋:該參數不能包含任何新行字符。 |
message | 必需。定義要發送的消息。應使用 LF (\n) 來分隔各行。 |
headers | 可選。規定附加的標題,好比 From、Cc 以及 Bcc。服務器 應當使用 CRLF (\r\n) 分隔附加的標題。ide |
parameters | 可選。對郵件發送程序規定額外的參數。 |
eg:函數
經過 PHP 發送電子郵件的最簡單的方式是發送一封文本 email。post
<?php $to = "someone@example.com"; $subject = "Test mail"; $message = "Hello! This is a simple email message."; $from = "legolas@example.com"; $headers = "From: $from"; mail($to,$subject,$message,$headers); echo "Mail Sent."; ?>
eg:spa
反饋表單:orm
<html> <body> <?php if (isset($_REQUEST['email'])) //if "email" is filled out, send email { //send email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail( "legolas@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 "<form method='post' action='mailform.php'> Email: <input name='email' type='text' /><br /> Subject: <input name='subject' type='text' /><br /> Message:<br /> <textarea name='message' rows='15' cols='40'> </textarea><br /> <input type='submit' /> </form>"; } ?> </body> </html>
郵件函數的行爲受 php.ini 的影響。htm
名稱 | 默認 | 描述 | 可更改 |
---|---|---|---|
SMTP | "localhost" | Windows 專用:SMTP 服務器的 DNS 名稱或 IP 地址。 | PHP_INI_ALL |
smtp_port | "25" | Windows 專用:SMTP 段口號。自 PHP 4.3 起可用。 | PHP_INI_ALL |
sendmail_from | NULL | Windows 專用:規定從 PHP 發送的郵件中使用的 "from" 地址。 | PHP_INI_ALL |
sendmail_path | NULL | Unix 系統專用:規定sendmail 程序的路徑(一般 /usr/sbin/sendmail 或 /usr/lib/sendmail) | PHP_INI_SYSTEM |