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

 更新时间:2016年11月25日 17:01  点击:1408

语法
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 文件中的配置设置进行定义

*/

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php发送邮件</title>
</head>

<body>
<table width="611" height="200" border="1">
  <tr>
    <td width="601"><form id="form1" name="form1" method="post" action="send.php">
      <table width="600" height="240" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td width="102" height="28">收件人地址</td>
          <td width="213"><input name="to" type="text" id="to" /></td>
          <td width="84">发送人地址</td>
          <td width="201"><input name="from" type="text" id="from" value="cert@163.com" /></td>
        </tr>
        <tr>
          <td height="33">发送人用户名</td>
          <td><input name="name" type="text" id="username" value="cert" /></td>
          <td>邮箱密码</td>
          <td><input name="password" type="password" id="password" value="***" /></td>
        </tr>
        <tr>
          <td height="27">smtp服务器</td>
          <td><input name="smtp" type="text" id="smtp" value="smtp.163.com" /></td>
          <td colspan="2">注:163邮箱smtp为: smtp.163.com</td>
          </tr>
        <tr>
          <td height="26">标题</td>
          <td colspan="3"><input name="subject" type="text" id="subject" value="cert测试php发送邮件" size="50" /></td>
          </tr>
        <tr>
          <td height="69">内容</td>
          <td colspan="3"><textarea name="content" cols="50" rows="6" id="content">计算机紧急响应组欢迎你!
http://www.111cn.net
组织网站即将进行改版</textarea></td>
          </tr>
        <tr>
       
          <td>&nbsp;</td>
          <td align="right"><input type="submit" name="submit" value="发送" /></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </table>
        <p>说明:我用163的邮箱发给 163 或 126 的邮箱立刻就能收到。大家试试。</p>
    </form>
    </td>
  </tr>
</table>
</body>
</html>

<?
require_once 'mail.php';

$conf['mail'] = array(
        'host'     => 'smtp.126.com',                  //smtp服务器地址
        'auth'     => true,                           //true表示smtp服务器需要验证,false不需要
        'username' => 'liangbowen',                   //用户名
        'password' => '******'                        //密码
);


//发送邮件
$headers['from']    = 'liangbowen@126.com';               //发信地址
$headers['to']      = 'liangbowen@hotmail.com';               //收信地址
$headers['subject'] = 'test mail send by php bowen.mvbb.com';   //邮件标题
$mail_object = &mail::factory('smtp', $conf['mail']);   
//邮件正文
$body = "这是一封自己发给自己的邮件。";

$mail_res = $mail_object->send($headers['to'], $headers, $body); //发送

                           
if(pear::iserror($mail_res)){      //检测错误
    die($mail_res->getmessage());
}
else{
 echo "send successful!";
}

源码下载

http://down.111cn.net/down/code/php/qitayuanma/2010/1220/22333.html

不需要邮件服务器,不使用mail内置函数,一个类就搞定,利用php教程mailer类我写了一个自定义函数 sendmail() ,very实用!

以前也在几个php论坛上发表过这个发邮件的函数,今天再发,因为today要附上使用例子,如果你还不会用,那就要补补php基础课了。

  1.准备文件 sendmail.class.php文件、phpmailer类     下载  

2.下载后,解压 phpmailer.rar 到服务器的任何目录下

3.打开 sendmail.class.php ,修改如下(浅黄色部分是必须 要修改的变量值)

 // 以下 5 个变量值必须据实修改
 $host     = '61.183.41.172';
 $username = 'admin@php95.com';
 $password = "******"     
 $from     = 'admin@php95.com';   
 $fromname = '天马博客';  

// 先设置 $to $subject $content 这三个变量的值,再调用 sendmail 函数来发送邮件
$to = 'nt2030@qq.com';
$subject = '测试phpmailer类发送邮件';
$content = '测试phpmailer类发送邮件';
sendmail($to,$subject,$content);

4.修改 sendmail.class.php 完毕后,运行它..

你的邮件是否发送成功?

天马测试本代码,is ok,见证:

 点击查看原图

以前都是利用mail函数或phpermail进行邮件发送,今天看这款利用qmail进行邮件发送,写法非常简单,是一款不错的工具。

