123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace KIF\Mail;
- use KIF\Core\Config;
- use KIF\Mail\PHPMailer;
- use KIF\Verify;
- class Mail
- {
- protected $objPHPMailer;
-
-
- protected $config;
- public function __construct(array $options = null)
- {
- $this->config = Config::getInstance()->get('smtp');
- $this->objPHPMailer = new PHPMailer();
- $this->objPHPMailer->IsSMTP();
- $this->objPHPMailer->SMTPAuth = true;
- if(isset($options['HOSTNAME'])){
- $this->objPHPMailer->Host = $options['HOSTNAME'];
- }else{
- $this->objPHPMailer->Host = $this->config['host'];
- }
- if(isset($options['PORT'])){
- $this->objPHPMailer->Port = $options['PORT'];
- }else{
- $this->objPHPMailer->Port = $this->config['port'];
- }
- if(isset($options['TIMEOUT'])){
- $this->objPHPMailer->Timeout = $options['TIMEOUT'];
- }else{
- $this->objPHPMailer->Timeout = $this->config['timeout'];
- }
- if(isset($options['USERNAME'])){
- $this->objPHPMailer->Username = $options['USERNAME'];
- }else{
- $this->objPHPMailer->Username = $this->config['username'];
- }
- if(isset($options['PASSWORD'])){
- $this->objPHPMailer->Password = $options['PASSWORD'];
- }else{
- $this->objPHPMailer->Password = $this->config['password'];
- }
- if(isset($options['CHARSET'])){
- $this->objPHPMailer->CharSet = $options['CHARSET'];
- }else{
- $this->objPHPMailer->CharSet = $this->config['charset'];
- }
- if(isset($options['Encoding'])){
- $this->objPHPMailer->Encoding = $options['Encoding'];
- }else{
- $this->objPHPMailer->Encoding = "base64";
- }
- }
-
- public function send($info, $retry_times = 0, $retry_frequency = 1)
- {
- $from = isset($info['from'])?$info['from']:'';
- if($from){
- if(!Verify::email($from)){
- echo '邮箱格式不真确';
- exit;
- }
- $this->objPHPMailer->From = $from;
- }else{
- $this->objPHPMailer->From = $this->config['from'];
- }
- $fromname = isset($info['fromname'])?$info['fromname']:'';
- if($fromname){
- $this->objPHPMailer->FromName = $fromname;
- }else{
- $this->objPHPMailer->FromName = $this->config['fromname'];
- }
- if(!$info["address"]){
- echo '收件人邮箱不能为空';
- exit;
- }
- if (is_array($info["address"])){
- foreach($info["address"] as $address){
- if(!$address['mail']){
- echo '收件人邮箱不能为空';
- exit;
- }
- $this->objPHPMailer->AddAddress($address["mail"], $address["name"]);
- }
- }else{
- $address = explode(',', $info["address"]);
- foreach($address as $add){
- if(!$add) continue;
- $this->objPHPMailer->AddAddress(trim($add));
- }
- }
- if (is_array($info["ccaddress"])){
- foreach($info["ccaddress"] as $address){
- if(!$address["mail"]){
- echo '抄送收件人邮箱不能为空';
- exit;
- }
- $this->objPHPMailer->AddCC($address["mail"], $address["name"]);
- }
- }else{
- $address = explode(',', $info["ccaddress"]);
- foreach($address as $add){
- if(!$add) continue;
- $this->objPHPMailer->AddCC(trim($add));
- }
- }
- if (is_array($info["bccaddress"])){
- foreach($info["bccaddress"] as $address){
- if(!$address["mail"]){
- echo '密送收件人邮箱不能为空';
- exit;
- }
- $this->objPHPMailer->AddBCC($address["mail"], $address["name"]);
- }
- }else{
- $address = explode(',', $info["bccaddress"]);
- foreach($address as $add){
- if(!$add) continue;
- $this->objPHPMailer->AddBCC(trim($add));
- }
- }
- $attachment = isset($info["attachment"])?$info["attachment"]:'';
- if($attachment)
- {
- $attachmenta = explode(',', $attachment);
- foreach($attachmenta as $tmpV)
- {
- if(!$tmpV) continue;
- $this->objPHPMailer->AddAttachment($tmpV);
- }
- }
- $ishtml = isset($info["ishtml"])?$info["ishtml"]:'';
- if($ishtml){
- $this->objPHPMailer->IsHTML($ishtml);
- }else{
- $this->objPHPMailer->IsHTML(true);
- }
- if(!$info["subject"]){
- echo '主题不能为空!';
- exit;
- }
- $this->objPHPMailer->Subject = $info["subject"];
- $body = isset($info["body"])?$info["body"]:'';
- if(!$body){
- $this->objPHPMailer->Body = $info["subject"];
- }else{
- $this->objPHPMailer->Body = $body;
- }
- return $this->objPHPMailer->Send();
- while (true) {
- $tmpSendResult = $this->objPHPMailer->Send();
- if ($tmpSendResult) {
- return $tmpSendResult;
- }
- $retry_times--;
- if ($retry_times < 0) {
- break;
- }
- sleep($retry_frequency);
- }
- }
- }
|