php PEAR mail发送邮件实例

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

<!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

/*邮件发送类
*功能:php教程 socket 使用smtp服务器发送邮件
*作者:longlong
*时间:2007-11-26
*转帖请加本贴引用地址:showpost.asp教程?threadid=1345 
*/
class smtp
{
/* 全局变量 */
var $smtp_port;
var $time_out;
var $host_name;
var $log_file;
var $relay_host;
var $debug;
var $auth;
var $user;
var $pass;

/* private variables */ 
var $sock;

/* 构造函数 */

function smtp($relay_host = "", $smtp_port = 25,$auth = false,$user,$pass)
{
  $this->debug = false;
  $this->smtp_port = $smtp_port;
  $this->relay_host = $relay_host;
  $this->time_out = 30; //is used in fsockopen() 
   
  #

  $this->auth = $auth;//auth
  $this->user = $user;
  $this->pass = $pass;
   
  #

  $this->host_name = "localhost"; //is used in helo command 
  $this->log_file = "";


  $this->sock = false;
}

/* 主函数,发送邮件 */

function sendmail($flag, $boundary, $to, $from, $subject = "", $body = "", $mailtype, $cc = "", $bcc = "", $additional_headers = "")
{
  $mail_from = $this->get_address($this->strip_comment($from));
  $body = ereg_replace("(^|(rn))(.)", "1.3", $body);
  $header = "mime-version:1.0rn";
   
  //*
  if($mailtype=="html"){
  //echo $boundary;exit;
if($flag==2)
{
  $header .= "content-type:multipart/mixed; boundary= $boundaryrn";
  //$header .= "content-type:text/htmlrn";
}
else

  $header .= "content-type:text/htmlrn";
}
  }
  //*/

  $header .= "to: ".$to."rn";

  if ($cc != "") {
  $header .= "cc: ".$cc."rn";
  }

  $header .= "from: $from<".$from.">rn";
  $header .= "subject: ".$subject."rn";
  $header .= $additional_headers;
  $header .= "date: ".date("r")."rn";
  $header .= "x-mailer:by redhat (php/".phpversion().")rn";

  //$header.=$body;//edit by shaolong
  
  list($msec, $sec) = explode(" ", microtime());

  $header .= "message-id: <".date("ymdhis", $sec).".".($msec*1000000).".".$mail_from.">rn";

  $to = explode(",", $this->strip_comment($to));

  if ($cc != "") {
  $to = array_merge($to, explode(",", $this->strip_comment($cc)));
  }


  if ($bcc != "") {
  $to = array_merge($to, explode(",", $this->strip_comment($bcc)));
  }

  $sent = true;

  foreach ($to as $rcpt_to) {
  $rcpt_to = $this->get_address($rcpt_to);
   
  if (!$this->smtp_sockopen($rcpt_to)) {
  $this->log_write("error: cannot send email to ".$rcpt_to."n");
  $sent = false;
  continue;
  }

  if ($this->smtp_send($this->host_name, $mail_from, $rcpt_to, $header, $body)) {
  $this->log_write("e-mail has been sent to <".$rcpt_to.">n");
  } else {
  $this->log_write("error: cannot send email to <".$rcpt_to.">n");
  $sent = false;
  }

  fclose($this->sock);

  $this->log_write("disconnected from remote hostn");
  }
  return $sent;
}

/* 私有函数 */

function smtp_send($helo, $from, $to, $header, $body = "")
{
  if (!$this->smtp_putcmd("helo", $helo)) {

  return $this->smtp_error("sending helo command");
  }

  #auth

  if($this->auth){
  if (!$this->smtp_putcmd("auth login", base64_encode($this->user))) {
  return $this->smtp_error("sending helo command");
  }

  if (!$this->smtp_putcmd("", base64_encode($this->pass))) {
  return $this->smtp_error("sending helo command");
  }
  }

  #

  if (!$this->smtp_putcmd("mail", "from:<".$from.">")) {
  return $this->smtp_error("sending mail from command");
  }

  if (!$this->smtp_putcmd("rcpt", "to:<".$to.">")) {
  return $this->smtp_error("sending rcpt to command");
  }

  if (!$this->smtp_putcmd("data")) {
  return $this->smtp_error("sending data command");
  }

  if (!$this->smtp_message($header, $body)) {
  return $this->smtp_error("sending message");
  }


  if (!$this->smtp_eom()) {
  return $this->smtp_error("sending <cr><lf>.<cr><lf> [eom]");
  }


  if (!$this->smtp_putcmd("quit")) {
  return $this->smtp_error("sending quit command");
  }
  return true;
}


