Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Add a tracer for mail sending
Browse files Browse the repository at this point in the history
  • Loading branch information
gggeek committed Jul 27, 2014
1 parent de86b18 commit 0ad6061
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 0 deletions.
26 changes: 26 additions & 0 deletions classes/ezperfloggermailprovider.php
@@ -0,0 +1,26 @@
<?php
/**
*
* @author gaetano.giunta
*/

class eZPerfLoggerMailProvider implements eZPerfLoggerProvider
{

static public function measure( $output, $returnCode=null )
{
return eZPerfLoggerGenericTracer::StdKPIsFromAccumulators( array(
'mail_sent'
), eZPerfLogger::TimeAccumulatorList()
);
}

public static function supportedVariables()
{
return array(
'mail_sent' => 'integer',
'mail_sent_t' => 'float (secs, rounded to msec)',
'mail_sent_tmax' => 'float (secs, rounded to msec)',
);
}
}
90 changes: 90 additions & 0 deletions classes/tracers/4.7/ezsendmailtracingtransport.php
@@ -0,0 +1,90 @@
<?php
/**
* Modified mail transport class that traces execution times even with debug off
*/

class eZSendmailTracing47Transport extends eZSendmailTransport
{
function sendMail( eZMail $mail )
{
$ini = eZINI::instance();
$sendmailOptions = '';
$emailFrom = $mail->sender();
$emailSender = isset( $emailFrom['email'] ) ? $emailFrom['email'] : false;
if ( !$emailSender || count( $emailSender) <= 0 )
$emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
if ( !$emailSender )
$emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
if ( !eZMail::validate( $emailSender ) )
$emailSender = false;

$isSafeMode = ini_get( 'safe_mode' ) != 0;

$sendmailOptionsArray = $ini->variable( 'MailSettings', 'SendmailOptions' );
if( is_array($sendmailOptionsArray) )
$sendmailOptions = implode( ' ', $sendmailOptionsArray );
elseif( !is_string($sendmailOptionsArray) )
$sendmailOptions = $sendmailOptionsArray;
if ( !$isSafeMode and
$emailSender )
$sendmailOptions .= ' -f'. $emailSender;

if ( $isSafeMode and
$emailSender and
$mail->sender() == false )
$mail->setSenderText( $emailSender );

if( function_exists( 'mail' ) )
{
$message = $mail->body();
$sys = eZSys::instance();
$excludeHeaders = array( 'Subject' );
// If not Windows PHP mail() implementation, we can not specify a To: header in the $additional_headers parameter,
// because then there will be 2 To: headers in the resulting e-mail.
// However, we can use "undisclosed-recipients:;" in $to.
if ( $sys->osType() != 'win32' )
{
$excludeHeaders[] = 'To';
$receiverEmailText = count( $mail->ReceiverElements ) > 0 ? $mail->receiverEmailText() : 'undisclosed-recipients:;';
}
// If Windows PHP mail() implementation, we can specify a To: header in the $additional_headers parameter,
// it will be used as the only To: header.
// We can not use "undisclosed-recipients:;" in $to, it will result in a SMTP server response: 501 5.1.3 Bad recipient address syntax
else
{
$receiverEmailText = $mail->receiverEmailText();
}

// If in debug mode, send to debug email address and nothing else
if ( $ini->variable( 'MailSettings', 'DebugSending' ) == 'enabled' )
{
$receiverEmailText = $ini->variable( 'MailSettings', 'DebugReceiverEmail' );
$excludeHeaders[] = 'To';
$excludeHeaders[] = 'Cc';
$excludeHeaders[] = 'Bcc';
}

$extraHeaders = $mail->headerText( array( 'exclude-headers' => $excludeHeaders ) );

eZPerfLogger::accumulatorStart( 'mail_sent' );
$returnedValue = mail( $receiverEmailText, $mail->subject(), $message, $extraHeaders, $sendmailOptions );
eZPerfLogger::accumulatorStop( 'mail_sent' );

if ( $returnedValue === false )
{
eZDebug::writeError( 'An error occurred while sending e-mail. Check the Sendmail error message for further information (usually in /var/log/messages)',
__METHOD__ );
}

return $returnedValue;
}
else
{
eZDebug::writeWarning( "Unable to send mail: 'mail' function is not compiled into PHP.", __METHOD__ );
}

return false;
}
}

?>
84 changes: 84 additions & 0 deletions classes/tracers/4.7/ezsmtptracingtransport.php
@@ -0,0 +1,84 @@
<?php
/**
* Modified mail transport class that traces execution times even with debug off
*/

