以前的mail(),已經不在流行,所以放上最近用的實例,留做回憶。
復制代碼 代碼如下:
<?php
require_once(dirname(__FILE__)."/../phpmailer/class.phpmailer.php");
//包含class.phpmailer.php
/**
* @param string $send_to_mail 目標郵件
* @param stinrg $subject 主題
* @param string $body 郵件內容
* @param string $extra_hdrs 附加信息
* @param string $username 收件人
* @param string $replyname 回復人
* @param string $replymail 回復地址
* @return array(bealoon,string) 返回數組包括兩個元素,bealoon表示是否成功,string為提示信息
*/
function SendMail($send_to_mail,$subject,$body,$extra_hdrs,$username,$replyname="reply",$replymail="
[email protected]"){
$mail=new PHPMailer();
$mail->IsSMTP(); //郵件發送方式
$mail->Host="smtp.host.com"; //SMTP服務器主機地址
$mail->SMTPAuth=true; //是否為可信任的SMTP
$mail->Username="
[email protected]"; //SMTP 用戶名 注意:普通郵件認證不需要加 @域名
$mail->Password="******"; //SMTP 用戶密碼
$mail->From="
[email protected]"; //發件人郵件地址
$mail->FromName="send"; //發件人
$mail->CharSet="GB2312"; //指定字符集
$mail->Encoding="base64";
$mail->AddAddress($send_to_mail,$username); //添加發送目標地址
$mail->AddReplyTo($replymail,$replyname); //添加回復地址
$mail->IsHTML(true); //郵件類型為HTML格式
$mail->Subject=$subject; //郵件主題
//郵件內容
$mail->Body="<html><head>
<meta http-equiv='Content-Language' content='zh-cn'>
<meta http-equiv='Content-Type' content='text/html; charset=GB2312'></head>
<body>
".$body."
</body>
</html>";
$mail->AltBody="text/html"; //內容文本格式
if (@!$mail->Send()) {
$results=array("result"=>false,"message"=>$mail->ErrorInfo);
return $results;
}else{
$results = array("result"=>true,"message"=>"郵件已經發送到{$send_to_mail}!");
return $results;
}
}
$send_mail=SendMail($to,$subject,$content,$headers,$name);
if($send_mail["result"]){
echo $send_mail["message"];
}else{
echo $send_mail["message"];
}
exit();
?>
復制代碼 代碼如下:
<?php
include ('class/class.phpmailer.php');
$config = array(
'host'=>'smtp.163.com',
'port'=>'25',
'user'=>'***',
'passwd'=>'****',
'from'=>'
[email protected]',
'fromname'=>'鄭州',
);
$subject = 'this is a test mail';
$body = '<table style="background:#dfdfdf"><tr><td>測試內容</td></tr><tr><td>這是內容</td></tr></table>';
$address='
[email protected]';
$username='本人';
$mail = new PHPMailer();
$mail->CharSet = 'gb2312';
$mail->IsSMTP();
$mail->Host = $config['host'];
$mail->Port = $config['port'];
$mail->From = $config['from'];
$mail->FromName = $config['fromname'];
$mail->SMTPAuth = true;
$mail->Username = $config['user'];
$mail->Password = $config['passwd'];
$mail->Subject=$subject;
$mail->AltBody="text/html";
$mail->MsgHTML($body);
$mail->AddAddress($address,$username);
if(!$mail->Send())
{
echo "Mail Error :".$mail->ErrorInfo;
}else
{
echo "恭喜發送成功!";
}