function smtp_sockopen($address)
{
  if ($this->relay_host == "") {

  return $this->smtp_sockopen_mx($address);

  } else {
  return $this->smtp_sockopen_relay();
  }
}


function smtp_sockopen_relay()
{
  $this->log_write("trying to ".$this->relay_host.":".$this->smtp_port."n");

  $this->sock = @fsockopen($this->relay_host, $this->smtp_port, $errno, $errstr, $this->time_out);

  if (!($this->sock && $this->smtp_ok())) {

  $this->log_write("error: cannot connenct to relay host ".$this->relay_host."n");

  $this->log_write("error: ".$errstr." (".$errno.")n");

  return false;
  }
  $this->log_write("connected to relay host ".$this->relay_host."n");
  return true;


function smtp_sockopen_mx($address)
{
  $domain = ereg_replace("^.+@([^@]+)$", "1", $address);
  if (!@getmxrr($domain, $mxhosts)) {
  $this->log_write("error: cannot resolve mx "".$domain.""n");
  return false;
  }

  foreach ($mxhosts as $host) {
  $this->log_write("trying to ".$host.":".$this->smtp_port."n");

  $this->sock = @fsockopen($host, $this->smtp_port, $errno, $errstr, $this->time_out);

  if (!($this->sock && $this->smtp_ok())) {
  $this->log_write("warning: cannot connect to mx host ".$host."n");
  $this->log_write("error: ".$errstr." (".$errno.")n");
  continue;
  }

  $this->log_write("connected to mx host ".$host."n");
  return true;
  }

  $this->log_write("error: cannot connect to any mx hosts (".implode(", ", $mxhosts).")n");
  return false;
}


function smtp_message($header, $body)
{
  fputs($this->sock, $header."rn".$body);
  $this->smtp_debug("> ".str_replace("rn", "n"."> ", $header."n> ".$body."n> "));
  return true;
}


function smtp_eom()
{
  fputs($this->sock, "rn.rn");

  $this->smtp_debug(". [eom]n");

  return $this->smtp_ok();
}


function smtp_ok()
{
  $response = str_replace("rn", "", fgets($this->sock, 512));
  $this->smtp_debug($response."n");

  if (!ereg("^[23]", $response)) {
  fputs($this->sock, "quitrn");
  fgets($this->sock, 512); 
  $this->log_write("error: remote host returned "".$response.""n");
  return false;

  }
  return true;
}


function smtp_putcmd($cmd, $arg = "")
{
  if ($arg != "") {
  if($cmd=="") $cmd = $arg;
  else $cmd = $cmd." ".$arg;
  }

  fputs($this->sock, $cmd."rn");
  $this->smtp_debug("> ".$cmd."n");
  return $this->smtp_ok();
}


function smtp_error($string)
{
  $this->log_write("error: error occurred while ".$string.".n");
  return false;
}


function log_write($message)
{
  $this->smtp_debug($message);
  if ($this->log_file == "") {

  return true;
  }

  $message = date("m d h:i:s ").get_current_user()."[".getmypid()."]: ".$message;
  if (!@file_exists($this->log_file) || !($fp = @fopen($this->log_file, "a"))) {
  $this->smtp_debug("warning: cannot open log file "".$this->log_file.""n");
  return false;;
  }

  flock($fp, lock_ex);
  fputs($fp, $message);
  fclose($fp);
  return true;
}

function strip_comment($address)
{
  $comment = "([^()]*)";
  while (ereg($comment, $address)) {
  $address = ereg_replace($comment, "", $address);
  }
  return $address;
}

function get_address($address)
{
  $address = ereg_replace("([ trn])+", "", $address);
  $address = ereg_replace("^.*<(.+)>.*$", "1", $address);
  return $address;
}


function smtp_debug($message)
{
  if ($this->debug) {
  echo $message;
  }
}
}

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

