diff --git a/module/mail/config.php b/module/mail/config.php index d4d1296aa4..46df89376c 100644 --- a/module/mail/config.php +++ b/module/mail/config.php @@ -1,24 +1,44 @@ mail->turnon = false; // trun on email feature or not. -$config->mail->fromAddress = ''; // The from address. -$config->mail->fromName = ''; // The from name. -$config->mail->mta = 'smtp'; // phpmail|sendmail|smtp|gmail +$config->mail->turnon = 0; // trun on email feature or not. +$config->mail->fromAddress = ''; // The from address. +$config->mail->fromName = 'zentao'; // The from name. +$config->mail->mta = 'smtp'; // smtp|phpmail. /* SMTP settings. */ if($config->mail->mta == 'smtp') { $config->mail->smtp->debug = 0; // Debug level, 0,1,2. $config->mail->smtp->auth = true; // Need auth or not. - $config->mail->smtp->host = ''; // The smtp server host address. - $config->mail->smtp->port = ''; // The smtp server host port. + $config->mail->smtp->host = 'localhost'; // The smtp server host address. + $config->mail->smtp->port = '25'; // The smtp server host port. $config->mail->smtp->secure = ''; // The type to encode datas, 'ssl' or 'tls' allowed $config->mail->smtp->username = ''; // The smtp user, may be a full email adress. $config->mail->smtp->password = ''; // The smtp user's password. } -/* Gmail setting. */ -elseif($config->mail->mta == 'gmail') -{ - $config->mail->gmail->debug = 0; // Debug level, 0,1,2. - $config->mail->gmail->username = ""; // GMAIL username - $config->mail->gmail->password = ""; // GMAIL password -} + +$config->mail->provider['163.com']->host = 'smtp.163.com'; +$config->mail->provider['yeah.net']->host = 'smtp.yeah.net'; +$config->mail->provider['netease.com']->host = 'smtp.netease.com'; +$config->mail->provider['126.com']->host = 'smtp.126.com'; +$config->mail->provider['qiye.163.com']->host = 'smtp.qiye.163.com'; + +$config->mail->provider['sina.com']->host = 'smtp.sina.com'; +$config->mail->provider['sina.cn']->host = 'smtp.sina.cn'; +$config->mail->provider['vip.sina.com']->host = 'smtp.vip.sina.com'; +$config->mail->provider['sina.net']->host = 'smtp.sina.net'; + +$config->mail->provider['sohu.com']->host = 'smtp.sohu.com'; +$config->mail->provider['vip.sohu.com']->host = 'smtp.vip.sohu.com'; + +$config->mail->provider['21cn.com']->host = 'smtp.21cn.com'; + +$config->mail->provider['qq.com']->host = 'smtp.qq.com'; + +$config->mail->provider['gmail.com']->host = 'smtp.gmail.com'; +$config->mail->provider['gmail.com']->secure = 'ssl'; +$config->mail->provider['gmail.com']->port = '465'; +$config->mail->provider['google.com'] = $config->mail->provider['gmail.com']; +$config->mail->provider['googlemail.com'] = $config->mail->provider['gmail.com']; + +$config->mail->provider['263.net']->host = 'smtp.263.net'; +$config->mail->provider['263xmail.com']->host = 'smtp.263xmail.com'; diff --git a/module/mail/control.php b/module/mail/control.php index a00ab7da94..551625b391 100755 --- a/module/mail/control.php +++ b/module/mail/control.php @@ -11,15 +11,65 @@ */ class mail extends control { + public function index() + { + if($this->config->mail->turnon) $this->locate(inlink('edit')); + $this->locate(inlink('detect')); + } + /** - * Config email. + * Detect email config auto. * * @access public * @return void */ - public function set() + public function detect() { - $this->view->mailConfig = $this->app->loadConfig('mail')->mail; + if($_POST) + { + $error = ''; + if($this->post->fromAddress == false) $error = sprintf($this->lang->error->notempty, $this->lang->mail->fromAddress); + if(!validater::checkEmail($this->post->fromAddress)) $error .= '\n' . sprintf($this->lang->error->email, $this->lang->mail->fromAddress); + + if($error) die(js::alert($error)); + + $mailConfig = $this->mail->autoDetect($this->post->fromAddress); + $mailConfig->fromAddress = $this->post->fromAddress; + $this->session->set('mailConfig', $mailConfig); + + die(js::locate(inlink('edit'), 'parent')); + } + + $this->view->header->title = $this->lang->mail->detect; + $this->view->position[] = html::a(inlink('index'), $this->lang->mail->common); + $this->view->position[] = $this->lang->mail->detect; + + $this->view->fromAddress = $this->session->mailConfig ? $this->session->mailConfig->fromAddress : ''; + + $this->display(); + } + + /** + * Edit the mail config. + * + * @access public + * @return void + */ + public function edit() + { + $mailConfig = $this->session->mailConfig ? $this->session->mailConfig : $this->config->mail->smtp; + + if(!isset($mailConfig->debug)) $mailConfig->debug = 1; + if(!isset($mailConfig->username)) $mailConfig->username = ''; + if(!isset($mailConfig->password)) $mailConfig->password = ''; + if(!isset($mailConfig->secure)) $mailConfig->secure = ''; + if(!isset($mailConfig->fromName)) $mailConfig->fromName = 'zentao'; + + $this->view->header->title = $this->lang->mail->edit; + $this->view->position[] = html::a(inlink('index'), $this->lang->mail->common); + $this->view->position[] = $this->lang->mail->edit; + + $this->view->mailConfig = $mailConfig; $this->display(); } @@ -33,81 +83,56 @@ class mail extends control { if(!empty($_POST)) { - if('gmail' == $this->post->mta) - { - $config = <<mail->turnon = {$this->post->turnon}; -\$config->mail->fromAddress = "{$this->post->fromAddress}"; -\$config->mail->mta = "gmail"; -\$config->mail->fromName = "{$this->post->fromName}"; -\$config->mail->gmail->debug = {$this->post->gmailDebug}; -\$config->mail->gmail->username = "{$this->post->gmailUsername}"; -\$config->mail->gmail->password = "{$this->post->gmailPassword}"; -EOT; - } - elseif('smtp' == $this->post->mta) - { - $position = strpos($this->post->fromAddress, '@'); - if($this->post->smtpHost == '') - { - $host = 'smtp.' . substr($this->post->fromAddress, $position + 1); - } - else - { - $host = $this->post->smtpHost; - } - - $config = <<mail->turnon = {$this->post->turnon}; +\$config->mail->mta = 'smtp'; \$config->mail->fromAddress = "{$this->post->fromAddress}"; -\$config->mail->mta = "{$this->post->mta}"; \$config->mail->fromName = "{$this->post->fromName}"; -\$config->mail->smtp->debug = {$this->post->smtpDebug}; -\$config->mail->smtp->username = "{$this->post->smtpUsername}"; -\$config->mail->smtp->password = "{$this->post->smtpPassword}"; -\$config->mail->smtp->auth = {$this->post->smtpAuth}; -\$config->mail->smtp->host = "$host"; -\$config->mail->smtp->secure = "{$this->post->smtpSecure}"; -\$config->mail->smtp->port = "{$this->post->smtpPort}"; +\$config->mail->smtp->host = "{$this->post->host}"; +\$config->mail->smtp->port = "{$this->post->port}"; +\$config->mail->smtp->auth = {$this->post->auth}; +\$config->mail->smtp->username = "{$this->post->username}"; +\$config->mail->smtp->password = "{$this->post->password}"; +\$config->mail->smtp->secure = "{$this->post->secure}"; +\$config->mail->smtp->debug = {$this->post->debug}; EOT; - } - elseif('phpmail' == $this->post->mta or 'sendmail' == $this->post->mta) - { - $config = <<mail->turnon = {$this->post->turnon}; -\$config->mail->fromAddress = "{$this->post->fromAddress}"; -\$config->mail->mta = "{$this->post->mta}"; -\$config->mail->fromName = "{$this->post->fromName}"; -EOT; - } /* Output config to the extconfig file of mail */ $configPath = $this->app->getModuleExtPath('mail', 'config'); - if(is_writable($configPath)) - { - if(file_put_contents($configPath . 'zzzemail.php', $config)) - { - /* Send test mail */ - $this->mail->send($this->app->user->account, $this->lang->mail->subject, $this->lang->mail->content,"", true); - if($this->mail->isError()) echo js::error($this->mail->getError()); - echo js::confirm($this->lang->mail->confirmSave,$this->createLink('mail', 'set')); - } - else - { - $this->view->config = $config; - $this->view->configPath = $configPath; - $this->display(); - } - } - else - { - $this->view->config = $config; - $this->view->configPath = $configPath; - $this->display(); - } + $configFile = $configPath . 'zzzemail.php'; + $saved = false; + if(is_file($configFile) and is_writable($configFile)) $saved = file_put_contents($configFile, $mailConfig); + if(!is_file($configFile) and is_writable($configPath)) $saved = file_put_contents($configFile, $mailConfig); + + $this->view->header->title = $this->lang->mail->save; + $this->view->position[] = html::a(inlink('index'), $this->lang->mail->common); + $this->view->position[] = $this->lang->mail->save; + + $this->view->mailConfig = $mailConfig; + $this->view->configPath = $configPath; + $this->view->configFile = $configFile; + $this->view->saved = $saved; + $this->display(); } } + + /** + * Send test email. + * + * @access public + * @return void + */ + public function test() + { + if($_POST) + { + $this->mail->send($this->post->to, $this->lang->mail->subject, $this->lang->mail->content,"", true); + if($this->mail->isError()) die(js::error($this->mail->getError())); + die(js::alert($this->lang->mail->successSended)); + } + + $this->view->users = $this->dao->select('account, CONCAT(realname, " ", email) AS email' )->from(TABLE_USER)->where('email')->ne('')->orderBy('account')->fetchPairs(); + $this->display(); + } } diff --git a/module/mail/js/detect.js b/module/mail/js/detect.js new file mode 100644 index 0000000000..06cfea9964 --- /dev/null +++ b/module/mail/js/detect.js @@ -0,0 +1,4 @@ +$(document).ready() +{ + $('#fromAddress').focus(); +} diff --git a/module/mail/js/edit.js b/module/mail/js/edit.js new file mode 100644 index 0000000000..ad537f83eb --- /dev/null +++ b/module/mail/js/edit.js @@ -0,0 +1,4 @@ +$(document).ready() +{ + $('#password').focus(); +} diff --git a/module/mail/js/set.js b/module/mail/js/set.js deleted file mode 100755 index 0293291ec3..0000000000 --- a/module/mail/js/set.js +++ /dev/null @@ -1,57 +0,0 @@ -function setMtaType(type) -{ - if(type == 'gmail') - { - $('#gmailUsername').show(); - $('#gmailPassword').show(); - $('#gmailDebug').show(); - $('#smtpDebug').hide(); - $('#smtpUsername').hide(); - $('#smtpPassword').hide(); - $('#smtpHost').hide(); - $('#smtpAuth').hide(); - $('#smtpSecure').hide(); - $('#smtpPort').hide(); - } - else if(type == 'smtp') - { - $('#gmailUsername').hide(); - $('#gmailPassword').hide(); - $('#gmailDebug').hide(); - $('#smtpDebug').show(); - $('#smtpUsername').show(); - $('#smtpPassword').show(); - $('#smtpHost').show(); - $('#smtpAuth').show(); - $('#smtpSecure').show(); - $('#smtpPort').show(); - } - else if(type == 'phpmail' || type == 'sendmail') - { - $('#gmailUsername').hide(); - $('#gmailPassword').hide(); - $('#gmailDebug').hide(); - $('#smtpDebug').hide(); - $('#smtpUsername').hide(); - $('#smtpPassword').hide(); - $('#smtpHost').hide(); - $('#smtpAuth').hide(); - $('#smtpSecure').hide(); - $('#smtpPort').hide(); - } -} -function setVerificationType(type) -{ - if(type == 'true') - { - $('#smtpUsername').show(); - $('#smtpPassword').show(); - } - else if(type == 'false') - { - $('#smtpUsername').hide(); - $('#smtpPassword').hide(); - } -} -type = document.getElementById("mta") -setMtaType(type.value); diff --git a/module/mail/lang/zh-cn.php b/module/mail/lang/zh-cn.php index 318c91cee8..4f4a9d70ba 100755 --- a/module/mail/lang/zh-cn.php +++ b/module/mail/lang/zh-cn.php @@ -1,53 +1,41 @@ mail->setParam = '请设置下面的配置参数'; +$lang->mail->common = '发信配置'; +$lang->mail->index = '首页'; +$lang->mail->detect = '检测'; +$lang->mail->edit = '编辑配置'; +$lang->mail->save = '保存配置'; +$lang->mail->test = '测试发信'; -$lang->mail->turnon = '是否打开发信功能'; -$lang->mail->fromAddress = '发信人邮箱'; -$lang->mail->fromName = '发信人名称'; -$lang->mail->mta = '请选择发信方式'; -$lang->mail->debugExample = '0表示关闭调试信息,1和2表示打开调试信息,但2比1调试信息显示更详细'; +$lang->mail->turnon = '是否打开'; +$lang->mail->fromAddress = '发信邮箱'; +$lang->mail->fromName = '发信人'; +$lang->mail->mta = '发信方式'; +$lang->mail->host = 'smtp服务器'; +$lang->mail->port = 'smtp端口号'; +$lang->mail->auth = '是否需要验证'; +$lang->mail->username = 'smtp帐号'; +$lang->mail->password = 'smtp密码'; +$lang->mail->secure = '是否加密'; +$lang->mail->debug = '调试级别'; -$lang->mail->mtaList['gmail'] = 'Gmail服务器方式'; -$lang->mail->mtaList['smtp'] = 'SMTP服务器方式'; -$lang->mail->mtaList['phpmail'] = 'PHP内置mail函数'; -$lang->mail->mtaList['sendmail'] = '本机sendmail'; +$lang->mail->turnonList[1] = '打开'; +$lang->mail->turnonList[0] = '关闭'; -/* Trun on email feature or not */ -$lang->mail->turnonList['true'] = '打开'; -$lang->mail->turnonList['false'] = '关闭'; +$lang->mail->debugList[0] = '关闭'; +$lang->mail->debugList[1] = '一般'; +$lang->mail->debugList[2] = '较高'; -$lang->mail->debugList[2] = '2'; -$lang->mail->debugList[0] = '0'; -$lang->mail->debugList[1] = '1'; +$lang->mail->authList[1] = '需要'; +$lang->mail->authList[0] = '不需要'; -$lang->mail->smtp->authList['true'] = '是'; -$lang->mail->smtp->authList['false'] = '否'; +$lang->mail->secureList[''] = '不加密'; +$lang->mail->secureList['ssl'] = 'ssl'; +$lang->mail->secureList['tls'] = 'tls'; -$lang->mail->smtp->secureList[''] = '不加密'; -$lang->mail->smtp->secureList['ssl'] = 'ssl'; -$lang->mail->smtp->secureList['tls'] = 'tls'; - -/* Set SMTP */ -$lang->mail->smtp->fromName = '发信人姓名'; -$lang->mail->smtp->auth = '是否需要验证'; -$lang->mail->smtp->debug = '请选择调试等级'; -$lang->mail->smtp->secure = '请选择SMTP加密方式'; -$lang->mail->smtp->host = '请输入HOST'; -$lang->mail->smtp->hostInfo = '如不是特殊HOST,可不填写,系统会自动生成'; -$lang->mail->smtp->username = '发信邮箱用户名'; -$lang->mail->smtp->password = '请输入密码'; -$lang->mail->smtp->port = '请输入端口号'; -$lang->mail->smtp->portInfo = 'ssl加密方式默认端口号为465,tls加密方式默认端口号为587,不加密端口号一般为空'; -/* Set gmail */ -$lang->mail->gmail->username = '请输入发信邮箱用户名'; -$lang->mail->gmail->password = '请输入发信邮箱密码'; -$lang->mail->gmail->debug = '请选择调试等级'; - -$lang->mail->confirmSave = '保存成功, 请到您的邮箱中查看测试邮件是否发送成功。'; -$lang->mail->subject = '测试邮件'; -$lang->mail->content = '邮箱设置成功'; - -/* Save config information */ -$lang->mail->configInfo = '配置信息'; -$lang->mail->saveConfig = '请将该配置信息保存到: '; -$lang->mail->createFile = '如果zzzemail.php文件不存在,请手动创建该文件,将以上配置保存即可。'; +$lang->mail->inputFromEmail = '请输入发信邮箱:'; +$lang->mail->nextStep = '下一步'; +$lang->mail->successSaved = '配置信息已经成功保存到:%s'; +$lang->mail->saveManual = '请将该配置信息保存到:%s'; +$lang->mail->subject = '测试邮件'; +$lang->mail->content = '邮箱设置成功'; +$lang->mail->successSended = '成功发送!'; diff --git a/module/mail/model.php b/module/mail/model.php index 9b1de6aad7..71db6954e2 100644 --- a/module/mail/model.php +++ b/module/mail/model.php @@ -25,6 +25,123 @@ class mailModel extends model $this->setMTA(); } + /** + * Auto detect email config. + * + * @param int $email + * @access public + * @return void + */ + public function autoDetect($email) + { + list($username, $domain) = explode('@', $email); + $domain = strtolower($domain); + + /* 1. try get config from providers. */ + if(isset($this->config->mail->provider[$domain])) + { + $config = $this->config->mail->provider[$domain]; + $config->mta = 'smtp'; + $config->username = $username; + $config->auth = 1; + if(!isset($config->port)) $config->port = 25; + if(!isset($config->secure)) $config->secure = ''; + return $config; + } + + /* 2. try get config by MX record. */ + $config = $this->getConfigByMXRR($domain); + if($config) + { + $config->username = $email; + return $config; + } + + /* 3. try 25 and 465 port. */ + $smtpHost = 'smtp.' . $domain; + $config->mta = 'smtp'; + $config->username = $username; + $config->host = $smtpHost; + $config->auth = 1; + + if($this->connectSMTP($smtpHost, 25)) + { + $config->port = 25; + $config->secure = ''; + return $config; + } + elseif($this->connectSMTP($smtpHost, 465)) + { + $config->port = 465; + $config->secure = 'ssl'; + return $config; + } + } + + /** + * Get config by MXRR. + * + * @param int $domain + * @access public + * @return void + */ + public function getConfigByMXRR($domain) + { + /* Try to get mx record, under linux, use getmxrr() directly, windows use nslookup. */ + if(function_exists('getmxrr')) + { + getmxrr($domain, $smtpHosts); + } + elseif(strpos(PHP_OS, 'WIN') !== false) + { + $smtpHosts = array(); + $result = `nslookup -q=mx {$domain} 2>nul`; + $lines = explode("\n", $result); + foreach($lines as $line) + { + if(stripos($line, 'exchanger')) $smtpHosts[] = trim(substr($line, strrpos($line, '=') + 1)); + } + } + + /* Cycle the smtpHosts and try to find it's config from the provider config. */ + foreach($smtpHosts as $smtpHost) + { + /* Get the domain name from the hosts, for example: imxbiz1.qq.com get qq.com. */ + $smtpDomain = explode('.', $smtpHost); + array_shift($smtpDomain); + $smtpDomain = strtolower(implode('.', $smtpDomain)); + + /* If there's config in the provider config, return it. */ + if(isset($this->config->mail->provider[$smtpDomain])) + { + $config = $this->config->mail->provider[$smtpDomain]; + $config->mta = 'smtp'; + $config->auth = 1; + if(!isset($config->port)) $config->port = 25; + return $config; + } + } + + return false; + } + + /** + * Try to connect SMTP server. + * + * @param int $host + * @param int $port + * @access public + * @return void + */ + public function connectSMTP($host, $port) + { + ini_set('default_socket_timeout', 1); + $connection = @fsockopen($host, $port); + if(!$connection) return false; + fclose($connection); + return true; + } + /** * Set MTA. * diff --git a/module/mail/view/detect.html.php b/module/mail/view/detect.html.php new file mode 100755 index 0000000000..0522a3d260 --- /dev/null +++ b/module/mail/view/detect.html.php @@ -0,0 +1,20 @@ + + * @package mail + * @version $Id$ + * @link http://www.zentao.net + */ +include '../../common/view/header.html.php'; +?> +
+ + + +
mail->inputFromEmail; ?>
mail->nextStep);?>
+
+ diff --git a/module/mail/view/edit.html.php b/module/mail/view/edit.html.php new file mode 100755 index 0000000000..c28bb591e7 --- /dev/null +++ b/module/mail/view/edit.html.php @@ -0,0 +1,68 @@ + + * @package mail + * @version $Id$ + * @link http://www.zentao.net + */ +include '../../common/view/header.html.php'; +?> +
'> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
mail->edit;?>
mail->turnon; ?>mail->turnonList, 1);?>
mail->fromAddress; ?>fromAddress, 'class=text-3');?>
mail->fromName; ?>fromName, 'class=text-3');?>
mail->host; ?>host, 'class=text-3');?>
mail->port; ?>port, 'class=text-3');?>
mail->auth; ?>mail->authList, $mailConfig->auth, 'onchange=setAuth(this.value)'); ?>
mail->username; ?>username, 'class=text-3') ?>
mail->password; ?>password, 'class="text-3" autocomplete="off"') ?>
mail->secure; ?>mail->secureList, $mailConfig->secure); ?>
mail->debug; ?>mail->debugList, $mailConfig->debug);?>
+ config->mail->turnon) echo html::linkButton($lang->mail->test, inlink('test')); + ?> +
+
+ diff --git a/module/mail/view/save.html.php b/module/mail/view/save.html.php index 3cff4242ed..6ef304539d 100755 --- a/module/mail/view/save.html.php +++ b/module/mail/view/save.html.php @@ -1,25 +1,27 @@ + * @author Chunsheng Wang * @package mail * @version $Id$ * @link http://www.zentao.net */ include '../../common/view/header.html.php'; ?> - - - - - - - - - - +
mail->configInfo ?>
-
mail->saveConfig . $configPath . 'zzzemail.php' ?>
mail->createFile ?>
+ + + +
mail->save ?>
+
'; + if($saved) printf($lang->mail->successSaved, $configFile); + if($saved) echo html::linkButton($lang->mail->test, inlink('test')); + if(!$saved) printf($lang->mail->saveManual, $configFile); + ?> +
+ diff --git a/module/mail/view/set.html.php b/module/mail/view/set.html.php deleted file mode 100755 index 0305991b3a..0000000000 --- a/module/mail/view/set.html.php +++ /dev/null @@ -1,110 +0,0 @@ - - * @package mail - * @version $Id$ - * @link http://www.zentao.net - */ -include '../../common/view/header.html.php'; -include '../../common/view/tablesorter.html.php';?> -turnon == 1) $turnon = 'ture'; -if(!empty($mailConfig->mta)) $mta = $mailConfig->mta; -if(!empty($mailConfig->gmail->username)) $gmailUsername = $mailConfig->gmail->username; -if(!empty($mailConfig->gmail->password)) $gmailPassword = $mailConfig->gmail->password; -if(!empty($mailConfig->smtp->username)) $smtpUsername = $mailConfig->smtp->username; -if(!empty($mailConfig->smtp->password)) $smtpPassword = $mailConfig->smtp->password; -if(!empty($mailConfig->smtp->host)) $smtpHost = $mailConfig->smtp->host; -if(!empty($mailConfig->smtp->secure)) $smtpSecure = $mailConfig->smtp->secure; -if(!empty($mailConfig->smtp->auth)) $smtpAuth = $mailConfig->smtp->auth; -if(!empty($mailConfig->smtp->port)) $smtpPort = $mailConfig->smtp->port; -?> -
'> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mail->setParam; ?>
mail->turnon; ?>mail->turnonList, $turnon, 'class=select-3'); ?>
mail->mta; ?>mail->mtaList, $mta, 'class=select-3 onchange=setMtaType(this.value)'); ?>
mail->fromAddress; ?>fromAddress, 'class=text-3') ?>
mail->fromName; ?>fromName, 'class=text-3') ?>
-
- diff --git a/module/mail/view/test.html.php b/module/mail/view/test.html.php new file mode 100755 index 0000000000..4fc2ec4cff --- /dev/null +++ b/module/mail/view/test.html.php @@ -0,0 +1,28 @@ + + * @package mail + * @version $Id$ + * @link http://www.zentao.net + */ +include '../../common/view/header.html.php'; +?> +
+ + + + + +
mail->test; ?>
+ user->account, 'class=text-3'); + echo html::submitButton($lang->mail->test); + echo html::linkButton($lang->mail->edit, $this->inLink('edit')); + ?> +
+
+