thinkphp集成系列之phpmailer批量发送邮件
前段時間寫了一篇博客:thinkphp集成系列之短信驗證碼、訂單通知
說了這是一個短信通知泛濫的年代;大部分網站的郵箱注冊都已經被短信注冊所取代;
但是郵件和短信相比在一些場景依然有著重要的意義和優勢;
1:零成本;發郵件沒有費用;
2:內容豐富且量大;郵件可以長篇大論;圖文并茂;
3:增加訪問量;用戶很容易通過郵件中的鏈接訪問網站;
好了;下面來來為thinkphp集成郵件系統;
示例項目:http://git.oschina.net/shuaibai123/thinkphp-bjyadmin
一:引入phpmail
從示例項目中復制兩個文件到自己的項目;
/ThinkPHP/Library/Org/Nx/class.phpmailer.php ;
/ThinkPHP/Library/Org/Nx/class.smtp.php ;
不要從官網上下載;因為下載的phpmail直接引入thinkphp是有一些坑會報錯的;
具體報什么錯;年代久遠;當年整合的時候沒有記錄;
二:設置配置項
/Application/Common/Conf/config.php
????'EMAIL_FROM_NAME'????????=>?'',?//?發件人'EMAIL_SMTP'?????????????=>?'',?//?SMTP服務器'EMAIL_USERNAME'?????????=>?'',?//?賬號'EMAIL_PASSWORD'?????????=>?'',?//?密碼如果使用163郵箱;此處有一個坑;首先是要開啟smtp;
開啟過程中是要設置一個授權密碼;開啟完成;又一個坑也就同步挖好了;
配置項中的EMAIL_PASSWORD 指的不是163郵箱的登錄密碼;而是授權密碼;
否則SMTP connect() failed;
三:發送郵件
/***?發送郵件*?@param??string?$address?需要發送的郵箱地址?發送給多個地址需要寫成數組形式*?@param??string?$subject?標題*?@param??string?$content?內容*?@return?boolean???????是否成功*/ function?send_email($address,$subject,$content){$email_smtp=C('EMAIL_SMTP');$email_username=C('EMAIL_USERNAME');$email_password=C('EMAIL_PASSWORD');$email_from_name=C('EMAIL_FROM_NAME');if(empty($email_smtp)?||?empty($email_username)?||?empty($email_password)?||?empty($email_from_name)){return?array("error"=>1,"message"=>'郵箱配置不完整');}require?'./ThinkPHP/Library/Org/Nx/class.phpmailer.php';require?'./ThinkPHP/Library/Org/Nx/class.smtp.php';$phpmailer=new?\Phpmailer();//?設置PHPMailer使用SMTP服務器發送Email$phpmailer->IsSMTP();//?設置為html格式$phpmailer->IsHTML(true);//?設置郵件的字符編碼'$phpmailer->CharSet='UTF-8';//?設置SMTP服務器。$phpmailer->Host=$email_smtp;//?設置為"需要驗證"$phpmailer->SMTPAuth=true;//?設置用戶名$phpmailer->Username=$email_username;//?設置密碼$phpmailer->Password=$email_password;//?設置郵件頭的From字段。$phpmailer->From=$email_username;//?設置發件人名字$phpmailer->FromName=$email_from_name;//?添加收件人地址,可以多次使用來添加多個收件人if(is_array($address)){foreach($address?as?$addressv){$phpmailer->AddAddress($addressv);}}else{$phpmailer->AddAddress($address);}//?設置郵件標題$phpmailer->Subject=$subject;//?設置郵件正文$phpmailer->Body=$content;//?發送郵件。if(!$phpmailer->Send())?{$phpmailererror=$phpmailer->ErrorInfo;return?array("error"=>1,"message"=>$phpmailererror);}else{return?array("error"=>0);} }?
發送郵件調用函數;
send_email('baijunyao@baijunyao','郵件標題','郵件內容'); //?如果群發郵件?則傳入數組即可 $emails=array('b1@baijunyao.com','b2@baijunyao.com'); send_email($emails,'郵件標題','郵件內容');?
本文為白俊遙原創文章,轉載無需和我聯系,但請注明來自白俊遙博客http://baijunyao.com ? ? ? ? ? ? ? ? ? ? ? ?
轉載于:https://blog.51cto.com/shuaibai123/1784209
總結
以上是生活随笔為你收集整理的thinkphp集成系列之phpmailer批量发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU5709 : Claris Lov
- 下一篇: 面试方法