*/

不需要邮件服务器,不使用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;

}

[!--infotagslink--]

相关文章

  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • PHP测试成功的邮件发送案例

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

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

    这篇文章主要介绍了c# 如何实现发送邮件的功能,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
  • php邮件发送的两种方式

    这篇文章研究的主要内容就是使用PHP来发送电子邮件,总结为以下两种方法:一、使用PHP内置的mail()函数<&#63;php $to = "test@163.com"; //收件人 $subject = "Test"; //主题 $message = "This is a test mail!"; //正文...2015-10-30
  • python实现企业微信定时发送文本消息的实例代码

    这篇文章主要介绍了python实现企业微信定时发送文本消息的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-25
  • c#使用netmail方式发送邮件示例

    这篇文章主要介绍了c#使用netmail方式发送邮件的示例,大家参考使用吧...2020-06-25
  • PHP测试成功的邮件发送案例

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

    PHPMailer在SAE上无法发送邮件怎么回事呢,我们以前在php5.2.7版本中使用了PHPMailer是可以发,但移到sae中发现无法发邮件了,那么此问题如何解决 在SAE上直接用5.2.7...2016-11-25
  • 整理几个android后台发送邮件的方法

    本文我们整理了三个android后台发送邮件的方法及示例,第一个是不借助Intent在android后台发送Email,第二个是用在收集应用的异常信息,第三个是分享一个android后台发送邮...2016-09-20
  • Perl中使用MIME::Lite发送邮件实例

    这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
  • 网上找到的两个PHP发送邮件的例子,很不错,贴出来给初学者参考吧(不知道是否有兄弟曾贴过),呵呵(2

    Advanced Example Here we will show the full capabilities of the PHP mail function. PHP Code: <?php echo "<html><body>"; $recipient = "Kris Arndt <karn@nu...2016-11-25
  • Delphi7中群发Email邮件的方法

    这篇文章主要介绍了Delphi7中群发Email邮件的方法,涉及邮件服务器软件的使用,电子邮件的判断与发送功能的实现,是非常实用的技巧,需要的朋友可以参考下...2020-06-30
  • PHP利用Jmail组件实现发送邮件

    学过asp的朋友可能知道jmail组件是使用在asp中一个常用的邮箱发送功能,在php中如果想调用jmail功能我们需要使用com组件来操作。 我们先来介绍格式 代码如...2016-11-25
  • node.js 基于 STMP 协议和 EWS 协议发送邮件

    这篇文章主要介绍了node.js 基于 STMP 协议和 EWS 协议发送邮件的示例,帮助大家更好的理解和使用node.js,感兴趣的朋友可以了解下...2021-02-15
  • phpMailer 发送邮件

    //原创:www.111cn.net 注明:转载说明来处www.111cn.net // 昨天听一网友说用php 里面的mail发邮件发不出去,我想一般都是发不了的,现在大多数据邮件提供商都不准那样了...2016-11-25
  • C#编程实现发送邮件的方法(可添加附件)

    这篇文章主要介绍了C#编程实现发送邮件的方法,具备添加附件的功能,涉及C#文件传输及邮件发送的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • php中利用curl smtp发送邮件实例

    本文章来介绍人一下关于与我们不同的发送邮件的方法我们来利用php curl stmp来实现邮件的发送程序。 $ telnet 邮箱SMTP服务地址 25 Trying 邮箱服务IP地址......2016-11-25
  • Python基于httpx模块实现发送请求

    这篇文章主要介绍了Python基于httpx模块实现发送请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-08
  • php定时发送邮件

    <?php // 请求 PHPmailer类 文件 require_once("class.phpmailer.php"); //发送Email函数 function smtp_mail ( $sendto_email, $subject, $body, $extra_hd...2016-11-25