westdc-core/Westdc/Mail/Mail.php

192 lines
4.7 KiB
PHP
Raw Normal View History

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;
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;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;
2014-09-19 09:09:53 +00:00
class Mail extends AbstractEventManager implements ServiceManagerAwareInterface{
2014-09-19 09:09:53 +00:00
protected $serviceManager;
public $mail;
public $config;
public $subject;
public $body;
public $type;
public $transport;
public $from;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
$this->init();
return $this;
}
private function init()
{
if(!$this->serviceManager instanceof ServiceManager)
{
$serviceManager = WestdcServiceManager::getInstance();
$this->serviceManager = $serviceManager->getServiceManager();
}
$this->loadConfigure();
$this->smtp();
$this->buildMailMessage();
}
//单独调用Mail类的时候需要先执行委托函数
public function __invoke()
2014-09-19 09:09:53 +00:00
{
$this->init();
}
2014-09-19 09:09:53 +00:00
public function loadConfigure()
{
$configService = $this->serviceManager->get('ConfigService');
$this->config = $configService->get('email.ini');
2014-09-19 09:09:53 +00:00
}
public function smtp()
2014-09-19 09:09:53 +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
));
$this->transport->setOptions($options);
2014-09-19 09:09:53 +00:00
}
public function buildMailMessage($mail = NULL)
2014-09-19 09:09:53 +00:00
{
if(empty($mail))
{
$this->mail = new Message();
}else{
$this->mail = $mail;
2014-09-19 09:09:53 +00:00
}
$this->mail->setEncoding("UTF-8");
}
//设置默认发件人
public function setDefaultFrom()
{
$this->mail->setFrom($this->config['smtp']['username'],$this->config['smtp']['name']);
2014-09-19 09:09:53 +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'];
}//加载模板
/**
* @param $from
*/
public function setFrom($from)
{
$this->from = $from;
}
/**
* @param null $from
* @return bool
*/
2014-12-24 16:38:23 +00:00
public function preSend($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);
}
if(empty($from) && empty($this->from))
{
$this->setDefaultFrom();
}else{
if(!empty($this->from))
$this->mail->setFrom($this->from['email'],$this->from['name']);
if(!empty($from))
$this->mail->setFrom($from['email'],$from['name']);
}
$this->mail->setSubject($this->subject);
2014-12-24 16:38:23 +00:00
return true;
}
//使用loadTemplate 的结果发送邮件
//在此之前需要使用 $this->mail->addTo()添加收件人
/**
* @param null $from
*/
2014-12-24 16:38:23 +00:00
public function send($from = NULL){
if(!$status = $this->preSend($from))
return $status;
try {
$this->transport->send($this->mail);
return true;
}catch(\Exception $e)
{
throw new \RuntimeException($e->getMessage());
}
}
2014-09-19 09:09:53 +00:00
}