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 |