166 lines
3.7 KiB
PHP
166 lines
3.7 KiB
PHP
<?php
|
|
namespace Mail;
|
|
|
|
use Helpers\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 = $this->config->smtp->port;
|
|
$this->conf->ssl = $this->config->smtp->ssl;
|
|
$this->conf->auth = $this->config->smtp->auth;
|
|
$this->conf->name = $this->config->smtp->name;
|
|
}
|
|
|
|
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 setDefaultTo()
|
|
{
|
|
$this->mail->addTo($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;
|
|
if(isset($row['type']))
|
|
{
|
|
$this->type = $row['type'];
|
|
}else{
|
|
$this->type = "text";
|
|
}
|
|
|
|
}//加载模板
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
} |