2013-09-26 10:28:55 +00:00
|
|
|
<?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;
|
|
|
|
|
2018-02-09 07:38:18 +00:00
|
|
|
function __construct($db = NULL, $mail = NULL){
|
2013-09-26 10:28:55 +00:00
|
|
|
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;
|
2018-07-20 07:08:44 +00:00
|
|
|
$this->conf->port = $this->config->smtp->port;
|
2013-09-26 10:28:55 +00:00
|
|
|
$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,
|
2018-07-20 05:05:41 +00:00
|
|
|
'port' => $this->conf->port,
|
2013-09-26 10:28:55 +00:00
|
|
|
'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;
|
2013-12-26 13:33:29 +00:00
|
|
|
if(isset($row['type']))
|
|
|
|
{
|
|
|
|
$this->type = $row['type'];
|
|
|
|
}else{
|
|
|
|
$this->type = "text";
|
|
|
|
}
|
2013-09-26 10:28:55 +00:00
|
|
|
|
|
|
|
}//加载模板
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-07-20 07:21:23 +00:00
|
|
|
$this->mail->send();
|
2013-09-26 10:28:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|