更新邮件模板类,将set簇方法和send() 进行了整合
This commit is contained in:
parent
0a2f5cc4d1
commit
4d78dbd2ff
|
@ -0,0 +1,164 @@
|
|||
<?php
|
||||
namespace helper;
|
||||
class email
|
||||
{
|
||||
private $db;
|
||||
private $config;
|
||||
|
||||
public $mail; //Zend_Mail Object
|
||||
private $charset = "utf-8";
|
||||
private $name = "寒区旱区科学数据中心";
|
||||
|
||||
private $tbl_mailTemplate = "emailtext";
|
||||
|
||||
protected $events = NULL;
|
||||
|
||||
public $subject;
|
||||
public $body;
|
||||
public $type;
|
||||
|
||||
function __construct($db = NULL,Zend_Mail_Transport_Smtp $transport = NULL)
|
||||
{
|
||||
if(empty($db))
|
||||
{
|
||||
$this->db = Zend_Registry::get('db');
|
||||
}else{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
$this->config = Zend_Registry::get('config');
|
||||
|
||||
$smtp = $this->config->smtp;
|
||||
|
||||
$mail_config = array(
|
||||
'ssl' => $smtp->ssl,
|
||||
'auth'=> $smtp->auth,
|
||||
'username'=> $smtp->username,
|
||||
'password'=> $smtp->password
|
||||
);
|
||||
|
||||
$transport = new Zend_Mail_Transport_Smtp($smtp->host, $mail_config);
|
||||
|
||||
Zend_Mail::setDefaultTransport($transport);
|
||||
|
||||
if(empty($mail))
|
||||
{
|
||||
$this->mail = new Zend_Mail();
|
||||
}else{
|
||||
$this->mail = $mail;
|
||||
}
|
||||
}
|
||||
|
||||
public function events(Zend_EventManager_EventCollection $events = NULL)
|
||||
{
|
||||
if ($events !== NULL) {
|
||||
$this->events = $events;
|
||||
} elseif ($this->events === NULL) {
|
||||
$this->events = new Zend_EventManager_EventManager(__CLASS__);
|
||||
}
|
||||
return $this->events;
|
||||
}
|
||||
|
||||
//默认使用westdc作为发件人
|
||||
public function withWestdc()
|
||||
{
|
||||
$this->mail->setFrom($this->config->supportemail,$this->name);
|
||||
}
|
||||
|
||||
//加载模板
|
||||
public function loadTemplate($id,$data){
|
||||
if(is_numeric($this->tmpid))
|
||||
{
|
||||
$sql = "SELECT * FROM {$this->tbl_mailTemplate} WHERE id='".$id."'";
|
||||
}else{
|
||||
$sql = "SELECT * FROM {$this->tbl_mailTemplate} WHERE \"template\"='".$id."'";
|
||||
}
|
||||
|
||||
$rs = $this->db->query($sql);
|
||||
|
||||
$row = $rs->fetch();
|
||||
|
||||
$subject = $row['subject'];
|
||||
$body = $row['body'];
|
||||
|
||||
if(count($data) > 0)
|
||||
{
|
||||
$patterns = array();
|
||||
$replacements = array();
|
||||
foreach($this->data as $k=>$v)
|
||||
{
|
||||
$patterns[]='/{'.$k.'}/i';
|
||||
$replacements[]=$v;
|
||||
}
|
||||
ksort($patterns);
|
||||
ksort($replacements);
|
||||
$body = preg_replace($patterns, $replacements, $body);
|
||||
$subject = preg_replace($patterns, $replacements, $subject);
|
||||
}//count($this->data)
|
||||
|
||||
$this->subject = $subject;
|
||||
$this->body = $body;
|
||||
$this->type = $row['type'];
|
||||
|
||||
}//加载模板
|
||||
|
||||
//使用loadTemplate 的结果发送邮件
|
||||
//在此之前需要使用 $this->mail->addTo()添加收件人
|
||||
public function send($from = NULL){
|
||||
if(empty($this->subject) || empty($this->body))
|
||||
{
|
||||
return "邮件信息不完整";
|
||||
}
|
||||
|
||||
if($this->type == 'html')
|
||||
{
|
||||
$this->mail->setBodyHtml($this->body);
|
||||
}else{
|
||||
$this->mail->setBodyText($this->body);
|
||||
}
|
||||
|
||||
if(empty($from))
|
||||
{
|
||||
$this->withWestdc();
|
||||
}else{
|
||||
$this->mail->setFrom($from['email'],$from['name']);
|
||||
}
|
||||
|
||||
$this->mail->setSubject($this->subject);
|
||||
|
||||
$this->mail->send();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class EmailListener implements Zend_EventManager_ListenerAggregate
|
||||
{
|
||||
private $db;
|
||||
|
||||
function __construct($db = NULL)
|
||||
{
|
||||
if(empty($db))
|
||||
{
|
||||
$this->db = Zend_Registry::get('db');
|
||||
}else{
|
||||
$this->db = $db;
|
||||
}
|
||||
$this->config = Zend_Registry::get('config');
|
||||
}
|
||||
|
||||
public function attach(Zend_EventManager_EventCollection $events)
|
||||
{
|
||||
$events->attach('send', array($this, 'send'), 100);
|
||||
}
|
||||
|
||||
public function detach(Zend_EventManager_EventCollection $events)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function send()
|
||||
{
|
||||
//邮件发送事件
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue