当前位置:编程学习 > php >>

php mail()函数发送电子邮件代码

php教程 mail() 函数用于从脚本中发送电子邮件。

语法
mail(to,subject,message,headers,parameters)参数 描述
to 必需。规定 email 接收者。
subject 必需。规定 email 的主题。注释:该参数不能包含任何新行字符。
message 必需。定义要发送的消息。应使用 lf (n) 来分隔各行。
headers 可选。规定附加的标题,比如 from、cc 以及 bcc。

应当使用 crlf (rn) 分隔附加的标题。
 
parameters 可选。对邮件发送程序规定额外的参数。


*/
$to='nobody@example.com';        //定义邮箱地址
$subject='mail';           //定义发送邮件的主题
$message='hello,php';         //定义邮件主体内容
$headers='from:webmaster@example.com'."rn".
'reply-to:webmaster@example.com'."rn".
 'x-mailer:php/'.phpversion();        //定义附加信息
mail($to,$subject,$message,$headers);      //发送邮件
?>
<?php
//发送html格式的邮件例子
$to='test@example.com'.',';
$to.='test1@example.com';        //定义多个收件人地址
$subject='mailhtml';          //定义邮件标题
//下面是邮件的html代码
$message='
<html>
<head>
<title>birthday reminders for august</title>
</head>
<body>
<p>here are the birthdays upcoming in august!</p>
<table>
  <tr>
    <th>person</th><th>day</th><th>month</th><th>year</th>
  </tr>
  <tr>
    <td>joe</td><td>3rd</td><td>august</td><td>1970</td>
  </tr>
  <tr>
    <td>sally</td><td>17th</td><td>august</td><td>1973</td>
  </tr>
</table>
</body>
</html>
';
//发送http请求
$headers='mime-version:1.0'."rn";        //定义附加信息
$headers.='content-type:text/html;charset=iso-8856-1'."rn";
$headers.='to:mary<mary@example.com>,kelly<kelly@example.com>'."rn";
$headers.='from:birthday reminder<birthday@example.com>'."rn";
$headers.='cc:birthdayarchive@example.com'."rn";
$headers.='bcc:birthdaycheck@example.com'."rn";
mail($to,$subject,$message,$headers);       //执行发送操作

/*

注释:php 需要一个已安装且正在运行的邮件系统,以便使邮件函数可用。所用的程序通过在 php.ini 文件中的配置设置进行定义

*/

补充:Php教程,邮件处理 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,