php 使用qmail发送邮件实现代码

 更新时间:2016年11月25日 17:01  点击:2154
以前都是利用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;

}

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

 点击查看原图

 代码如下 复制代码

$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>";


   ?>

用的mail()函数,感觉不好用,而且感觉这个邮件地址不能太多,一次copy了100个可能会出问题,下面提供一款可以限制发送邮件php类函数。
*/

 代码如下 复制代码

class sendmail{
    function __construct(){
        $this->set();
        $this->auth = 1;
    }

    function set($server=yj_sysmail_smtp,$user="",$password=yj_sysmail_pass,$port=yj_sysmailport,$type=1,$mailusername=0){
        $user=($user=="")?substr(yj_sysmail,0,stripos(yj_sysmail,"@")):$user;

        $this->type = $type;
        $this->server = $server;
        $this->port = $port;
        $this->user = $user;
        $this->password = $password;
        $this->mailusername = $mailusername;
    }

    function send($email_to, $email_subject, $email_message, $email_from = ''){
        $email_subject = '=?utf-8?b?'.base64_encode(str_replace("r", '', $email_subject)).'?=';
        $email_message = str_replace("rn.", " rn..", str_replace("n", "rn", str_replace("r", "n", str_replace("rn", "n", str_replace("nr", "r", $email_message)))));
        $email_from = $email_from == '' ? '=?utf-8?b?'.base64_encode(yj_sysname)."?= <".yj_sysmail.">" : (preg_match('/^(.+?) <(.+?)>$/',$email_from, $from) ? '=?utf-8?b?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
        $emails = explode(',', $email_to);
        foreach($emails as $touser){
            $tousers[] = preg_match('/^(.+?) <(.+?)>$/',$touser, $to) ? ($this->mailusername ? '=?utf-8?b?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
        }
        $email_to = implode(',', $tousers);
        $headers = "mime-version: 1.0rnto: {$email_to}rnfrom: {$email_from}<{$email_from}>rnx-priority: 3rnx-mailer: eglive rndate: ".date("r")."rncontent-type: text/html; charset=utf-8rn";

        if($this->type == 1){
            return $this->smtp($email_to, $email_subject, $email_message, $email_from, $headers);
        }elseif($this->type == 2){
            return @mail($email_to, $email_subject, $email_message, $headers);
        }else{
            ini_set('smtp', $this->server);
            ini_set('smtp_port', $this->port);
            ini_set('sendmail_from', $email_from);
            return @mail($email_to, $email_subject, $email_message, $headers);
        }
    }

    function smtp($email_to, $email_subject, $email_message, $email_from = '', $headers = ''){
        if(!$fp = @fsockopen($this->server, $this->port, $errno, $errstr, 10)){
            $this->errorlog('smtp', "($this->server:$this->port) connect - unable to connect to the smtp server", 0);
            return false;
        }
        stream_set_blocking($fp, true);
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != '220'){
            $this->errorlog('smtp', "$this->server:$this->port connect - $lastmessage", 0);
            return false;
        }
        fputs($fp, ($this->auth ? 'ehlo' : 'helo')." phpcmsrn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250){
            $this->errorlog('smtp', "($this->server:$this->port) helo/ehlo - $lastmessage", 0);
            return false;
        }
        while(1){
            if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage)){
                break;
            }
            $lastmessage = fgets($fp, 512);
        }
        fputs($fp, "auth loginrn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334){
            $this->errorlog('smtp', "($this->server:$this->port) auth login - $lastmessage", 0);
            return false;
        }
        fputs($fp, base64_encode($this->user)."rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 334){
            $this->errorlog('smtp', "($this->server:$this->port) username - $lastmessage", 0);
            return false;
        }
        fputs($fp, base64_encode($this->password)."rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 235){
            $this->errorlog('smtp', "($this->server:$this->port) password - $lastmessage", 0);
            return false;
        }
        fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "", $email_from).">rn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 250){
            fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "", $email_from).">rn");
            $lastmessage = fgets($fp, 512);
            if(substr($lastmessage, 0, 3) != 250){
                $this->errorlog('smtp', "($this->server:$this->port) mail from - $lastmessage", 0);
                return false;
            }
        }
        $email_tos = array();
        $emails = explode(',', $email_to);
        foreach($emails as $touser){
            $touser = trim($touser);
            if($touser){
                fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "", $touser).">rn");
                $lastmessage = fgets($fp, 512);
                if(substr($lastmessage, 0, 3) != 250){
                    fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "", $touser).">rn");
                    $lastmessage = fgets($fp, 512);
                    $this->errorlog('smtp', "($this->server:$this->port) rcpt to - $lastmessage", 0);
                    return false;
                }
            }
        }
        fputs($fp, "datarn");
        $lastmessage = fgets($fp, 512);
        if(substr($lastmessage, 0, 3) != 354){
            $this->errorlog('smtp', "($this->server:$this->port) data - $lastmessage", 0);
        }
        $headers .= 'message-id: <'.gmdate('ymdhs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_server['http_host'].">rn";
        fputs($fp, "date: ".gmdate('r')."rn");
        fputs($fp, "to: ".$email_to."rn");
        fputs($fp, "subject: ".$email_subject."rn");
        fputs($fp, $headers."rn");
        fputs($fp, "rnrn");
        fputs($fp, "$email_messagern.rn");
        $lastmessage = fgets($fp, 512);
        fputs($fp, "quitrn");
        return true;
    }

    function errorlog($type, $message, $is){
        $this->error[] = array($type, $message, $is);
    }

    function mailmess($mess=""){
        return "<style>*{font-family:verdana;font-size:13px;/*www.111cn.net提供*/}</style>
        <table cellpadding='5' cellspacing='1' border='0' width='750' style='background-color:#fff;border:1px solid #3b5998;padding:15px;'>
        <tr><td style='background:#3b5998;color:#fff;font-weight:bold;font-size:14px;'>".yj_syscnname." - ".yj_sysname."</td></tr>
        <tr><td>{$mess}</td></tr>
      
        </table>";
    }
}

