eZ Publish  [trunk]
ezsendmailtransport.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZSendmailTransport class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZSendmailTransport ezsendmailtransport.php
00013   \brief Sends the email message to sendmail which takes care of sending the actual message.
00014 
00015   Uses the mail() function in PHP to pass the email to the sendmail system.
00016 
00017 */
00018 
00019 class eZSendmailTransport extends eZMailTransport
00020 {
00021     /*!
00022      Constructor
00023     */
00024     function eZSendmailTransport()
00025     {
00026     }
00027 
00028     function sendMail( eZMail $mail )
00029     {
00030         $ini = eZINI::instance();
00031         $sendmailOptions = '';
00032         $emailFrom = $mail->sender();
00033         $emailSender = isset( $emailFrom['email'] ) ? $emailFrom['email'] : false;
00034         if ( !$emailSender || count( $emailSender) <= 0 )
00035             $emailSender = $ini->variable( 'MailSettings', 'EmailSender' );
00036         if ( !$emailSender )
00037             $emailSender = $ini->variable( 'MailSettings', 'AdminEmail' );
00038         if ( !eZMail::validate( $emailSender ) )
00039             $emailSender = false;
00040 
00041         $isSafeMode = ini_get( 'safe_mode' ) != 0;
00042 
00043         $sendmailOptionsArray = $ini->variable( 'MailSettings', 'SendmailOptions' );
00044         if( is_array($sendmailOptionsArray) )
00045             $sendmailOptions = implode( ' ', $sendmailOptionsArray );
00046         elseif( !is_string($sendmailOptionsArray) )
00047             $sendmailOptions = $sendmailOptionsArray;
00048         if ( !$isSafeMode and
00049              $emailSender )
00050             $sendmailOptions .= ' -f'. $emailSender;
00051 
00052         if ( $isSafeMode and
00053              $emailSender and
00054              $mail->sender() == false )
00055             $mail->setSenderText( $emailSender );
00056 
00057         if( function_exists( 'mail' ) )
00058         {
00059             $message = $mail->body();
00060             $sys = eZSys::instance();
00061             $excludeHeaders = array( 'Subject' );
00062             // If not Windows PHP mail() implementation, we can not specify a To: header in the $additional_headers parameter,
00063             // because then there will be 2 To: headers in the resulting e-mail.
00064             // However, we can use "undisclosed-recipients:;" in $to.
00065             if ( $sys->osType() != 'win32' )
00066             {
00067                 $excludeHeaders[] = 'To';
00068                 $receiverEmailText = count( $mail->ReceiverElements ) > 0 ? $mail->receiverEmailText() : 'undisclosed-recipients:;';
00069             }
00070             // If Windows PHP mail() implementation, we can specify a To: header in the $additional_headers parameter,
00071             // it will be used as the only To: header.
00072             // We can not use "undisclosed-recipients:;" in $to, it will result in a SMTP server response: 501 5.1.3 Bad recipient address syntax
00073             else
00074             {
00075                 $receiverEmailText = $mail->receiverEmailText();
00076             }
00077 
00078             // If in debug mode, send to debug email address and nothing else
00079             if ( $ini->variable( 'MailSettings', 'DebugSending' ) == 'enabled' )
00080             {
00081                 $receiverEmailText = $ini->variable( 'MailSettings', 'DebugReceiverEmail' );
00082                 $excludeHeaders[] = 'To';
00083                 $excludeHeaders[] = 'Cc';
00084                 $excludeHeaders[] = 'Bcc';
00085             }
00086 
00087             $extraHeaders = $mail->headerText( array( 'exclude-headers' => $excludeHeaders ) );
00088 
00089             $returnedValue = mail( $receiverEmailText, $mail->subject(), $message, $extraHeaders, $sendmailOptions );
00090             if ( $returnedValue === false )
00091             {
00092                 eZDebug::writeError( 'An error occurred while sending e-mail. Check the Sendmail error message for further information (usually in /var/log/messages)',
00093                                      __METHOD__ );
00094             }
00095 
00096             return $returnedValue;
00097         }
00098         else
00099         {
00100             eZDebug::writeWarning( "Unable to send mail: 'mail' function is not compiled into PHP.", __METHOD__ );
00101         }
00102 
00103         return false;
00104     }
00105 }
00106 
00107 ?>