diff --git a/trunk/module/action/model.php b/trunk/module/action/model.php index c70fda88be..52d7cf7eaa 100644 --- a/trunk/module/action/model.php +++ b/trunk/module/action/model.php @@ -57,6 +57,14 @@ class actionModel extends model return $actions; } + /* 获得action信息。*/ + public function getById($actionID) + { + $action = $this->dao->findById((int)$actionID)->from(TABLE_ACTION)->fetch(); + $action->date = date('Y-m-d H:i:s', $action->date); + return $action; + } + /* 返回某一个action所对应的字段修改记录。*/ public function getHistory($actionID) { diff --git a/trunk/module/bug/control.php b/trunk/module/bug/control.php index 480cd0e315..bb2257e5f9 100644 --- a/trunk/module/bug/control.php +++ b/trunk/module/bug/control.php @@ -132,7 +132,8 @@ class bug extends control { $bugID = $this->bug->create(); if(dao::isError()) die(js::error(dao::getError())); - $this->action->create('bug', $bugID, 'Opened'); + $actionID = $this->action->create('bug', $bugID, 'Opened'); + $this->sendmail($bugID, $actionID); die(js::locate($this->createLink('bug', 'browse', "productID={$this->post->product}&type=byModule¶m={$this->post->module}"), 'parent')); } @@ -196,6 +197,7 @@ class bug extends control if(!empty($files)) $fileAction = "Add Files " . join(',', $files) . "\n" ; $actionID = $this->action->create('bug', $bugID, $action, $fileAction . $this->post->comment); $this->action->logHistory($actionID, $changes); + $this->sendmail($bugID, $actionID); } die(js::locate($this->createLink('bug', 'view', "bugID=$bugID"), 'parent')); } @@ -241,6 +243,7 @@ class bug extends control $this->bug->resolve($bugID); if(dao::isError()) die(js::error(dao::getError())); $actionID = $this->action->create('bug', $bugID, 'Resolved', $this->post->comment); + $this->sendmail($bugID, $actionID); die(js::locate($this->createLink('bug', 'view', "bugID=$bugID"), 'parent')); } @@ -268,7 +271,8 @@ class bug extends control if(dao::isError()) die(js::error(dao::getError())); $this->loadModel('file'); $files = $this->file->saveUpload('files', 'bug', $bugID); - $this->action->create('bug', $bugID, 'Activated', $this->post->comment); + $actionID = $this->action->create('bug', $bugID, 'Activated', $this->post->comment); + $this->sendmail($bugID, $actionID); die(js::locate($this->createLink('bug', 'view', "bugID=$bugID"), 'parent')); } @@ -296,7 +300,8 @@ class bug extends control { $this->bug->close($bugID); if(dao::isError()) die(js::error(dao::getError())); - $this->action->create('bug', $bugID, 'Closed', $this->post->comment); + $actionID = $this->action->create('bug', $bugID, 'Closed', $this->post->comment); + $this->sendmail($bugID, $actionID); die(js::locate($this->createLink('bug', 'view', "bugID=$bugID"), 'parent')); } @@ -321,4 +326,47 @@ class bug extends control $bugs = $this->bug->getUserBugPairs($account); die(html::select('bug', $bugs, '', 'class=select-1')); } + + /* 发送变量。*/ + private function sendmail($bugID, $actionID) + { + /* 设定toList和ccList。*/ + $bug = $this->bug->getByID($bugID); + $toList = $bug->assignedTo; + $ccList = trim($bug->mailto, ','); + if($toList == '') + { + if($ccList == '') return; + if(strpos($ccList, ',') === false) + { + $toList = $ccList; + $ccList = ''; + } + else + { + $commaPos = strpos($ccList, ','); + $toList = substr($ccList, 0, $commaPos); + $ccList = substr($ccList, $commaPos + 1); + } + } + elseif(strtolower($toList) == 'closed') + { + $toList = $bug->resolvedBy; + } + + /* 获得action信息。*/ + $action = $this->action->getById($actionID); + $histories = $this->action->getHistory($actionID); + if(strtolower($action->action) == 'opened') $action->comment = $bug->steps; + + /* 赋值,获得邮件内容。*/ + $this->assign('bug', $bug); + $this->assign('action', $action); + $this->assign('histories', $histories); + $mailContent = $this->fetch($this->moduleName, 'sendmail'); + + /* 发信。*/ + $this->loadModel('mail')->send($toList, 'BUG #' . $bug->id . $this->lang->colon . $bug->title, $mailContent, $ccList); + if($this->mail->isError()) echo js::error($this->mail->getError()); + } } diff --git a/trunk/module/bug/view/sendmail.html.php b/trunk/module/bug/view/sendmail.html.php new file mode 100644 index 0000000000..a841e66e39 --- /dev/null +++ b/trunk/module/bug/view/sendmail.html.php @@ -0,0 +1,34 @@ +. + * + * @copyright Copyright: 2009 Chunsheng Wang + * @author Chunsheng Wang + * @package bug + * @version $Id$ + * @link http://www.zentao.cn + */ +?> + + + + + + + +
+ BUG #id . "=>$bug->assignedTo " . html::a(common::getSysURL() . $this->createLink('bug', 'view', "bugID=$bug->id"), $bug->title);?> +
diff --git a/trunk/module/common/mail.html.php b/trunk/module/common/mail.html.php new file mode 100644 index 0000000000..ad47c05dda --- /dev/null +++ b/trunk/module/common/mail.html.php @@ -0,0 +1,30 @@ +date, $action->action by $action->actor"; ?> +comment) or !empty($histories)):?> +
+id] as $history) + { + if($history->diff != '') + { + echo "CHANGE $history->field, the diff is:
" . nl2br($history->diff) . "
"; + } + else + { + echo "CHANGE $history->field FROM '$history->old' TO '$history->new' .
"; + } + } +} +echo nl2br($action->comment); +?> +
+ + diff --git a/trunk/module/mail/config.php b/trunk/module/mail/config.php index 2196602c30..bd15bf52e1 100644 --- a/trunk/module/mail/config.php +++ b/trunk/module/mail/config.php @@ -1,7 +1,10 @@ mail->trunon = false; + /* 设置发件人地址和名称。*/ -$config->mail->fromAddress = ''; // 发件人地址。 -$config->mail->fromName = ''; // 发件人名称。 +$config->mail->fromAddress = ''; // 发件人地址。 +$config->mail->fromName = ''; // 发件人名称。 /* 设置发信方式,目前支持mail|sendmail|smtp|gmail。*/ $config->mail->mta = 'gmail'; diff --git a/trunk/module/mail/model.php b/trunk/module/mail/model.php index 9a8fe4a689..decca36279 100644 --- a/trunk/module/mail/model.php +++ b/trunk/module/mail/model.php @@ -28,6 +28,7 @@ class mailModel extends model private static $instance; private $mta; private $mtaType; + private $errors = array(); public function __construct() { @@ -41,33 +42,12 @@ class mailModel extends model { if(self::$instance == null) self::$instance = new phpmailer(true); $this->mta = self::$instance; + $this->mta->CharSet = $this->config->encoding; $funcName = "set{$this->config->mail->mta}"; if(!method_exists($this, $funcName)) echo $this->app->error("The MTA {$this->config->mail->mta} not supported now.", __FILE__, __LINE__, $exit = false); $this->$funcName(); } - /* 发送邮件。*/ - public function send($subject, $body, $toList, $ccList) - { - try - { - $this->mta->setFrom($this->config->mail->fromAddress, $this->config->mail->fromName); - $this->setSubject($subject); - $this->setTO($toList); - $this->setCC($ccList); - $this->setBody($body); - $this->mta->send(); - } - catch (phpmailerException $e) - { - echo $e->errorMessage(); - } - catch (Exception $e) - { - echo $e->getMessage(); - } - } - /* SMTP方式。*/ private function setSMTP() { @@ -105,17 +85,57 @@ class mailModel extends model $this->mta->Password = $this->config->mail->gmail->password; } - /* 设置发送地址。*/ - private function setTO($toList) + /* 发送邮件。*/ + public function send($toList, $subject, $body = '', $ccList = '') { - foreach($toList as $toName => $toAddress) $this->mta->addAddress($toAddress, $toName); + if(!$this->config->mail->turnon) return; + + /* 获得用户的真实姓名和email列表。*/ + $this->loadModel('user'); + $emails = $this->user->getRealNameAndEmails(str_replace(' ', '', $toList . ',' . $ccList)); + + try + { + $this->mta->setFrom($this->config->mail->fromAddress, $this->config->mail->fromName); + $this->setSubject($subject); + $this->setTO($toList, $emails); + $this->setCC($ccList, $emails); + $this->setBody($body); + $this->mta->send(); + } + catch (phpmailerException $e) + { + $this->errors[] = trim(strip_tags($e->errorMessage())); + } + catch (Exception $e) + { + $this->errors[] = trim(strip_tags($e->getMessage())); + } + } + + /* 设置发送地址。*/ + private function setTO($toList, $emails) + { + $toList = explode(',', str_replace(' ', '', $toList)); + foreach($toList as $account) + { + if(!isset($emails[$account]) or isset($emails[$account]->sended) or strpos($emails[$account]->email, '@') == false) continue; + $this->mta->addAddress($emails[$account]->email, $emails[$account]->realname); + $emails[$account]->sended = true; + } } /* 设置抄送地址。*/ - private function setCC($ccList) + private function setCC($ccList, $emails) { + $ccList = explode(',', str_replace(' ', '', $ccList)); if(!is_array($ccList)) return; - foreach($ccList as $ccName => $ccAddress) $this->mta->addCC($ccAddress, $ccName); + foreach($ccList as $account) + { + if(!isset($emails[$account]) or isset($emails[$account]->sended) or strpos($emails[$account]->email, '@') == false) continue; + $this->mta->addCC($emails[$account]->email, $emails[$account]->realname); + $emails[$account]->sended = true; + } } /* 设置主题。*/ @@ -127,7 +147,7 @@ class mailModel extends model /* 设置body。*/ private function setBody($body) { - $this->mta->msgHtml($body); + $this->mta->msgHtml("$body"); } /* 清楚地址和附件。*/ @@ -136,4 +156,18 @@ class mailModel extends model $this->mta->clearAddresses(); $this->mta->cearAttachments(); } + + /* 判断是否有错!*/ + public function isError() + { + return !empty($this->errors); + } + + /* 获得错误。*/ + public function getError() + { + $errors = $this->errors; + $this->errors = array(); + return $errors; + } }