本款php发送邮件代码他利用了php自由的邮件发送函数mail进行邮件发送的,他会利用二种试一是判断mail函数是否可用,如果行就利用它来发送否则fsockopen来操作。
*/

 代码如下 复制代码

class email
{
 function email()
 {
  $this->__construct();
 }

 function __construct()
    {
  @define('charset','gbk');
  $this->set(mail_server, mail_port, mail_user, mail_pwd,mail_type);
  $this->auth = 1;
 }

 function set($server, $port, $user, $password, $type = 1, $delimiter = 1, $mailusername = 0)
 {
  $this->type = $type;
  $this->server = $server;
  $this->port = $port;
  $this->user = $user;
  $this->password = $password;
        $this->delimiter = $delimiter == 1 ? "rn" : ($delimiter == 2 ? "r" : "n");
  $this->mailusername = $mailusername;
 }
 
 function send($email_to, $email_subject, $email_message, $email_from = '')
 {
  global $dircms;
  $email_to=iconv("utf-8", "gbk", $email_to);
  $email_subject=iconv("utf-8", "gbk", $email_subject);
  $email_message=iconv("utf-8", "gbk", $email_message);
  $email_subject = '=?'.charset.'?b?'.base64_encode(str_replace("r", '', $email_subject)).'?=';
  $email_message = str_replace("rn.", " rn..", str_replace("n", "rn", str_replace("r", "n", str_replace("rn", "n", str_replace("nr", "r", $email_message)))));
  $adminemail = $this->type == 1 ? $dircms['mail_user'] : $dircms['mail_user'];
  $email_from = $email_from == '' ? '=?'.charset.'?b?'.base64_encode($dircms['site_name'])."?= <$adminemail>" : (preg_match('/^(.+?) <(.+?)>$/',$email_from, $from) ? '=?'.charset.'?b?'.base64_encode($from[1])."?= <$from[2]>" : $email_from);
  $emails = explode(',', $email_to);
  foreach($emails as $touser)
  {
   $tousers[] = preg_match('/^(.+?) <(.+?)>$/',$touser, $to) ? ($this->mailusername ? '=?'.charset.'?b?'.base64_encode($to[1])."?= <$to[2]>" : $to[2]) : $touser;
  }
  $email_to = implode(',', $tousers);
  $headers = "from: $email_from{$this->delimiter}x-priority: 3{$this->delimiter}x-mailer: dircms {$this->delimiter}mime-version: 1.0{$this->delimiter}content-type: text/html; charset=".charset."{$this->delimiter}";
  if($this->type == 1)
  {
   return $this->smtp($email_to, $email_subject, $email_message, $email_from, $headers);
  }
  elseif($this->type == 2)
  {
   return @mail($email_to, $email_subject, $email_message, $headers);
  }
  else
  {
   ini_set('smtp', $this->server);
   ini_set('smtp_port', $this->port);
   ini_set('sendmail_from', $email_from);
   return @mail($email_to, $email_subject, $email_message, $headers);
  }
 }

