网上搜索了些资料,下面是发送邮件的例子:
共两个文件 mail.inc.php和sendmail.php
=================================================================================
mail.inc.php:
$M_db= new Mailconfig(
array(
'host' => "smtp.xxxxx.cn", //发件服务器的地址
'port' => 25, //发件服务器的端口一般默认为25
'auth' => "test", //发件人姓名
'isck' => 1, //发件服务器需要密码验证.
'from' => "test@document.cn", //发件人EMAIL
'user' => "username", //发件人帐号名称
'pass' => "*******************", //发件人帐号密码
)
);
Class Mailconfig {
var $smtp;
function Mailconfig($smtp=array()){
$this->smtp['host'] = $smtp['host'];
$this->smtp['port'] = $smtp['port'];
$this->smtp['auth'] = $smtp['auth'];
$this->smtp['isck'] = $smtp['isck'];
$this->smtp['from'] = $smtp['from'];
$this->smtp['user'] = $smtp['user'];
$this->smtp['pass'] = $smtp['pass'];
}
}/*
发送邮件函数:
$toname----收件人名称
$toemail-----------收件人邮件地址
$subject-----------邮件主题
$message--------邮件内容
$html---------------邮件格式:文本:0,html:1
*/
function sendemail($toname,$toemail,$subject,$message,$html=1){
global $M_db;
$M_db->smtp['html']=$html;
$db_charset="gb2312";
if(!$fp=fsockopen($M_db->smtp['host'],$M_db->smtp['port'],&$errno,&$errstr,30)) return false;
if(substr(fgets($fp,512),0,3)!="220") return false;
if($M_db->smtp['isck']) {
fwrite($fp,"EHLO mobeiguaishou qq4620498/r/n");
while($rt=strtolower(fgets($fp,512))){
if(strpos($rt,"-")!==3 || empty($rt)){
break;
}elseif(strpos($rt,"2")!==0){
return false;
}
}
fwrite($fp, "AUTH LOGIN /r/n");
if(substr(fgets($fp,512),0,3) != 334) return false;
fwrite($fp, base64_encode($M_db->smtp['user'])." /r/n");
if(substr(fgets($fp,512),0,3) != 334) return false;
fwrite($fp, base64_encode($M_db->smtp['pass'])." /r/n");
if(substr(fgets($fp,512),0,3) != 235) return false;
} else {
fwrite($fp, "HELO mobeiguaishou qq4620498/r/n");
}
$from = $M_db->smtp['from'];
$from = preg_replace("/.*/.*/", "//1", $from);
fwrite($fp, "MAIL FROM: /r/n");
if(substr(fgets($fp,512),0,3) != 250){
return false;
}
fwrite($fp, "RCPT TO: /r/n");
//fwrite($fp, "RCPT TO: ,/r/n");
if(substr(fgets($fp,512),0,3) != 250){
return false;
}
fwrite($fp, "DATA/r/n");
if(substr(fgets($fp,512),0,3) != 354){
return false;
}
$subject = str_replace("/n",' ',$subject);
$msg = "Date: ".Date("r")."/r/n";
$msg .= "From: =?$db_charset?B?".base64_encode($M_db->smtp['auth'])."?=/r/n";
$msg .= "To: =?$db_charset?B?".base64_encode($sendtoname)."?=/r/n";
$msg .= "Subject: =?$db_charset?B?".base64_encode($subject)."?=/r/n";
$msg .= "X-mailer: Php Auto SendMail for mobeiguaishou qq4620498/r/n";
$msg .= "Mime-Version: 1.0/r/n";
if ($M_db->smtp['html'])
$msg .= "Content-Type: text/html;/r/n";
else
$msg .= "Content-Type: text/plain;/r/n";
$msg .= "/tcharset=/"$db_charset/"/r/n";
$msg .= "Content-Transfer-Encoding: base64/r/n/r/n";
$msg .= base64_encode($message)."/r/n./r/n";
fwrite($fp, $msg);
fwrite($fp, "QUIT/r/n");
fclose($fp);
return true;
}
?>
========================================================================================
sendmail.php
//这里用循环发送多封邮件
require("mail.inc.php");
//设置发件人信息:
$M_db->smtp['auth']="username";
$M_db->smtp['from']=username@163.com;
if($action='send'){
$mTo="u1@163.com,u2@163.com,u3@163.com"; //收件人邮件地址
$mToArr=split(",",$mTo);
$mContent="邮件内容";
$tlg=true;
//循环发送邮件数组发送邮件
for($i=0;$i<=count($mToArr)-1;$i++)
{
//这里收件人名称和邮件地址相同,实际情况请根据需要修改
if (sendemail($mToArr[$i],$mToArr[$i],$_POST['mailTitle'],$mContent)){
}else{
$tlg=false;
echo "";
exit;
}
}
//echo count($mToArr);
if($tlg){
echo "";
}
}
?>