PHP 教程 PHP 表单 PHP 高级教程 PHP 数据库 PHP XML PHP 与 AJAX PHP 参考手册

发布于 2016-11-18 14:33:22 | 52 次阅读 | 评论: 0 | 来源: 网络整理


定义和用法

mail() 函数允许您从脚本中直接发送电子邮件。

如果电子邮件的投递被成功地接受,则返回 TRUE,否则返回 FALSE。

语法

 mail(to,subject,message,headers,parameters) 

参数描述
to 必需。规定电子邮件的接收者。
subject必需。规定电子邮件的主题。注释:该参数不能包含任何换行字符。
message必需。定义要发送的消息。用 LF(n)将每一行分开。行不应超过70个字符。

Windows 注释:当 PHP 直接连接到 SMTP 服务器时,如果在消息的某行开头发现一个句号,则会被删掉。要解决这个问题,请将单个句号替换成两个句号:
<?php
$txt = str_replace("n.", "n..", $txt);
?>

headers可选。规定额外的报头,比如 From、Cc 和 Bcc。附加标头应该用 CRLF(rn)分开。

注释:发送电子邮件时,它必须包含一个 From 标头。可通过该参数进行设置或在 php.ini 文件中进行设置。

parameters可选。规定 sendmail 程序的额外参数(在 sendmail_path 配置设置中定义)。例如:当 sendmail 和 -f sendmail 的选项一起使用时,sendmail 可用于设置发件人地址。


提示和注释

注释:您需要谨记,电子邮件的投递被接受,并不意味着电子邮件到达了计划的目的地。


实例 1

发送一封简单的电子邮件:

 <?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("somebody@example.com","My subject",$txt);
 ?> 


实例 2

发送一封带有额外报头的电子邮件:

 <?php
 $to = "somebody@example.com";
 $subject = "My subject";
 $txt = "Hello world!";
 $headers = "From: webmaster@example.com" . "rn" .
 "CC: somebodyelse@example.com";

 mail($to,$subject,$txt,$headers);
 ?> 


实例 3

发送一封 HTML 电子邮件:

 <?php
 $to = "somebody@example.com, somebodyelse@example.com";
 $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-版本: 1.0" . "rn";
 $headers .= "Content-type:text/html;charset=iso-8859-1" . "rn";

 // More headers
 $headers .= 'From: <webmaster@example.com>' . "rn";
 $headers .= 'Cc: myboss@example.com' . "rn";

 mail($to,$subject,$message,$headers);
 ?> 


最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务