Blame view

framework/dev/LogEmailWriter.php 2.56 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  <?php
  require_once 'Zend/Log/Writer/Abstract.php';
  
  /**
   * Sends an error message to an email.
   * 
   * @see SS_Log for more information on using writers.
   * 
   * @package framework
   * @subpackage dev
   */
  class SS_LogEmailWriter extends Zend_Log_Writer_Abstract {
  
  	/**
  	 * @config
  	 * @var $send_from Email address to send log information from
  	 */
  	private static $send_from = 'errors@silverstripe.com';
  
  	protected $emailAddress;
  
  	protected $customSmtpServer;
  
  	public function __construct($emailAddress, $customSmtpServer = false) {
  		$this->emailAddress = $emailAddress;
  		$this->customSmtpServer = $customSmtpServer;
  	}
  	
  	public static function factory($emailAddress, $customSmtpServer = false) {
  		return new SS_LogEmailWriter($emailAddress, $customSmtpServer);
  	}
  
  	/**
  	 * @deprecated 3.2 Use the "SS_LogEmailWriter.send_from" config setting instead
  	 */
  	public static function set_send_from($address) {
  		Deprecation::notice('3.2', 'Use the "SS_LogEmailWriter.send_from" config setting instead');
  		Config::inst()->update('SS_LogEmailWriter', 'send_from', $address);
  	}
  
  	/**
  	 * @deprecated 3.2 Use the "SS_LogEmailWriter.send_from" config setting instead
  	 */
  	public static function get_send_from() {
  		Deprecation::notice('3.2', 'Use the "SS_LogEmailWriter.send_from" config setting instead');
  		return Config::inst()->get('SS_LogEmailWriter', 'send_from');
  	}
  
  	/**
  	 * Send an email to the email address set in
  	 * this writer.
  	 */
  	public function _write($event) {
  		// If no formatter set up, use the default
  		if(!$this->_formatter) {
  			$formatter = new SS_LogErrorEmailFormatter();
  			$this->setFormatter($formatter);
  		}
  
  		$formattedData = $this->_formatter->format($event);
  		$subject = $formattedData['subject'];
  		$data = $formattedData['data'];
  		$from = Config::inst()->get('SS_LogEmailWriter', 'send_from');
  
  		// override the SMTP server with a custom one if required
  		$originalSMTP = ini_get('SMTP');
  		if($this->customSmtpServer) ini_set('SMTP', $this->customSmtpServer);
  
  		// Use plain mail() implementation to avoid complexity of Mailer implementation.
  		// Only use built-in mailer when we're in test mode (to allow introspection)
  		$mailer = Email::mailer();
  		if($mailer instanceof TestMailer) {
  			$mailer->sendHTML(
  				$this->emailAddress,
  				null,
  				$subject,
  				$data,
  				null,
  				"Content-type: text/html\nFrom: " . $from
  			);
  		} else {
  			mail(
  				$this->emailAddress,
  				$subject,
  				$data,
  				"Content-type: text/html\nFrom: " . $from
  			);			
  		}
  
  		// reset the SMTP server to the original
  		if($this->customSmtpServer) ini_set('SMTP', $originalSMTP);
  	}
  
  }