无风自舞

也无风雨也无晴

php邮件发送

今天要用到php的邮件发送,便用phpmailer弄了个

类文件:emaillclass.php

<?php

class PhpMailer{
var $smtpHost;
var $smtpUser;
var $smtpPass;
var $mailFrom;
 var $contentType = "html";
var $errMsg = '';
function __construct($host,$user,$pass){
$this->smtpHost = $host;
$this->smtpUser = $user;
$this->smtpPass = $pass;
$this->mailFrom = $this->smtpUser;
}
    function send($addr,$fromName,$title,$content){
     $headers = "Content-Type: text/".$this->contentType."; charset=\"gb2312\"
Content-Transfer-Encoding: base64"; 
     $lb="
"; 
     $hdr = explode($lb,$headers);
     if($content){
   $bdy = explode($lb,$content);

$smtp = array( 
array("EHLO hello".$lb,"220,250","EHLO error: "), 
array("AUTH LOGIN".$lb,"334","AUTH error:"), 
array(base64_encode($this->smtpUser).$lb,"334","AUTHENTIFICATION error : "), 
array(base64_encode($this->smtpPass).$lb,"235","AUTHENTIFICATION error : ")
); 
$smtp[] = array("MAIL FROM: <".$this->mailFrom.">".$lb,"250","MAIL FROM error: "); 
$smtp[] = array("RCPT TO: <".$addr.">".$lb,"250","RCPT TO error: "); 
$smtp[] = array("DATA".$lb,"354","DATA error: "); 
$smtp[] = array("From: ".$fromName.$lb,"",""); 
$smtp[] = array("To: ".$addr.$lb,"",""); 
$smtp[] = array("Subject: ".$title.$lb,"",""); 
foreach($hdr as $h) {
     $smtp[] = array($h.$lb,"","");

$smtp[] = array($lb,"",""); 
if($bdy) {
   foreach($bdy as $b) {
      $smtp[] = array(base64_encode($b.$lb).$lb,"","");
    }

$smtp[] = array(".".$lb,"250","DATA(end)error: "); 
$smtp[] = array("QUIT".$lb,"221","QUIT error: "); 
$fp = @fsockopen($this->smtpHost, 25); 
if (!$fp) $this->errMsg = "<b>错误:</b> 无法连接到 ".$this->smtpHost.""; 
while($result = @fgets($fp, 1024)){
   if(substr($result,3,1) == " ") { break; }
}
foreach($smtp as $req){ 
@fputs($fp, $req[0]); 
if($req[1]){ 
   while($result = @fgets($fp, 1024)){ 
    if(substr($result,3,1) == " ") { break; } 
   }; 
if (!strstr($req[1],substr($result,0,3))){ 
     $this->errMsg.=$req[2].$result.""; 



@fclose($fp); 
if($this->errMsg ==''){
return true; 
}else{
return false;
}
    }
}

?>

调用:send.php

<?php
 require_once('emaillclass.php');
 $test =new PhpMailer("smtp服务器地址","发件箱地址","发件箱密码");
 $send =$test->send('收件箱地址','发件箱地址','标题','邮件内容');
?>


阅读更多
个人分类: php
想对作者说点什么? 我来说一句

PHP邮件发送(类 例子)

2008年11月19日 4KB 下载

php邮件发送实例php邮件发送实例

2011年07月01日 1.03MB 下载

php 邮件发送代码

2011年03月14日 3KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