From 644baf0a20b0851148abb4fcc2a11c7066be4994 Mon Sep 17 00:00:00 2001 From: Li Jianxuan Date: Thu, 22 Aug 2013 08:08:45 +0000 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E4=BA=86=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E9=82=AE=E4=BB=B6=E7=B1=BB=EF=BC=8C=E6=96=B9=E4=BE=BF=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=EF=BC=8C=E5=B9=B6=E4=B8=94=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/module/Mail/Event/MailEvent.php | 10 ++ .../module/Mail/Listener/MailListener.php | 26 +++ application/module/Mail/Mail.php | 157 ++++++++++++++++++ .../module/Mail/Operation/MailOperate.php | 44 +++++ 4 files changed, 237 insertions(+) create mode 100644 application/module/Mail/Event/MailEvent.php create mode 100644 application/module/Mail/Listener/MailListener.php create mode 100644 application/module/Mail/Mail.php create mode 100644 application/module/Mail/Operation/MailOperate.php diff --git a/application/module/Mail/Event/MailEvent.php b/application/module/Mail/Event/MailEvent.php new file mode 100644 index 00000000..ca0358e4 --- /dev/null +++ b/application/module/Mail/Event/MailEvent.php @@ -0,0 +1,10 @@ +event = new \Zend_EventManager_EventManager(); + } + + public function attach(\Zend_EventManager_EventCollection $events) + { + $Events = new MailOperate(); + $events->attach('mail.presend', array($Events, 'preSend'), 100); + $events->attach('mail.sended', array($Events, 'mailSended'), 100); + } + + public function detach(\Zend_EventManager_EventCollection $events) + { + + } + +} \ No newline at end of file diff --git a/application/module/Mail/Mail.php b/application/module/Mail/Mail.php new file mode 100644 index 00000000..b6c0cecc --- /dev/null +++ b/application/module/Mail/Mail.php @@ -0,0 +1,157 @@ +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 = 465; + $this->conf->ssl = $this->config->smtp->ssl; + $this->conf->auth = $this->config->smtp->auth; + + $this->conf->name = $this->config->title->site; + } + + 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 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; + $this->type = $row['type']; + + }//加载模板 + + 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(); + + } + + +} \ No newline at end of file diff --git a/application/module/Mail/Operation/MailOperate.php b/application/module/Mail/Operation/MailOperate.php new file mode 100644 index 00000000..7cf32c43 --- /dev/null +++ b/application/module/Mail/Operation/MailOperate.php @@ -0,0 +1,44 @@ +db = \Zend_Registry::get('db'); + }else{ + $this->db = $db; + } + + $this->config = \Zend_Registry::get('config'); + } + + public function preSend(\Zend_EventManager_Event $e) + { + $email = $e->getParam('email'); + + return true; + } + + public function mailSended(\Zend_EventManager_Event $e) + { + $email = $e->getParam('email'); + $time = date("Y-m-d H:i:s"); + $title = $e->getParam('title'); + $content = $e->getParam('content'); + + return true; + } + +} \ No newline at end of file