 function smtp($email_to, $email_subject, $email_message, $email_from = '', $headers = '')
 {
  global $dircms;
  if(!$fp = fsockopen($this->server, $this->port, $errno, $errstr, 10))
  {
   $this->errorlog('smtp', "($this->server:$this->port) connect - unable to connect to the smtp server", 0);
   return false;
  }
  stream_set_blocking($fp, true);
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != '220')
  {
   $this->errorlog('smtp', "$this->server:$this->port connect - $lastmessage", 0);
   return false;
  }
  fputs($fp, "ehlo dircmsrn");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 220 && substr($lastmessage, 0, 3) != 250)
  {
   $this->errorlog('smtp', "($this->server:$this->port) helo/ehlo - $lastmessage", 0);
   return false;
  }
  while(1)
  {
   if(substr($lastmessage, 3, 1) != '-' || empty($lastmessage))
   {
    break;
   }
   $lastmessage = fgets($fp, 512);
  }
  fputs($fp, "auth loginrn");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 334)
  {
   $this->errorlog('smtp', "($this->server:$this->port) auth login - $lastmessage", 0);
   return false;
  }
  fputs($fp, base64_encode($this->user)."rn");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 334)
  {
   $this->errorlog('smtp', "($this->server:$this->port) username - $lastmessage", 0);
   return false;
  }
  fputs($fp, base64_encode($this->password)."rn");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 235)
  {
   $this->errorlog('smtp', "($this->server:$this->port) password - $lastmessage", 0);
   return false;
  }
  fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "", $email_from).">rn");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 250)
  {
   fputs($fp, "mail from: <".preg_replace("/.*<(.+?)>.*/", "", $email_from).">rn");
   $lastmessage = fgets($fp, 512);
   if(substr($lastmessage, 0, 3) != 250)
   {
    $this->errorlog('smtp', "($this->server:$this->port) mail from - $lastmessage", 0);
    return false;
   }
  }
  $email_tos = array();
  $emails = explode(',', $email_to);
  foreach($emails as $touser)
  {
   $touser = trim($touser);
   if($touser)
   {
    fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "", $touser).">rn");
    $lastmessage = fgets($fp, 512);
    if(substr($lastmessage, 0, 3) != 250)
    {
     fputs($fp, "rcpt to: <".preg_replace("/.*<(.+?)>.*/", "", $touser).">rn");
     $lastmessage = fgets($fp, 512);
     $this->errorlog('smtp', "($this->server:$this->port) rcpt to - $lastmessage", 0);
     return false;
    }
   }
  }
  fputs($fp, "datarn");
  $lastmessage = fgets($fp, 512);
  if(substr($lastmessage, 0, 3) != 354)
  {
   $this->errorlog('smtp', "($this->server:$this->port) data - $lastmessage", 0);
  }
  $headers .= 'message-id: <'.gmdate('ymdhs').'.'.substr(md5($email_message.microtime()), 0, 6).rand(100000, 999999).'@'.$_server['http_host'].">{$this->delimiter}";
  fputs($fp, "date: ".gmdate('r')."rn");
  fputs($fp, "to: ".$email_to."rn");
  fputs($fp, "subject: ".$email_subject."rn");
  fputs($fp, $headers."rn");
  fputs($fp, "rnrn");
  fputs($fp, "$email_messagern.rn");
  fputs($fp, "quitrn");
  return true;
 }

 function errorlog($type, $message, $is)
 {
  $this->error[] = array($type, $message, $is);
 }
}

[!--infotagslink--]

