当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > mail() 函数

PHP
PHP 安全邮件
MySQL 介绍
连接 MySQL
创建 MySQL
MySQL 插入记录
MySQL 选择记录
MySQL Where
MySQL Order By
MySQL 记录更新
MySQL 删除记录
PHP ODBC
XML Expat Parser
XML SimpleXML
PHP 数组参考
PHP Calendar
PHP Date
PHP Directory
PHP Filesystem
PHP FTP
PHP HTTP

PHP 中的 mail() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 615 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The mail() function allows you to send emails directly from a script.
Mail()函数的作用是:直接从脚本发送邮件。

This function returns TRUE if the email was successfully accepted for delivery, otherwise it returns FALSE.
如果同意发送邮件,则返回True;否则返回False。

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
必要参数。指定邮件主题。注意:这个参数不可以包含换行符[newline characters]
message Required. Defines the message to be sent. Each line should be separated with a LF (n). Lines should not exceed 70 characters.
必要参数。定义发送的邮件内容

Windows note: If a full stop is found on the beginning of a line in the message, it might be removed. To solve this problem, replace the full stop with a double dot:
Windows 提示: 如果“full stop”出现在信息的某一行开头,那么该行信息将被删除;为了解决这个问题,用“double dot”来替代“full stop”。

<?php
$txt = str_replace("n.", "n..", $txt);
?>

headers Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (rn).
可选参数。指定附加的header,如:From、Cc以及Bcc;如要继续添加header,则应该用(rn)隔开

Note: When sending an email, it must contain a From header. This can be set with this parameter or in the php.ini file.
注意:当发送一封email时,它将包含一个From header;这个参数可以设置在php.ini文件中

parameters Optional. Specifies an additional parameter to the sendmail program (the one defined in the sendmail_path configuration setting). (i.e. this can be used to set the envelope sender address when using sendmail with the -f sendmail option)
可选参数。为sendmail程序指定一个附加的参数(它将在sendmail_path属性设置中定义)。(例如:通过是用sendmail的“-f sendmail”选项,它可以设置envelope sender的路径)


Tips and Notes
注意点

Note: Keep in mind that just because the email was accepted for delivery, it does NOT mean the email is actually sent and received.
注意:同意发送或接收email的并不意味着接收者已经真正收到了email。


Example 1
案例1

Send a simple email:
发送一封简单的email:

<?php
$txt = "First line of textnSecond line of text";
// Use wordwrap() if lines are longer than 70 characters
$txt = wordwrap($txt,70);
// Send email
mail("[email protected]","My subject",$txt);
?>


Example 2
案例2

Send an email with extra headers:
发送一封包含额外header的email:

<?php
$to = "[email protected]";
$subject = "My subject";
$txt = "Hello world!";
$headers = "From: [email protected]" . "rn" .
"CC: [email protected]";
mail($to,$subject,$txt,$headers);
?>


Example 3
案例3

Send an HTML email:
发送一封HTML格式的email:

<?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>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "rn";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";
// More headers
$headers .= 'From: <[email protected]>' . "rn";
$headers .= 'Cc: [email protected]' . "rn";
mail($to,$subject,$message,$headers);
?>

评论 (0) All

登陆 | 还没注册?