2014-09-19 09:09:53 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: Li Jianxuan
|
|
|
|
* Date: 14-9-19
|
|
|
|
* Time: 下午3:43
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Westdc\Mail;
|
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
use Zend\Mime\Mime;
|
|
|
|
use Zend\ServiceManager\ServiceManager;
|
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface;
|
|
|
|
use Westdc\EventModel\AbstractEventManager;
|
|
|
|
use Westdc\Service\ServiceManager as WestdcServiceManager;
|
2014-09-19 09:09:53 +00:00
|
|
|
use Zend\Mail\Message;
|
|
|
|
use Zend\Mail\Transport\Smtp as SmtpTransport;
|
|
|
|
use Zend\Mail\Transport\SmtpOptions;
|
2014-12-23 14:32:23 +00:00
|
|
|
use Zend\Mime\Message as MimeMessage;
|
|
|
|
use Zend\Mime\Part as MimePart;
|
2014-09-19 09:09:53 +00:00
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
class Mail extends AbstractEventManager implements ServiceManagerAwareInterface{
|
2014-09-19 09:09:53 +00:00
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
protected $serviceManager;
|
|
|
|
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
public $mail;
|
|
|
|
public $config;
|
|
|
|
public $subject;
|
|
|
|
public $body;
|
|
|
|
public $type;
|
|
|
|
public $transport;
|
2014-12-24 07:59:42 +00:00
|
|
|
public $from;
|
2014-12-23 14:32:23 +00:00
|
|
|
|
|
|
|
public function setServiceManager(ServiceManager $serviceManager)
|
|
|
|
{
|
|
|
|
$this->serviceManager = $serviceManager;
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function init()
|
|
|
|
{
|
|
|
|
if(!$this->serviceManager instanceof ServiceManager)
|
|
|
|
{
|
|
|
|
$serviceManager = new WestdcServiceManager();
|
|
|
|
$this->serviceManager = $serviceManager->getServiceManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
$dbService = $this->serviceManager->get('Db');
|
|
|
|
$this->db = $dbService->getPdo();
|
|
|
|
|
|
|
|
$this->loadConfigure();
|
|
|
|
$this->smtp();
|
|
|
|
$this->buildMailMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
//单独调用Mail类的时候需要先执行委托函数
|
|
|
|
public function __invoke()
|
2014-09-19 09:09:53 +00:00
|
|
|
{
|
2014-12-23 14:32:23 +00:00
|
|
|
$this->init();
|
|
|
|
}
|
2014-09-19 09:09:53 +00:00
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
public function loadConfigure()
|
|
|
|
{
|
|
|
|
$configService = $this->serviceManager->get('ConfigService');
|
|
|
|
$this->config = $configService->get('email.ini');
|
2014-09-19 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
public function smtp()
|
2014-09-19 09:09:53 +00:00
|
|
|
{
|
2014-12-23 14:32:23 +00:00
|
|
|
$this->transport = new SmtpTransport();
|
|
|
|
|
|
|
|
$options = new SmtpOptions(array(
|
|
|
|
'name' => $this->config['smtp']['hostname'],
|
|
|
|
'host' => $this->config['smtp']['host'],
|
|
|
|
'port' => $this->config['smtp']['port'], // Notice port change for TLS is 587
|
|
|
|
'connection_class' => $this->config['smtp']['auth'],
|
|
|
|
'connection_config' => array(
|
|
|
|
'username' => $this->config['smtp']['username'],
|
|
|
|
'password' => $this->config['smtp']['password'],
|
|
|
|
'ssl' => $this->config['smtp']['ssl'],
|
|
|
|
),
|
2014-09-19 09:09:53 +00:00
|
|
|
));
|
2014-12-23 14:32:23 +00:00
|
|
|
|
|
|
|
$this->transport->setOptions($options);
|
2014-09-19 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
public function buildMailMessage($mail = NULL)
|
2014-09-19 09:09:53 +00:00
|
|
|
{
|
2014-12-23 14:32:23 +00:00
|
|
|
if(empty($mail))
|
|
|
|
{
|
|
|
|
$this->mail = new Message();
|
|
|
|
}else{
|
|
|
|
$this->mail = $mail;
|
2014-09-19 09:09:53 +00:00
|
|
|
}
|
2014-12-23 14:32:23 +00:00
|
|
|
|
|
|
|
$this->mail->setEncoding("UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
//设置默认发件人
|
2014-12-24 07:59:42 +00:00
|
|
|
public function setDefaultFrom()
|
2014-12-23 14:32:23 +00:00
|
|
|
{
|
|
|
|
$this->mail->setFrom($this->config['smtp']['username'],$this->config['smtp']['name']);
|
2014-09-19 09:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
//添加收件人
|
|
|
|
public function addTo($email,$name)
|
|
|
|
{
|
|
|
|
$this->mail->addTo($email,$name);
|
|
|
|
}
|
|
|
|
|
|
|
|
//加载模板
|
|
|
|
public function loadTemplate($id,$data){
|
|
|
|
|
|
|
|
$mailTemplate = $this->serviceManager->get('Mail/Template');
|
|
|
|
|
|
|
|
$content = $mailTemplate->load($id,$data);
|
|
|
|
|
|
|
|
$this->subject = $content['subject'];
|
|
|
|
$this->body = $content['body'];
|
|
|
|
$this->type = $content['type'];
|
|
|
|
|
|
|
|
}//加载模板
|
|
|
|
|
2014-12-24 07:59:42 +00:00
|
|
|
/**
|
|
|
|
* @param $from
|
|
|
|
*/
|
|
|
|
public function setFrom($from)
|
|
|
|
{
|
|
|
|
$this->from = $from;
|
|
|
|
}
|
|
|
|
|
2014-12-23 14:32:23 +00:00
|
|
|
//使用loadTemplate 的结果发送邮件
|
|
|
|
//在此之前需要使用 $this->mail->addTo()添加收件人
|
|
|
|
public function send($from = NULL){
|
|
|
|
|
|
|
|
if(empty($this->subject) || empty($this->body))
|
|
|
|
{
|
|
|
|
return "邮件信息不完整";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($this->type == 'html')
|
|
|
|
{
|
|
|
|
$bodyPart = new MimeMessage();
|
|
|
|
|
|
|
|
$bodyMessage = new MimePart($this->body);
|
|
|
|
$bodyMessage->type = 'text/html';
|
|
|
|
|
|
|
|
$bodyPart->setParts(array($bodyMessage));
|
|
|
|
|
|
|
|
$this->mail->setBody($bodyPart);
|
|
|
|
}else{
|
|
|
|
$this->mail->setBody($this->body);
|
|
|
|
}
|
|
|
|
|
2014-12-24 07:59:42 +00:00
|
|
|
if(empty($from) && empty($this->from))
|
2014-12-23 14:32:23 +00:00
|
|
|
{
|
2014-12-24 07:59:42 +00:00
|
|
|
$this->setDefaultFrom();
|
2014-12-23 14:32:23 +00:00
|
|
|
}else{
|
2014-12-24 07:59:42 +00:00
|
|
|
if(!empty($this->from))
|
|
|
|
$this->mail->setFrom($this->from['email'],$this->from['name']);
|
|
|
|
if(!empty($from))
|
|
|
|
$this->mail->setFrom($from['email'],$from['name']);
|
2014-12-23 14:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->mail->setSubject($this->subject);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->transport->send($this->mail);
|
|
|
|
return true;
|
|
|
|
}catch(\Exception $e)
|
|
|
|
{
|
|
|
|
throw new \RuntimeException($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-19 09:09:53 +00:00
|
|
|
}
|