首页 > 编程技术 > php

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

发布时间:2016-11-25 17:01

以前都是利用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--]

您可能感兴趣的文章: