Пример #1
0
 /**
  * Send email to all customers according provided template id
  *
  * @access	public
  * @author	John.Meng(孟远螓) arzen1013@gmail.com 2009-4-4
  * @param	int	$id 	template id
  * @return	void
  */
 function sendMail($id)
 {
     $this->mail_msg = array();
     $mail_template = $this->read(null, $id);
     $mail_data = $mail_template['MailTemplate'];
     //-- import phpmailer
     App::import("Vendor", "PHPMailer", array('file' => 'PHPMailer' . DS . 'class.phpmailer.php'));
     //-- get all mail servers
     App::import('Model', 'Webmailler.MailServer');
     $mail_server_obj = new MailServer();
     $servers_list = $mail_server_obj->find('all');
     $servers = Set::extract($servers_list, '{n}.MailServer');
     //--- get all customers by page
     App::import('Model', 'Webmailler.MailCustomer');
     $mail_customer_obj = new MailCustomer();
     $limit = count($servers);
     $total = $mail_customer_obj->find('count');
     $pages = ceil($total / $limit);
     for ($page = 1; $page <= $pages; $page++) {
         $conditions = array('fields' => array('MailCustomer.nickname', 'MailCustomer.gender', 'MailCustomer.email'), 'limit' => $limit, 'page' => $page, 'conditions' => array('MailCustomer.mail_customer_category_id' => explode($this->group_splitor, $mail_data['to'])));
         $customers_list = $mail_customer_obj->find('all', $conditions);
         $customers = Set::extract($customers_list, '{n}.MailCustomer');
         if (is_array($customers)) {
             foreach ($customers as $key => $customer) {
                 $server = $servers[$key];
                 $mail = new PHPMailer();
                 if ($server['ssl']) {
                     $mail->SMTPSecure = "ssl";
                 }
                 $mail->IsSMTP();
                 $mail->SMTPAuth = true;
                 $mail->Host = $server['host'];
                 if ($server['port'] > 0) {
                     $mail->Port = $server['port'];
                 }
                 $mail->Username = $server['account'];
                 $mail->Password = $server['passwd'];
                 $mail->AddReplyTo($server['account'], "JohnMeng");
                 $mail->From = $server['account'];
                 $mail->FromName = $mail_data["from"];
                 $mail->AddReplyTo($mail_data["from_name"], $mail_data["from"]);
                 $mail->Subject = $mail_data['subject'];
                 $contents = $this->_parseMailContent($mail_data['content'], $customer);
                 if ($mail_data['plain_text']) {
                     $mail->Body = strip_tags($contents);
                 } else {
                     $contents = $this->_parseEmbedMedia($contents, $mail);
                     $mail->MsgHTML($contents);
                     $mail->AltBody = strip_tags($contents);
                 }
                 $mail->AddAddress($customer['email'], $customer['nickname']);
                 //-- add attachments
                 if ($mail_data['attachments']) {
                     $attachmens = explode($this->attachments_splitor, $mail_data['attachments']);
                     foreach ($attachmens as $attach) {
                         $mail->AddAttachment($attach);
                     }
                 }
                 if (!$mail->Send()) {
                     $this->mail_msg[] = sprintf(__("Send to %s error: %s ", true), $customer['email'], $mail->ErrorInfo);
                 } else {
                     $this->mail_msg[] = sprintf(__("Send to %s done ", true), $customer['email']);
                 }
             }
         }
     }
     return $this->mail_msg;
 }
Пример #2
0
 /**
  * Send email to all customers according provided template id
  *
  * @access	public
  * @author	John.Meng(孟远螓) arzen1013@gmail.com 2009-4-4
  * @param	int	$id 	template id
  * @return	void
  */
 function sendMail($id)
 {
     $this->mail_msg = array();
     $mail_template = $this->read(null, $id);
     $mail_data = $mail_template['MailTemplate'];
     //-- import phpmailer
     App::import("Vendor", "PHPMailer", array('file' => 'PHPMailer' . DS . 'class.phpmailer.php'));
     //-- get all mail servers
     App::import('Model', 'Webmailler.MailServer');
     $mail_server_obj = new MailServer();
     $servers_list = $mail_server_obj->find('all');
     $servers = Set::extract($servers_list, '{n}.MailServer');
     //--- get all customers by page
     App::import('Model', 'Webmailler.MailCustomer');
     $mail_customer_obj = new MailCustomer();
     //--- log
     App::import('Model', 'Webmailler.MailLog');
     $mail_log_obj = new MailLog();
     $limit = count($servers);
     $total = $mail_customer_obj->find('count', array('conditions' => array('MailCustomer.mail_customer_category_id' => explode($this->group_splitor, $mail_data['to']))));
     $pages = ceil($total / $limit);
     $total = $success = $fail = 0;
     $charset = "UTF-8";
     if (Configure::read('Config.language') == "zh-CN") {
         $charset = "GBK";
     }
     for ($page = 1; $page <= $pages; $page++) {
         $conditions = array('fields' => array('MailCustomer.nickname', 'MailCustomer.gender', 'MailCustomer.email'), 'limit' => $limit, 'page' => $page, 'conditions' => array('MailCustomer.mail_customer_category_id' => explode($this->group_splitor, $mail_data['to'])));
         $customers_list = $mail_customer_obj->find('all', $conditions);
         $customers = Set::extract($customers_list, '{n}.MailCustomer');
         if (is_array($customers)) {
             foreach ($customers as $key => $customer) {
                 $server = $servers[$key];
                 $mail = new PHPMailer();
                 $mail->CharSet = $charset;
                 if ($server['ssl']) {
                     $mail->SMTPSecure = "ssl";
                 }
                 $mail->IsSMTP();
                 $mail->SMTPAuth = true;
                 $mail->Host = $server['host'];
                 if ($server['port'] > 0) {
                     $mail->Port = $server['port'];
                 }
                 $mail->Username = $server['account'];
                 $mail->Password = $server['passwd'];
                 //					$mail->AddReplyTo($server['account'],"JohnMeng");
                 $mail->From = $server['account'];
                 $mail->FromName = $mail_data["from"];
                 $mail->AddReplyTo($mail_data["from_name"], $mail_data["from"]);
                 $mail->Subject = $mail_data['subject'];
                 $contents = $this->_parseMailContent($mail_data['content'], $customer);
                 if ($charset != "UTF-8") {
                     $mail->Subject = $this->_utf8ToGBK($mail->Subject);
                     $contents = $this->_utf8ToGBK($contents);
                 }
                 if ($mail_data['plain_text']) {
                     $mail->Body = strip_tags($contents);
                 } else {
                     $contents = $this->_parseEmbedMedia($contents, $mail);
                     $mail->MsgHTML($contents);
                     $mail->AltBody = strip_tags($contents);
                 }
                 $mail->AddAddress($customer['email'], $customer['nickname']);
                 //-- add attachments
                 if ($mail_data['attachments']) {
                     $attachmens = explode($this->attachments_splitor, $mail_data['attachments']);
                     foreach ($attachmens as $attach) {
                         $mail->AddAttachment($attach);
                     }
                 }
                 $result = $mail->Send();
                 if (!$result) {
                     printf(__("Send to %s error: %s ", true) . "<br/>", $customer['email'], $mail->ErrorInfo);
                     $fail++;
                 } else {
                     printf(__("Send to %s done ", true) . "<br/>", $customer['email']);
                     $success++;
                 }
             }
         }
         $total++;
     }
     $log = sprintf(__("Sent Total:%s Success:%s Fail:%s ", true) . "\n", $total, $success, $fail);
     $mail_log_obj->log($mail_data['subject'] . "[{$log}]");
     echo nl2br($log);
     return $this->mail_msg;
 }