相关文章

  • 图解PHP使用Zend Guard 6.0加密方法教程

    有时为了网站安全和版权问题,会对自己写的php源码进行加密,在php加密技术上最常用的是zend公司的zend guard 加密软件,现在我们来图文讲解一下。 下面就简单说说如何...2016-11-25
  • ps怎么使用HSL面板

    ps软件是现在很多人都会使用到的,HSL面板在ps软件中又有着非常独特的作用。这次文章就给大家介绍下ps怎么使用HSL面板,还不知道使用方法的下面一起来看看。 &#8195;...2017-07-06
  • Plesk控制面板新手使用手册总结

    许多的朋友对于Plesk控制面板应用不是非常的了解特别是英文版的Plesk控制面板,在这里小编整理了一些关于Plesk控制面板常用的使用方案整理,具体如下。 本文基于Linu...2016-10-10
  • 使用insertAfter()方法在现有元素后添加一个新元素

    复制代码 代码如下: //在现有元素后添加一个新元素 function insertAfter(newElement, targetElement){ var parent = targetElement.parentNode; if (parent.lastChild == targetElement){ parent.appendChild(newEl...2014-05-31
  • 使用GruntJS构建Web程序之构建篇

    大概有如下步骤 新建项目Bejs 新建文件package.json 新建文件Gruntfile.js 命令行执行grunt任务 一、新建项目Bejs源码放在src下,该目录有两个js文件,selector.js和ajax.js。编译后代码放在dest,这个grunt会...2014-06-07
  • 使用percona-toolkit操作MySQL的实用命令小结

    1.pt-archiver 功能介绍: 将mysql数据库中表的记录归档到另外一个表或者文件 用法介绍: pt-archiver [OPTION...] --source DSN --where WHERE 这个工具只是归档旧的数据,不会对线上数据的OLTP查询造成太大影响,你可以将...2015-11-24
  • 如何使用php脚本给html中引用的js和css路径打上版本号

    在搜索引擎中搜索关键字.htaccess 缓存,你可以搜索到很多关于设置网站文件缓存的教程,通过设置可以将css、js等不太经常更新的文件缓存在浏览器端,这样访客每次访问你的网站的时候,浏览器就可以从浏览器的缓存中获取css、...2015-11-24
  • jQuery 1.9使用$.support替代$.browser的使用方法

    jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合...2014-05-31
  • MySQL日志分析软件mysqlsla的安装和使用教程

    一、下载 mysqlsla [root@localhost tmp]# wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz--19:45:45-- http://hackmysql.com/scripts/mysqlsla-2.03.tar.gzResolving hackmysql.com... 64.13.232.157Conn...2015-11-24
  • 安装和使用percona-toolkit来辅助操作MySQL的基本教程

    一、percona-toolkit简介 percona-toolkit是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,这些任务包括: 检查master和slave数据的一致性 有效地对记录进行归档 查找重复的索...2015-11-24
  • C#注释的一些使用方法浅谈

    C#注释的一些使用方法浅谈,需要的朋友可以参考一下...2020-06-25
  • php语言中使用json的技巧及json的实现代码详解

    目前,JSON已经成为最流行的数据交换格式之一,各大网站的API几乎都支持它。我写过一篇《数据类型和JSON格式》,探讨它的设计思想。今天,我想总结一下PHP语言对它的支持,这是开发互联网应用程序(特别是编写API)必须了解的知识...2015-10-30
  • PHP实现无限级分类(不使用递归)

    无限级分类在开发中经常使用,例如:部门结构、文章分类。无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式; 查找分类A下面所有分类包含的文章。1.实现原理 几种常见的实现方法,各有利弊。其中...2015-10-23
  • php类的使用实例教程

    php类的使用实例教程 <?php /** * Class program for yinghua05-2 * designer :songsong */ class Template { var $tpl_vars; var $tpl_path; var $_deb...2016-11-25
  • NodeJS实现阿里大鱼短信通知发送

    本文给大家介绍的是nodejs实现使用阿里大鱼短信API发送消息的方法和代码,有需要的小伙伴可以参考下。...2016-01-20
  • 双冒号 ::在PHP中的使用情况

    前几天在百度知道里面看到有人问PHP中双冒号::的用法,当时给他的回答比较简洁因为手机打字不大方便!今天突然想起来,所以在这里总结一下我遇到的双冒号::在PHP中使用的情况!双冒号操作符即作用域限定操作符Scope Resoluti...2015-11-08
  • PHP测试成功的邮件发送案例

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

    Promise是异步编程的一种解决方案,在ES6中Promise被列为了正式规范,统一了用法,原生提供了Promise对象。接下来通过本文给大家介绍Promise的介绍及基本用法,感兴趣的朋友一起看看吧...2021-10-21
  • 使用jquery修改表单的提交地址基本思路

    基本思路: 通过使用jquery选择器得到对应表单的jquery对象,然后使用attr方法修改对应的action 示例程序一: 默认情况下,该表单会提交到page_one.html 点击button之后,表单的提交地址就会修改为page_two.html 复制...2014-06-07
  • PHP mysql与mysqli事务使用说明 分享

    mysqli封装了诸如事务等一些高级操作,同时封装了DB操作过程中的很多可用的方法。应用比较多的地方是 mysqli的事务。...2013-10-02