From 6467e338234570c6d635dc22aebccd8c3a1c8ddc Mon Sep 17 00:00:00 2001 From: wangchunsheng Date: Tue, 1 Dec 2009 03:29:35 +0000 Subject: [PATCH] + import the mail module. --- config/config.php | 2 +- module/mail/config.php | 25 +++++++++ module/mail/model.php | 124 ++++++++++++++++++++++++++++------------- 3 files changed, 110 insertions(+), 41 deletions(-) create mode 100644 module/mail/config.php diff --git a/config/config.php b/config/config.php index 7c91ef5c4d..5ac1f8008d 100644 --- a/config/config.php +++ b/config/config.php @@ -21,7 +21,7 @@ * @version $Id$ * @link http://www.zentao.cn */ -$config->version = '0.1 alpha'; // °汾ºţ¬ÇÐÎðÐ޸ġ£ +$config->version = '0.2 alpha'; // °汾ºţ¬ÇÐÎðÐ޸ġ£ $config->debug = true; // ÊǷñ´ò¿ªdebug¹¦Äܡ£ $config->webRoot = '/'; // webÍøվµĸùĿ¼¡£ $config->encoding = 'UTF-8'; // ÍøվµıàÂ롣 diff --git a/module/mail/config.php b/module/mail/config.php new file mode 100644 index 0000000000..2196602c30 --- /dev/null +++ b/module/mail/config.php @@ -0,0 +1,25 @@ +mail->fromAddress = ''; // 发件人地址。 +$config->mail->fromName = ''; // 发件人名称。 + +/* 设置发信方式,目前支持mail|sendmail|smtp|gmail。*/ +$config->mail->mta = 'gmail'; + +/* 普通SMTP的配置:*/ +if($config->mail->mta == 'smtp') +{ + $config->mail->smtp->debug = 0; // smtp debug级别,0,1, 2, 数字越大,级别越高。 + $config->mail->smtp->auth = true; // 是否需要验证。 + $config->mail->smtp->host = ''; // smtp主机。 + $config->mail->smtp->port = ''; // 端口号。 + $config->mail->smtp->username = ''; // 登陆用户名,有的smtp需要完整的邮箱地址。 + $config->mail->smtp->password = ''; // 密码。 +} +/* GMAIL的配置。*/ +elseif($config->mail->mta == 'gmail') +{ + $config->mail->gmail->debug = 0; // debug级别,0,1, 2, 数字越大,级别越高。 + $config->mail->gmail->username = ""; // GMAIL username + $config->mail->gmail->password = ""; // GMAIL password +} diff --git a/module/mail/model.php b/module/mail/model.php index 6ae48b66be..9a8fe4a689 100644 --- a/module/mail/model.php +++ b/module/mail/model.php @@ -25,71 +25,115 @@ app->loadClass('phpmailer', $static = true); + $this->setMTA(); } - private function factory($mta, $param = array()) + /* 设置邮件传输代理: MTA。*/ + public function setMTA() { - $funcName = "set$mta"; - $this->mta = new phpmailer(true); - $this->$funcName($param); + if(self::$instance == null) self::$instance = new phpmailer(true); + $this->mta = self::$instance; + $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(); } - private function setSMTP($param) + /* 发送邮件。*/ + 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() { $this->mta->isSMTP(); - $this->mta->Host = $param->host; - $this->mta->SMTPAuth = $param->auth; - $this->mta->Username = $param->username; - $this->mta->Password = $param->password; + $this->mta->SMTPDebug = $this->config->mail->smtp->debug; + $this->mta->Host = $this->config->mail->smtp->host; + $this->mta->SMTPAuth = $this->config->mail->smtp->auth; + $this->mta->Username = $this->config->mail->smtp->username; + $this->mta->Password = $this->config->mail->smtp->password; + if(isset($this->config->mail->smtp->port)) $this->mta->Port = $this->config->mail->smtp->port; } - private function setPhpMail($param) + /* PHP Mail方式。*/ + private function setPhpMail() { $this->mta->isMail(); } - private function setSendMail($param) + /* SendMail方式。*/ + private function setSendMail() { $this->mta->isSendmail(); } - public function send($subject, $body, $to, $cc) + /* GMAIL方式。*/ + private function setGMail() { - $this->factory($this->config->mailer->mta); - // Define From Address. - $this->From = $BugConfig["this"]["FromAddress"]; - $this->FromName = $BugConfig["this"]["FromName"]; + $this->mta->isSMTP(); + $this->mta->SMTPDebug = $this->config->mail->gmail->debug; + $this->mta->Host = 'smtp.gmail.com'; + $this->mta->Port = 465; + $this->mta->SMTPSecure = "ssl"; + $this->mta->SMTPAuth = true; + $this->mta->Username = $this->config->mail->gmail->username; + $this->mta->Password = $this->config->mail->gmail->password; + } - // Add To Address. - foreach($ToList as $To) - { - $this->addAddress($To); - } - if(is_array($CCList)) - { - foreach($CCList as $CC) - { - $this->addCC($CC); - } - } - // Add Subject. - $this->Subject = stripslashes($Subject); + /* 设置发送地址。*/ + private function setTO($toList) + { + foreach($toList as $toName => $toAddress) $this->mta->addAddress($toAddress, $toName); + } - // Set Body. - $this->IsHTML(true); - $this->CharSet = $BugConfig["Charset"]; - $this->Body = stripslashes($Message); - if(!$this->Send()) - { - $MyJS->alert($this->ErrorInfo); - } + /* 设置抄送地址。*/ + private function setCC($ccList) + { + if(!is_array($ccList)) return; + foreach($ccList as $ccName => $ccAddress) $this->mta->addCC($ccAddress, $ccName); + } + + /* 设置主题。*/ + private function setSubject($subject) + { + $this->mta->Subject = stripslashes($subject); + } + + /* 设置body。*/ + private function setBody($body) + { + $this->mta->msgHtml($body); + } + + /* 清楚地址和附件。*/ + private function clear() + { + $this->mta->clearAddresses(); + $this->mta->cearAttachments(); } }