phpmailer,smtp發送郵件實例(轉)

一,用phpmailer發送郵件
查看複製打印?
<?php  
include "class.phpmailer.php";    //包函郵件發送類  
  
//郵件發送  
function send_mail($frommail,$tomail,$subject,$body,$ccmail,$bccmail) {  
 $mail = new PHPMailer();  
 $mail->IsSMTP();                            // 經smtp發送  
 $mail->Host     = "smtp.163.com";           // SMTP 服務器  
 $mail->SMTPAuth = true;                     // 打開SMTP 認證  
 $mail->Username = "zhangyinghf@163.com";    // 用戶名  
 $mail->Password = "**********";          // 密碼  
 $mail->From     = $frommail;                  // 發信人  
 $mail->FromName = "suport of 51yip";        // 發信人別名  
 $mail->AddAddress($tomail);                 // 收信人  
 if(!emptyempty($ccmail)){  
 $mail->AddCC($ccmail);                    // cc收信人  
 }  
 if(!emptyempty($bccmail)){  
 $mail->AddCC($bccmail);                   // bcc收信人  
 }  
 $mail->WordWrap = 50;  
 $mail->IsHTML(true);                            // 以html方式發送  
 $mail->Subject  = $subject;                 // 郵件標題  
 $mail->Body     = $body;                    // 郵件內空  
 $mail->AltBody  =  "請使用HTML方式查看郵件。";  
 return $mail->Send();  
}  
  
$result= send_mail("zhangyinghf@163.com","xtaying@gmail.com","test","test");  
?>  
phpmailer的功能不少,很是的全面,你能夠打開class.phpmailer.php文件看一下,他裏面帶了好多功能,發郵件經常使用的功能它都有。大體以下:
在發送郵時指定多個收件人,抄送地址,暗送地址和回覆地址
支持多種郵件編碼包括:8bit,base64,binary和quoted-printable
支持SMTP驗證
支持冗餘SMTP服務器
支持帶附件的郵件和Html格式的郵件
自定義郵件頭
支持在郵件中嵌入圖片
調試靈活
經測試兼容的SMTP服務器包括:Sendmail,qmail,Postfix,Imail,Exchange等
可運行在任何平臺之上
二,用smtp發送郵件
查看複製打印?
<?php  
include "smtp.php";  
  
$smtpserver = "smtp.163.com";                     //SMTP服務器  
$smtpserverport = 25;                             //SMTP服務器端口  
$smtpusermail = "zhangyinghf@163.com";            //SMTP服務器的用戶郵箱  
$smtpuser = "zhangyinghf@163.com";                //SMTP服務器的用戶賬號  
$smtppass = "*********";                      //SMTP服務器的用戶密碼  
$smtpemailto = "xtaying@gmail.com";               //發送給誰  
  
$mailsubject = "帳號分配";                                                    //郵件主題  
$mailbody = "<h1>this is a test";                //郵件內容  
$mailtype = "HTML";                              //郵件格式(HTML/TXT),TXT爲文本郵件  
  
$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//這裏面的一個true是表示使用身份驗證,不然不使用身份驗證.  
$smtp->debug = true;//是否顯示發送的調試信息  
$mailsubject = mb_convert_encoding($mailsubject, "gb2312", "utf-8");  
$mailbody = mb_convert_encoding($mailbody, "gb2312", "utf-8");  
$result=    $smtp->sendmail($smtpemailto, $smtpusermail,$mailsubject, $mailbody, $mailtype);  
?>  
smtp的功能少多了,不過能知足基本的需求。
相關文章
相關標籤/搜索