Allow test mail configuration - refs #8308

pull/2487/head
Angel Fernando Quiroz Campos 9 years ago
parent 5f6730f5e6
commit 439008d4c5
  1. 2
      app/config/mail.conf.dist.php
  2. 65
      main/admin/email_tester.php
  3. 6
      main/admin/index.php
  4. 72
      main/inc/lib/message.lib.php
  5. 19
      main/template/default/admin/email_tester.tpl

@ -10,7 +10,7 @@ $platform_email['SMTP_FROM_EMAIL'] = (isset($administrator['email']) ? $administ
$platform_email['SMTP_FROM_NAME'] = (isset($administrator['name']) ? $administrator['name'] : 'Admin'); $platform_email['SMTP_FROM_NAME'] = (isset($administrator['name']) ? $administrator['name'] : 'Admin');
$platform_email['SMTP_HOST'] = 'localhost'; $platform_email['SMTP_HOST'] = 'localhost';
$platform_email['SMTP_PORT'] = 25; $platform_email['SMTP_PORT'] = 25;
$platform_email['SMTP_MAILER'] = IS_WINDOWS_OS ? 'smtp' : 'mail'; // mail, sendmail or smtp $platform_email['SMTP_MAILER'] = 'sendmail'; // mail, sendmail or smtp
$platform_email['SMTP_AUTH'] = 0; $platform_email['SMTP_AUTH'] = 0;
$platform_email['SMTP_USER'] = ''; $platform_email['SMTP_USER'] = '';
$platform_email['SMTP_PASS'] = ''; $platform_email['SMTP_PASS'] = '';

@ -0,0 +1,65 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Index page of the admin tools
* @package chamilo.admin
*/
// Resetting the course id.
$cidReset = true;
// Including some necessary chamilo files.
require_once '../inc/global.inc.php';
api_protect_admin_script();
// Setting the section (for the tabs).
$this_section = SECTION_PLATFORM_ADMIN;
$toolName = get_lang('EMailTester');
$form = new FormValidator('email_tester');
$form->addText('smtp_host', get_lang('Host'), false, ['cols-size' => [3, 8, 1]]);
$form->addText('smtp_port', get_lang('Port'), false, ['cols-size' => [3, 8, 1]]);
$form->addText('destination', get_lang('Destination'), true, ['cols-size' => [3, 8, 1]]);
$form->addText('subject', get_lang('Subject'), true, ['cols-size' => [3, 8, 1]]);
$form->addHtmlEditor('content', get_lang('Message'), true, false, ['ToolbarSet' => 'Minimal', 'cols-size' => [3, 8, 1]]);
$form->addButtonSend(get_lang('SendMessage'), 'submit', false, ['cols-size' => [3, 8, 1]]);
$form->setDefaults([
'smtp_host' => $platform_email['SMTP_HOST'],
'smtp_port' => $platform_email['SMTP_PORT']
]);
$form->freeze(['smtp_host', 'smtp_port']);
$errorsInfo = MessageManager::failedSentMailErrors();
if ($form->validate()) {
$values = $form->exportValues();
$user = api_get_user_entity(api_get_user_id());
$mailIsSent = api_mail_html(
get_lang('UserTestingEMailConf'),
$values['destination'],
$values['subject'],
$values['content'],
$user->getCompleteName(),
$user->getEmail()
);
Display::addFlash(
Display::return_message(get_lang('MailingTestSent'), 'warning')
);
header('Location: ' . api_get_self());
exit;
}
$view = new Template($toolName);
$view->assign('form', $form->returnForm());
$view->assign('errors', $errorsInfo);
$template = $view->get_template('admin/email_tester.tpl');
$content = $view->fetch($template);
$view->assign('header', $toolName);
$view->assign('content', $content);
$view->display_one_col_template();

@ -350,11 +350,9 @@ if (api_is_platform_admin()) {
$items[] = array('url' => 'archive_cleanup.php', 'label' => get_lang('ArchiveDirCleanup')); $items[] = array('url' => 'archive_cleanup.php', 'label' => get_lang('ArchiveDirCleanup'));
$items[] = array('url' => 'resource_sequence.php', 'label' => get_lang('ResourcesSequencing')); $items[] = array('url' => 'resource_sequence.php', 'label' => get_lang('ResourcesSequencing'));
$items[] = ['url' => 'email_tester.php', 'label' => get_lang('EMailTester')];
if (isset($_configuration['db_manager_enabled']) && if (api_get_configuration_value('db_manager_enabled') == true && api_is_global_platform_admin()) {
$_configuration['db_manager_enabled'] == true &&
api_is_global_platform_admin()
) {
$host = $_configuration['db_host']; $host = $_configuration['db_host'];
$username = $_configuration['db_user']; $username = $_configuration['db_user'];
$databaseName = $_configuration['main_database']; $databaseName = $_configuration['main_database'];

@ -227,7 +227,8 @@ class MessageManager
$topic_id = 0, $topic_id = 0,
$sender_id = null, $sender_id = null,
$directMessage = false $directMessage = false
) { )
{
$table_message = Database::get_main_table(TABLE_MESSAGE); $table_message = Database::get_main_table(TABLE_MESSAGE);
$group_id = intval($group_id); $group_id = intval($group_id);
$receiver_user_id = intval($receiver_user_id); $receiver_user_id = intval($receiver_user_id);
@ -434,7 +435,8 @@ class MessageManager
$sender_id = null, $sender_id = null,
$sendCopyToDrhUsers = false, $sendCopyToDrhUsers = false,
$directMessage = false $directMessage = false
) { )
{
$result = MessageManager::send_message( $result = MessageManager::send_message(
$receiver_user_id, $receiver_user_id,
$subject, $subject,
@ -592,7 +594,8 @@ class MessageManager
$receiver_user_id = 0, $receiver_user_id = 0,
$sender_user_id = 0, $sender_user_id = 0,
$group_id = 0 $group_id = 0
) { )
{
$tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT); $tbl_message_attach = Database::get_main_table(TABLE_MESSAGE_ATTACHMENT);
// Try to add an extension to the file if it hasn't one // Try to add an extension to the file if it hasn't one
@ -1882,4 +1885,67 @@ class MessageManager
); );
} }
} }
/**
* Get the error log from failed mailing
* @return array|bool
*/
public static function failedSentMailErrors()
{
$base = api_get_path(SYS_ARCHIVE_PATH) . 'mail/';
$mailq = $base . 'mailq';
if (!file_exists($mailq) || !is_readable($mailq)) {
return false;
}
$file = fopen($mailq, 'r');
$i = 1;
while (!feof($file)) {
$line = fgets($file);
//$line = trim($line);
if (trim($line) == '') {
continue;
}
//Get the mail code, something like 1WBumL-0002xg-FF
if (preg_match('/(.*)\s((.*)-(.*)-(.*))\s<(.*)$/', $line, $codeMatches)) {
$mail_queue[$i]['code'] = $codeMatches[2];
}
$fullMail = $base . $mail_queue[$i]['code'];
$mailFile = fopen($fullMail, 'r');
//Get the reason of mail fail
$iX = 1;
while (!feof($mailFile)) {
$mailLine = fgets($mailFile);
#if ($iX == 4 && preg_match('/(.*):\s(.*)$/', $mailLine, $matches)) {
if (
$iX == 2 &&
preg_match('/(.*)(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s(.*)/', $mailLine, $detailsMatches)
) {
$mail_queue[$i]['reason'] = $detailsMatches[3];
}
$iX++;
}
fclose($mailFile);
//Get the time of mail fail
if (preg_match('/^\s?(\d+)(\D+)\s+(.*)$/', $line, $timeMatches)) {
$mail_queue[$i]['time'] = $timeMatches[1] . $timeMatches[2];
} elseif (preg_match('/^(\s+)((.*)@(.*))\s+(.*)$/', $line, $emailMatches)) {
$mail_queue[$i]['mail'] = $emailMatches[2];
$i++;
}
}
fclose($file);
return array_reverse($mail_queue);
}
} }

@ -0,0 +1,19 @@
<div class="row">
<div class="col-sm-5">
{{ form }}
</div>
{% if not errors is empty %}
<div class="col-sm-7">
<h4 class="page-header">{{ 'Errors'|get_lang }}</h4>
<ul>
{% for error in errors %}
<li>
{{ 'Email: %s. %s ago'|format(error.mail, error.time) }}
<pre>{{ error.reason|replace({'\n': '<br>'}) }}</pre>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
</div>
Loading…
Cancel
Save