创建了新的邮件类,方便操作,并且添加了事件功能

This commit is contained in:
Li Jianxuan 2013-08-22 08:08:45 +00:00
parent 87f2aaa67d
commit 644baf0a20
4 changed files with 237 additions and 0 deletions

View File

@ -0,0 +1,10 @@
<?php
namespace Mail\Event;
interface MailEvent
{
public function preSend(\Zend_EventManager_Event $e);
public function mailSended(\Zend_EventManager_Event $e);
}

View File

@ -0,0 +1,26 @@
<?php
namespace Mail\Listener;
use Mail\Operation\MailOperate;
class MailListener implements \Zend_EventManager_ListenerAggregate
{
private $event;
function __construct()
{
$this->event = new \Zend_EventManager_EventManager();
}
public function attach(\Zend_EventManager_EventCollection $events)
{
$Events = new MailOperate();
$events->attach('mail.presend', array($Events, 'preSend'), 100);
$events->attach('mail.sended', array($Events, 'mailSended'), 100);
}
public function detach(\Zend_EventManager_EventCollection $events)
{
}
}

View File

@ -0,0 +1,157 @@
<?php
namespace Mail;
use helper\dbh;
class Mail
{
private $db; //传入PDO对象.
private $conf; //邮件设置
private $config; //站点设置
protected $events = NULL;
private $tbl_mailTemplate = "emailtext";
public $mail; //邮件
public $subject;
public $body;
public $type;
function __construct($db = NULL,$mail = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
$this->conf = new \stdClass();
$this->loadconf();
$this->smtp();
if(empty($mail))
{
$this->mail = new \Zend_Mail('UTF-8');
}else{
$this->mail = $mail;
}
}
private function loadconf()
{
$this->conf->host = $this->config->smtp->host;
$this->conf->username = $this->config->smtp->username;
$this->conf->password = $this->config->smtp->password;
$this->conf->port = 465;
$this->conf->ssl = $this->config->smtp->ssl;
$this->conf->auth = $this->config->smtp->auth;
$this->conf->name = $this->config->title->site;
}
private function smtp()
{
$mail_config = array(
'ssl' => $this->conf->ssl,
'port' => $this->conf->port,
'auth' => $this->conf->auth,
'username' => $this->conf->username,
'password' => $this->conf->password
);
$transport = new \Zend_Mail_Transport_Smtp($this->conf->host, $mail_config);
\Zend_Mail::setDefaultTransport($transport);
}
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;
}
//设置默认发件人
public function setDefaultForm()
{
$this->mail->setFrom($this->conf->username,$this->conf->name);
}
//加载模板
public function loadTemplate($id,$data){
if(is_numeric($id))
{
$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($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'];
}//加载模板
public function addTo($email,$name)
{
$this->mail->addTo($email,$name);
}
//使用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->setDefaultForm();
}else{
$this->mail->setFrom($from['email'],$from['name']);
}
$this->mail->setSubject($this->subject);
@$this->mail->send();
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace Mail\Operation;
use Helpers\View as view;
use Helpers\dbh;
//事件中存在的操作
class MailOperate implements \Mail\Event\MailEvent
{
private $db; //传入PDO对象误
private $config; //全局配置
public $tbl_maillog = ""; //邮件日志表
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 preSend(\Zend_EventManager_Event $e)
{
$email = $e->getParam('email');
return true;
}
public function mailSended(\Zend_EventManager_Event $e)
{
$email = $e->getParam('email');
$time = date("Y-m-d H:i:s");
$title = $e->getParam('title');
$content = $e->getParam('content');
return true;
}
}