Version: current

     1 <?php
     2 require_once 'Zend/Log/Writer/Abstract.php';
     3
     4 /**
     5  * Sends an error message to an email whenever an error occurs
     6  * in sapphire.
     7  *
     8  * @see SS_Log for more information on using writers.
     9  *
    10  * @package sapphire
    11  * @subpackage dev
    12  */
    13 class SS_LogEmailWriter extends Zend_Log_Writer_Abstract {
    14
    15     /**
    16      * @var $send_from Email address to send log information from
    17      */
    18     protected static $send_from = 'errors@silverstripe.com';
    19
    20     protected $emailAddress;
    21
    22     protected $customSmtpServer;
    23
    24     public function __construct($emailAddress, $customSmtpServer = false) {
    25         $this->emailAddress = $emailAddress;
    26         $this->customSmtpServer = $customSmtpServer;
    27     }
    28
    29     public static function set_send_from($address) {
    30         self::$send_from = $address;
    31     }
    32
    33     public static function get_send_from() {
    34         return self::$send_from;
    35     }
    36
    37     /**
    38      * Send an email to the email address set in
    39      * this writer.
    40      */
    41     public function _write($event) {
    42         // If no formatter set up, use the default
    43         if(!$this->_formatter) {
    44             $formatter = new SS_LogErrorEmailFormatter();
    45             $this->setFormatter($formatter);
    46         }
    47
    48         $formattedData = $this->_formatter->format($event);
    49         $subject = $formattedData['subject'];
    50         $data = $formattedData['data'];
    51
    52         $originalSMTP = ini_get('SMTP');
    53         // override the SMTP server with a custom one if required
    54         if($this->customSmtpServer) ini_set('SMTP', $this->customSmtpServer);
    55         $headers = array(
    56             'Content-type: text/html',
    57             'From: ' . self::$send_from,
    58         );
    59         $member = Member::currentUser();
    60         $headers[] = 'X-Info-User: ' . (($member) ? $member->Email : 'unknown');
    61
    62
    63         foreach (array('REMOTE_ADDR', 'HTTP_HOST', 'REQUEST_URI', 'HTTP_REFERER', 'HTTP_USER_AGENT') as $var) {
    64             if (isset($_SERVER[$var])) {
    65                 $name = 'X-Info-' . mb_convert_case(str_replace('_', '-', $var), MB_CASE_TITLE, "UTF-8");
    66                 $headers[] = $name . ': ' . $_SERVER[$var];
    67             }
    68         }
    69
    70         mail(
    71             $this->emailAddress,
    72             $subject,
    73             $data,
    74             join("\n", $headers)
    75         );
    76
    77         // reset the SMTP server to the original
    78         if($this->customSmtpServer) ini_set('SMTP', $originalSMTP);
    79     }
    80
    81 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -