Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mantisbt/mantisbt
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a83d43fe1b0b
Choose a base ref
...
head repository: mantisbt/mantisbt
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0e2c6b469d7c
Choose a head ref
  • 3 commits
  • 3 files changed
  • 1 contributor

Commits on Jan 14, 2013

  1. Copy the full SHA
    aadfbcb View commit details

Commits on Jan 17, 2013

  1. Improve logging when sending e-mails

    The email_api email_send() and email_send_all() functions have been
    modified to provide a better log trace in case of errors.
    
    A log entry is now printed in the following situations
    - a message as already been sent
    - the server is not responding and the batch sending is aborted
    - whenever a phpMailer exception is thrown in email_send()
    
    In addition, the log message gets the detailed error from phpMailer's
    ErrorInfo property instead of the exception's getMessage(), as in some
    cases (particularly when using PHPMAILER_METHOD_SMTP), the latter only
    contains partial information instead of the full error text.
    
    $t_emails_recipients_failed variable initialization has been removed as
    it was not used in the code.
    
    Fixes #15382
    dregad committed Jan 17, 2013
    Copy the full SHA
    ad5f2f7 View commit details
  2. Make log_event() calls print to stdout when running from CLI

    This simple modification to the logging_api allows command-line scripts
    such as send_emails.php to easily provide detailed information about
    their operations, provided that the appropriate log_level is set in
    config_inc.php.
    
    This output can then be redirected to log files, etc.
    
    Fixes #15382
    dregad committed Jan 17, 2013
    Copy the full SHA
    0e2c6b4 View commit details
Showing with 44 additions and 24 deletions.
  1. +1 −0 .gitignore
  2. +34 −20 core/email_api.php
  3. +9 −4 core/logging_api.php
1 change: 1 addition & 0 deletions .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ plugins/*
.settings
.idea/
*.kdev4
nbproject/

# IIS7
web.config
54 changes: 34 additions & 20 deletions core/email_api.php
Original file line number Diff line number Diff line change
@@ -867,34 +867,43 @@ function email_store( $p_recipient, $p_subject, $p_message, $p_headers = null )
*/
function email_send_all($p_delete_on_failure = false) {
$t_ids = email_queue_get_ids('ASC');
$t_date_format = config_get( 'complete_date_format' );

log_event( LOG_EMAIL, "Processing e-mail queue (" . count( $t_ids ) . " messages)" );

$t_emails_recipients_failed = array();
$t_start = microtime(true);
log_event( LOG_EMAIL, "Processing " . count( $t_ids ) . " queued messages" );
foreach( $t_ids as $t_id ) {
$t_email_data = email_queue_get( $t_id );
$t_start = microtime(true);

log_event( LOG_EMAIL,
"Sending message #$t_id queued on " .
date( config_get( 'complete_date_format' ), $t_email_data->submitted )
date( $t_date_format, $t_email_data->submitted )
);

# check if email was not found. This can happen if another request picks up the email first and sends it.
# check if email was not found. This can happen if another request
# picks up the email first and sends it.
if( $t_email_data === false ) {
continue;
$t_email_sent = true;
log_event( LOG_EMAIL, 'message has already been sent' );
} else {
$t_email_sent = email_send( $t_email_data );
}

# if unable to place the email in the email server queue, then the connection to the server is down,
# and hence no point to continue trying with the rest of the emails.
if( !email_send( $t_email_data ) ) {
if( !$t_email_sent ) {
if ($p_delete_on_failure) {
email_queue_delete( $t_email_data->email_id );
}

# If unable to place the email in the email server queue and more
# than 5 seconds have elapsed, then we assume that the server
# connection is down, hence no point to continue trying with the
# rest of the emails.
if( microtime(true) - $t_start > 5 ) {
log_event( LOG_EMAIL, 'Server not responding for 5 seconds, aborting' );
break;
} else {
continue;
}
}

}
}

@@ -916,6 +925,8 @@ function email_send( $p_email_data ) {
$t_debug_email = config_get( 'debug_email' );
$t_mailer_method = config_get( 'phpMailer_method' );

$t_log_msg = 'ERROR: Message could not be sent - ';

if( is_null( $g_phpMailer ) ) {
if ( $t_mailer_method == PHPMAILER_METHOD_SMTP ) {
register_shutdown_function( 'email_smtp_close' );
@@ -986,8 +997,9 @@ function email_send( $p_email_data ) {

try {
$mail->AddAddress( $t_recipient, '' );
} catch ( phpmailerException $e ) {
log_event( LOG_EMAIL, "ERROR: Message could not be sent - " . $e->getMessage() );
}
catch ( phpmailerException $e ) {
log_event( LOG_EMAIL, $t_log_msg . $mail->ErrorInfo );
$t_success = false;
$mail->ClearAllRecipients();
$mail->ClearAttachments();
@@ -1020,20 +1032,22 @@ function email_send( $p_email_data ) {
}
}

try
{
if ( !$mail->Send() ) {
$t_success = false;
} else {
try {
$t_success = $mail->Send();
if ( $t_success ) {
$t_success = true;

if ( $t_email_data->email_id > 0 ) {
email_queue_delete( $t_email_data->email_id );
}
} else {
# We should never get here, as an exception is thrown after failures
log_event( LOG_EMAIL, $t_log_msg . $mail->ErrorInfo );
$t_success = false;
}
}
catch ( phpmailerException $e )
{
catch ( phpmailerException $e ) {
log_event( LOG_EMAIL, $t_log_msg . $mail->ErrorInfo );
$t_success = false;
}

13 changes: 9 additions & 4 deletions core/logging_api.php
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@
* Log an event
* @param int $p_level Valid debug log level
* @param string|array $p_msg Either a string, or an array structured as (string,execution time)
* @param object $p_backtrace [Optional] debug_backtrace() stack to use
* @param object $p_backtrace [Optional] debug_backtrace() stack to use
* @return null
*/
function log_event( $p_level, $p_msg, $p_backtrace = null ) {
@@ -99,14 +99,16 @@ function log_event( $p_level, $p_msg, $p_backtrace = null ) {
if ( is_blank( $t_log_destination ) ) {
$t_destination = '';
} else {
# Use @ to avoid error when there is no delimiter in log destination
@list( $t_destination, $t_modifiers ) = explode( ':', $t_log_destination, 2 );
}

$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;

switch( $t_destination ) {
case 'none':
break;
case 'file':
$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
error_log( $t_php_event . PHP_EOL, 3, $t_modifiers );
break;
case 'page':
@@ -124,17 +126,20 @@ function log_event( $p_level, $p_msg, $p_backtrace = null ) {
if( $firephp === null ) {
$firephp = FirePHP::getInstance(true);
}
$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
$firephp->log( $p_msg, $t_php_event );
return;
}
// if firebug is not available, fall through
default:
# use default PHP error log settings
$t_php_event = $t_now . ' ' . $t_level . ' ' . $s_msg;
error_log( $t_php_event . PHP_EOL );
break;
}

# If running from command line, echo log event to stdout
if( $t_destination != 'none' && php_sapi_name() == 'cli' ) {
echo $t_php_event . PHP_EOL;
}
}

function log_print_to_page() {