class eZSMTPTracing47Transport extends eZSMTPTransport
{
function sendMail( eZMail $mail )
{
$ini = eZINI::instance();
$parameters = array();
$parameters['host'] = $ini->variable( 'MailSettings', 'TransportServer' );
$parameters['helo'] = $ini->variable( 'MailSettings', 'SenderHost' );
$parameters['port'] = $ini->variable( 'MailSettings', 'TransportPort' );
$parameters['connectionType'] = $ini->variable( 'MailSettings', 'TransportConnectionType' );
$user = $ini->variable( 'MailSettings', 'TransportUser' );
$password = $ini->variable( 'MailSettings', 'TransportPassword' );
if ( $user and
$password )
{
$parameters['auth'] = true;
$parameters['user'] = $user;
$parameters['pass'] = $password;
}

/* If email sender hasn't been specified or is empty
* we substitute it with either MailSettings.EmailSender or AdminEmail.
*/
if ( !$mail->senderText() )
{
$emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
if ( !$emailSender )
$emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );

eZMail::extractEmail( $emailSender, $emailSenderAddress, $emailSenderName );

if ( !eZMail::validate( $emailSenderAddress ) )
$emailSender = false;

if ( $emailSender )
$mail->setSenderText( $emailSender );
}

$excludeHeaders = $ini->variable( 'MailSettings', 'ExcludeHeaders' );
if ( count( $excludeHeaders ) > 0 )
$mail->Mail->appendExcludeHeaders( $excludeHeaders );

$options = new ezcMailSmtpTransportOptions();
if( $parameters['connectionType'] )
{
$options->connectionType = $parameters['connectionType'];
}
$smtp = new ezcMailSmtpTransport( $parameters['host'], $user, $password,
$parameters['port'], $options );

// If in debug mode, send to debug email address and nothing else
if ( $ini->variable( 'MailSettings', 'DebugSending' ) == 'enabled' )
{
$mail->Mail->to = array( new ezcMailAddress( $ini->variable( 'MailSettings', 'DebugReceiverEmail' ) ) );
$mail->Mail->cc = array();
$mail->Mail->bcc = array();
}

// send() from ezcMailSmtpTransport doesn't return anything (it uses exceptions in case
// something goes bad)
try
{
eZPerfLogger::accumulatorStart( 'mail_sent' );
$smtp->send( $mail->Mail );
eZPerfLogger::accumulatorStop( 'mail_sent' );
}
catch ( ezcMailException $e )
{
eZPerfLogger::accumulatorStop( 'mail_send' );
eZDebug::writeError( $e->getMessage(), __METHOD__ );
return false;
}

// return true in case of no exceptions
return true;
}
}

?>
3 changes: 3 additions & 0 deletions doc/changelogs/changelog-0.11.1-to-0.12
Expand Up @@ -6,6 +6,9 @@ changelog from version 0.11 to 0.12 - released 2014.7.xx
See the new settings in ezcontentsync.ini and binaryfile.ini.append.php for how to enable this.
NB: this only works if you are using the standard text extractors or the eztika extension.

- It is now possible to trace the time eZ spends sending mails.
Only available for sendail/smpt transports, for eZP 4.7

* Bugfixes


Expand Down
7 changes: 7 additions & 0 deletions settings/ezperformancelogger.ini
Expand Up @@ -112,6 +112,10 @@ TrackVariables[]=_server/REQUEST_METHOD
#TrackVariables[]=binaryfile_metadataextractions_t
#TrackVariables[]=binaryfile_metadataextractions_tmax

#TrackVariables[]=mail_sent
#TrackVariables[]=mail_sent_t
#TrackVariables[]=mail_sent_tmax

# A list of php classes used to provide performance variables and their values.
# At the end of every page execution, the static method measure() will be invoked on these
# classes, to allow them to set proper measured data for the custom variables defined above.
Expand All @@ -125,6 +129,9 @@ VariableProviders[]=ezPerfLoggerEventListener
# When enbaling this, also edit binaryfile.ini.append.php to enable the corrsponding metadata extractors
#VariableProviders[]=eZPerfLoggerMetadataExtractorProvider

# ...
#VariableProviders[]=eZPerfLoggerMailProvider

# The default providers available for all release from 4.4 to 5.1 (change name as appropriate) are:
#VariableProviders[]=eZMySQLiTracing47DB
#VariableProviders[]=eZDFSTracing47FileHandler
Expand Down
5 changes: 5 additions & 0 deletions settings/site.ini.append.php
Expand Up @@ -37,3 +37,8 @@
#ImplementationAlias[ezmysqli]=eZMySQLiTracing47DB
#ImplementationAlias[ezmysqli]=eZMySQLiTracing50DB
#ImplementationAlias[ezmysqli]=eZMySQLiTracing51DB

[MailSettings]
# ONLY UNCOMMENT ONE LINE PER PHP VERSION
#TransportAlias[smtp]=eZSMTPTracing47Transport
#TransportAlias[sendmail]=eZSendmailTracing47Transport

0 comments on commit 0ad6061

Please sign in to comment.