* Code for task#818.

This commit is contained in:
wangchunsheng
2012-06-23 02:33:14 +00:00
parent 1f9c3853db
commit db990d05df
7 changed files with 117 additions and 103 deletions
+21 -7
View File
@@ -57,13 +57,20 @@ class mail extends control
*/
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';
if($this->config->mail->turnon)
{
$mailConfig = $this->config->mail->smtp;
$mailConfig->fromAddress = $this->config->mail->fromAddress;
$mailConfig->fromName = $this->config->mail->fromName;
}
elseif($this->session->mailConfig)
{
$mailConfig = $this->session->mailConfig;
}
else
{
$this->locate(inlink('detect'));
}
$this->view->header->title = $this->lang->mail->edit;
$this->view->position[] = html::a(inlink('index'), $this->lang->mail->common);
@@ -105,6 +112,8 @@ EOT;
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);
if($saved) $this->session->set('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;
@@ -125,6 +134,11 @@ EOT;
*/
public function test()
{
if(!$this->config->mail->turnon)
{
die(js::alert($this->lang->mail->needConfigure) . js::locate('back'));
}
if($_POST)
{
$this->mail->send($this->post->to, $this->lang->mail->subject, $this->lang->mail->content,"", true);
+35 -46
View File
@@ -1,53 +1,42 @@
<?php
$lang->mail->setParam = 'Please input param of Email';
$lang->mail->common = 'Email setting';
$lang->mail->index = 'Index';
$lang->mail->detect = 'Detect';
$lang->mail->edit = 'Configure';
$lang->mail->save = 'Save Configuration';
$lang->mail->test = 'Testing';
$lang->mail->turnon = 'trun on email feature or not';
$lang->mail->fromAddress = 'The from address';
$lang->mail->fromName = 'The from name';
$lang->mail->mta = 'The style of send email';
$lang->mail->debugExample = '0:close,1 and 2 :open,2 is more infomation than 1';
$lang->mail->turnon = 'Turnon';
$lang->mail->fromAddress = 'From email';
$lang->mail->fromName = 'From title';
$lang->mail->mta = 'MTA';
$lang->mail->host = 'SMTP host';
$lang->mail->port = 'SMTP port';
$lang->mail->auth = 'Authentication';
$lang->mail->username = 'SMTP account';
$lang->mail->password = 'SMTP password';
$lang->mail->secure = 'Secure';
$lang->mail->debug = 'Debug';
$lang->mail->mtaList['gmail'] = 'Gmail sever';
$lang->mail->mtaList['smtp'] = 'Smtp server';
$lang->mail->mtaList['phpmail'] = 'Mail function build in php';
$lang->mail->mtaList['sendmail'] = 'Sendmail on localhost';
$lang->mail->turnonList[1] = 'on';
$lang->mail->turnonList[0] = 'off';
/* Trun on email feature or not */
$lang->mail->turnonList['true'] = 'open';
$lang->mail->turnonList['false'] = 'close';
$lang->mail->debugList[0] = 'off';
$lang->mail->debugList[1] = 'normal';
$lang->mail->debugList[2] = 'high';
$lang->mail->debugList[2] = '2';
$lang->mail->debugList[0] = '0';
$lang->mail->debugList[1] = '1';
$lang->mail->authList[1] = 'necessary';
$lang->mail->authList[0] = 'unnecessary';
$lang->mail->smtp->authList['true'] = 'yes';
$lang->mail->smtp->authList['false'] = 'no';
$lang->mail->secureList[''] = 'plain';
$lang->mail->secureList['ssl'] = 'ssl';
$lang->mail->secureList['tls'] = 'tls';
$lang->mail->smtp->secureList[''] = 'not encode';
$lang->mail->smtp->secureList['ssl'] = 'ssl';
$lang->mail->smtp->secureList['tls'] = 'tls';
/* Set SMTP */
$lang->mail->smtp->fromName = 'The from name';
$lang->mail->smtp->auth = 'Need auth or not';
$lang->mail->smtp->debug = 'Debug level';
$lang->mail->smtp->secure = 'The type to encode datas';
$lang->mail->smtp->host = 'The smtp HOST';
$lang->mail->smtp->hostInfo = 'If the HOST is not special,you can not write it,systom can auto write it.';
$lang->mail->smtp->username = 'The smtp user';
$lang->mail->smtp->password = "The smtp user's password";
$lang->mail->smtp->port = 'The smtp server host port';
$lang->mail->smtp->portInfo = 'ssl: default port is 465, tls: default port is 587, not encode: default port is empty';
/* Set gmail */
$lang->mail->gmail->username = 'The gmail user';
$lang->mail->gmail->password = "The gmail user'password";
$lang->mail->gmail->debug = 'Debug level';
$lang->mail->confirmSave = 'Save successful.Please login your email to view the test mail';
$lang->mail->subject = 'The test mail';
$lang->mail->content = "The email's param is setted";
/* Save config information */
$lang->mail->configInfo = 'Config information';
$lang->mail->saveConfig = 'Please save Config information to:';
$lang->mail->createFile = 'If the zzzemail.php is not exist, please create it.';
$lang->mail->inputFromEmail = 'Please input the from email:';
$lang->mail->nextStep = 'Next';
$lang->mail->successSaved = 'The configuration successfully saved to: <span class="red">%s</span>';
$lang->mail->saveManual = 'Please save the configuration to: <span class="red">%s</span>';
$lang->mail->subject = "It's a testing email from zentao.";
$lang->mail->content = 'If you can see this, the email notification feature can work now!';
$lang->mail->successSended = 'Successfully sended!';
$lang->mail->needConfigure = "I can not find the configuration, please configure it first.";
+1
View File
@@ -39,3 +39,4 @@ $lang->mail->saveManual = '请将该配置信息保存到:<span class="red
$lang->mail->subject = '测试邮件';
$lang->mail->content = '邮箱设置成功';
$lang->mail->successSended = '成功发送!';
$lang->mail->needConfigure = '无法找到邮件配置信息,请先配置邮件发送参数。';
+57 -47
View File
@@ -28,16 +28,48 @@ class mailModel extends model
/**
* Auto detect email config.
*
* @param int $email
* @param string $email
* @access public
* @return void
* @return object
*/
public function autoDetect($email)
{
/* Split the email to username and domain. */
list($username, $domain) = explode('@', $email);
$domain = strtolower($domain);
/* 1. try get config from providers. */
/*
* 1. try to find config from the providers.
* 2. try to find the mx record to get the domain and then search it in providers.
* 3. try smtp.$domain's 25 and 465 port, if can connect, use smtp.$domain.
*/
$config = $this->getConfigFromProvider($domain, $username);
if(!$config) $config = $this->getConfigByMXRR($domain, $username);
if(!$config) $config = $this->getConfigByDetectingSMTP($domain, $username, 25);
if(!$config) $config = $this->getConfigByDetectingSMTP($domain, $username, 465);
/* Set default values. */
$config->mta = 'smtp';
$config->fromName = 'zentao';
$config->password = '';
$config->debug = 1;
if(!isset($config->host)) $config->host = '';
if(!isset($config->auth)) $config->auth = 1;
if(!isset($config->port)) $config->port = '25';
return $config;
}
/**
* Try get config from providers.
*
* @param int $domain
* @param int $username
* @access public
* @return bool|object
*/
public function getConfigFromProvider($domain, $username)
{
if(isset($this->config->mail->provider[$domain]))
{
$config = $this->config->mail->provider[$domain];
@@ -48,44 +80,18 @@ class mailModel extends model
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;
}
}
return false;
}
/**
* Get config by MXRR.
*
* @param int $domain
* @param string $domain
* @param string $username
* @access public
* @return void
* @return bool|object
*/
public function getConfigByMXRR($domain)
public function getConfigByMXRR($domain, $username)
{
/* Try to get mx record, under linux, use getmxrr() directly, windows use nslookup. */
if(function_exists('getmxrr'))
@@ -110,14 +116,9 @@ class mailModel extends model
$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]))
if($config = $this->getConfigFromProvider($smtpDomain, $username))
{
$config = $this->config->mail->provider[$smtpDomain];
$config->mta = 'smtp';
$config->auth = 1;
if(!isset($config->port)) $config->port = 25;
$config->username = "$username@$domain";
return $config;
}
}
@@ -126,20 +127,29 @@ class mailModel extends model
}
/**
* Try to connect SMTP server.
* Try connect to smtp.$domain's 25 or 465 port and compute the config according to the connection result.
*
* @param int $host
* @param string $domain
* @param string $username
* @param int $port
* @access public
* @return void
* @return bool|object
*/
public function connectSMTP($host, $port)
public function getConfigByDetectingSMTP($domain, $username, $port)
{
$host = 'smtp.' . $domain;
ini_set('default_socket_timeout', 1);
$connection = @fsockopen($host, $port);
if(!$connection) return false;
fclose($connection);
return true;
$config->username = $username;
$config->host = $host;
$config->auth = 1;
$config->port = $port;
$config->secure = $port == 465 ? 'ssl' : '';
return $config;
}
/**
+1 -1
View File
@@ -15,7 +15,7 @@ include '../../common/view/header.html.php';
<table align='center' class='table-4'>
<caption><?php echo $lang->mail->edit;?></caption>
<tr>
<th class='rowhead'><?php echo $lang->mail->turnon; ?></th>
<th class='rowhead w-120px'><?php echo $lang->mail->turnon; ?></th>
<td><?php echo html::radio('turnon', $lang->mail->turnonList, 1);?></td>
</tr>
<tr>
+1 -1
View File
@@ -18,8 +18,8 @@ include '../../common/view/header.html.php';
<?php
echo html::textArea('', $mailConfig, "rows='12' class='area-1'") . '<br /><br />';
if($saved) printf($lang->mail->successSaved, $configFile);
if($saved) echo html::linkButton($lang->mail->test, inlink('test'));
if(!$saved) printf($lang->mail->saveManual, $configFile);
echo html::linkButton($lang->mail->test, inlink('test'));
?>
</td>
</tr>
+1 -1
View File
@@ -17,7 +17,7 @@ include '../../common/view/header.html.php';
<tr>
<td class='a-center'>
<?php
echo html::select('to', $users, $app->user->account, 'class=text-3');
echo html::select('to', $users, $app->user->account);
echo html::submitButton($lang->mail->test);
echo html::linkButton($lang->mail->edit, $this->inLink('edit'));
?>