add function of send email in backend

This commit is contained in:
Jianxuan Li 2014-12-25 00:38:23 +08:00
parent af4200c386
commit 0c31626c84
4 changed files with 96 additions and 13 deletions

View File

@ -20,5 +20,40 @@ class Tools {
return true; return true;
} }
/**
* @param $cmd
*/
public function execBackend($cmd)
{
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start cmd /c ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
return;
}
/**
* 返回的汉语文字信息在windows中是GB2312编码需要手动改成UTF8
* iconv("GB2312","UTF-8",$read);
* @param $cmd
* @return array
*/
public function execFront($cmd)
{
$response = array();
$handle = popen("$cmd 2>&1", 'r');
$read = '';
while ($read = fread($handle, 20096)) {
$response[] = trim($read);
}
pclose($handle);
flush();
return $response;
}
} }

View File

@ -8,7 +8,6 @@
namespace Westdc\Mail; namespace Westdc\Mail;
use Zend\Mime\Mime;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface; use Zend\ServiceManager\ServiceManagerAwareInterface;
use Westdc\EventModel\AbstractEventManager; use Westdc\EventModel\AbstractEventManager;
@ -16,6 +15,7 @@ use Westdc\Service\ServiceManager as WestdcServiceManager;
use Zend\Mail\Message; use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions; use Zend\Mail\Transport\SmtpOptions;
use Zend\Mime\Mime;
use Zend\Mime\Message as MimeMessage; use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart; use Zend\Mime\Part as MimePart;
@ -23,8 +23,6 @@ class Mail extends AbstractEventManager implements ServiceManagerAwareInterface{
protected $serviceManager; protected $serviceManager;
private $db;
public $mail; public $mail;
public $config; public $config;
public $subject; public $subject;
@ -50,9 +48,6 @@ class Mail extends AbstractEventManager implements ServiceManagerAwareInterface{
$this->serviceManager = $serviceManager->getServiceManager(); $this->serviceManager = $serviceManager->getServiceManager();
} }
$dbService = $this->serviceManager->get('Db');
$this->db = $dbService->getPdo();
$this->loadConfigure(); $this->loadConfigure();
$this->smtp(); $this->smtp();
$this->buildMailMessage(); $this->buildMailMessage();
@ -134,10 +129,9 @@ class Mail extends AbstractEventManager implements ServiceManagerAwareInterface{
$this->from = $from; $this->from = $from;
} }
//使用loadTemplate 的结果发送邮件
//在此之前需要使用 $this->mail->addTo()添加收件人
public function send($from = NULL){
public function preSend($from = NULL)
{
if(empty($this->subject) || empty($this->body)) if(empty($this->subject) || empty($this->body))
{ {
return "邮件信息不完整"; return "邮件信息不完整";
@ -169,6 +163,16 @@ class Mail extends AbstractEventManager implements ServiceManagerAwareInterface{
$this->mail->setSubject($this->subject); $this->mail->setSubject($this->subject);
return true;
}
//使用loadTemplate 的结果发送邮件
//在此之前需要使用 $this->mail->addTo()添加收件人
public function send($from = NULL){
if(!$status = $this->preSend($from))
return $status;
try { try {
$this->transport->send($this->mail); $this->transport->send($this->mail);
return true; return true;

47
Westdc/Mail/Sender.php Normal file
View File

@ -0,0 +1,47 @@
<?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;
class Sender implements ServiceManagerAwareInterface{
protected $serviceManager;
public function setServiceManager(ServiceManager $serviceManager)
{
$this->serviceManager = $serviceManager;
return $this;
}
public function backend($options)
{
$cmd = "php ".CURRENT_BOOTSTRAP_SCRIPT;
$cmd .= ' mail send';
$cmd .= ' --email="'.$options['email'].'"';
$cmd .= ' --name="'.$options['name'].'"';
$cmd .= ' --template="'.$options['template'].'"';
if(isset($options['data']))
{
$data = json_encode($options['data']);
$cmd .= ' --data=\''.$data.'\'';
}
$tools = $this->serviceManager->get('Tools');
//$tools->execBackend($cmd);
//echo $cmd;
//var_dump($tools->execFront($cmd));
//exit();
return true;
}
}

View File

@ -25,7 +25,6 @@ class Template implements ServiceManagerAwareInterface
{ {
$dbService = $this->serviceManager->get('Db'); $dbService = $this->serviceManager->get('Db');
$this->db = $dbService->getPdo(); $this->db = $dbService->getPdo();
} }
public function fetchAll() public function fetchAll()
@ -94,11 +93,10 @@ class Template implements ServiceManagerAwareInterface
} }
$rs=$this->db->query($sql); $rs=$this->db->query($sql);
return $rs->fetch(); return $rs->fetch();
} }
<<<<<<< HEAD
=======
/** /**
* @param $key * @param $key
* @param $data * @param $data
@ -137,6 +135,5 @@ class Template implements ServiceManagerAwareInterface
]; ];
}//function load }//function load
>>>>>>> 91044298c8430244e78f4e502aa5d9d0ce84a3f8
} }