function send_check_mail($email, $subject,$uid,$buffer)
{
    echo "hello";
 $command = "/var/qmail/bin/qmail-inject ".$email; //qmail程序地址,$email是要发送的地址
 $handle = popen($command, "w"); //打开管道
 if (!$handle) {
  return false;
 }

 $from = "yangxuemei2012@111cn.net"; //发件人
 fwrite($handle, "from: ".$from."n"); //往管道写数据
 fwrite($handle, "return-path: ".$from."n");
 fwrite($handle, "to: ".$uid."n");
 fwrite($handle, "subject: ".$subject."n");
 fwrite($handle, "mime-version: 1.0n");
 fwrite($handle, "content-type: text/html; charset="gb2312"nn");
 fwrite($handle, $buffer."n");
 pclose($handle); //关闭管道

 return true;
}
$subject = "测试邮件";

$uid = $_post['uid']; //from信息
$content= "<html><body>".$u_email

   ." 您好!<br><br>谢谢,www.111cn.net!<br</body></html>"; //内容信息

$u_email = "machunjie2003@111cn.net"; //发送到的邮箱
if (send_check_mail($u_email, $subject, $uid, $content)) {

 echo "恭喜!发送投票邮件到您的邮箱!<br><br>请检查您的邮箱:<font color=#cc0033>".$u_email." </font><br><br>". $close;
 } else {

 echo "很不幸,发送投票邮件到您的邮箱失败,请重试或者联系开发人员。<br><br>". $close;

}

 代码如下 复制代码

$pdfname="test.pdf";
$email="test@test.com";
$text = "您好,附件中是您需要的pdf文件。请点击下载。<br><a href=http://www.111cn.net>www.111cn.net</a>";         //文本内容

$text = base64_encode($text);     //用base64方法把它编码
$text = chunk_split($text);     //把这个长字符串切成由每行76个字符组成的小块

$subject = $pdfname;         //标题
$from = "admin@111cn.net";     //发送者
$to = $email;     //接受者

//附件
// 定义分界线
$boundary = "nextpart_".uniqid("");
$boundary2 = "nextpart_".uniqid("");
$headers = "to: $torn";
$headers .= "from: $fromrn";
$headers .="mime-version: 1.0rn";
$headers .= "content-type: multipart/mixed;
            boundary="----=_$boundary"rn";

$read=file_get_contents($pdfname);

$read = base64_encode($read);     //用base64方法把它编码
$read = chunk_split($read);     //把这个长字符串切成由每行76个字符组成的小块

//现在我们可以建立邮件的主体
$body = "this is a multi-part message in mime format.

------=_$boundary
content-type: multipart/alternative;
    boundary="----=_$boundary2";

------=_$boundary2
content-type: text/html;
    charset="gbk"
content-transfer-encoding: base64

$text

------=_$boundary2--

------=_$boundary
content-type: application/octet-stream;
    charset="gbk";
    name="$pdfname"
content-disposition: attachment; filename="$pdfname"
content-transfer-encoding: base64

$read

-------=_$boundary--";

if(mail($to, $subject,$body,$headers))
   echo "您需要的pdf文件(".$pdfname.")已经发往您的邮箱:".$to."。<br>请查收。";
else
   echo "抱歉,发送失败了。<br>";


   ?>

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • PHP测试成功的邮件发送案例

    mail()函数的作用:连接到邮件服务器,利用smtp协议,与该服务器交互并投邮件。注意:1、mail函数不支持esmtp协议,---即,只能直投,不能登陆2、由上条,我们只能直投至最终的收件服务器地址.而该地址,又是在PHP.ini中指定的,所...2015-10-30
  • PHP加密解密函数详解

    分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • php的mail函数发送UTF-8编码中文邮件时标题乱码的解决办法

    最近遇到一个问题,就是在使用php的mail函数发送utf-8编码的中文邮件时标题出现乱码现象,而邮件正文却是正确的。最初以为是页面编码的问题,发现页面编码utf-8没有问题啊,找了半天原因,最后找到了问题所在。 1.使用 PEAR 的...2015-10-21