├── README.md ├── composer.json ├── wonolog.php └── src └── Handler └── WordpressMailerHandler.php /README.md: -------------------------------------------------------------------------------- 1 | # outlandish-wonolog 2 | MU-plugin to setup Wonolog for Outlandish projects 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "outlandish/wonolog", 3 | "version": "0.2.0", 4 | "description": "Wonolog configuration file for Outlandish projects.", 5 | "type": "wordpress-muplugin", 6 | 7 | "autoload": { 8 | "psr-4": { 9 | "Outlandish\\Wonolog\\": "src" 10 | } 11 | }, 12 | 13 | "require": { 14 | "inpsyde/wonolog": "^1.0" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /wonolog.php: -------------------------------------------------------------------------------- 1 | setContentType('text/html')->setFormatter(new \Monolog\Formatter\HtmlFormatter()); 25 | 26 | $fingers = new FingersCrossedHandler( 27 | $email, 28 | new ErrorLevelActivationStrategy(Logger::ERROR), 29 | 10, 30 | true, 31 | true, 32 | Logger::WARNING 33 | ); 34 | 35 | 36 | 37 | $wonolog_controller = Wonolog\bootstrap(); 38 | $wonolog_controller->use_handler($fingers); 39 | -------------------------------------------------------------------------------- /src/Handler/WordpressMailerHandler.php: -------------------------------------------------------------------------------- 1 | 14 | * @link https://outlandish.com 15 | */ 16 | class WordpressMailerHandler extends MailHandler 17 | { 18 | 19 | /** 20 | * The email addresses to which the message will be sent 21 | * @var array 22 | */ 23 | protected $to; 24 | 25 | /** 26 | * The subject of the email 27 | * @var string 28 | */ 29 | protected $subject; 30 | 31 | /** 32 | * Optional headers for the message 33 | * @var array 34 | */ 35 | protected $headers = array(); 36 | 37 | /** 38 | * Optional parameters for the message 39 | * @var array 40 | */ 41 | protected $parameters = array(); 42 | 43 | /** 44 | * The wordwrap length for the message 45 | * @var int 46 | */ 47 | protected $maxColumnWidth; 48 | 49 | /** 50 | * The Content-type for the message 51 | * @var string 52 | */ 53 | protected $contentType = 'text/plain'; 54 | 55 | /** 56 | * The encoding for the message 57 | * @var string 58 | */ 59 | protected $encoding = 'utf-8'; 60 | 61 | /** 62 | * @param string|array $to The receiver of the mail 63 | * @param string $subject The subject of the mail 64 | * @param int $level The minimum logging level at which this handler will be triggered 65 | * @param bool $bubble Whether the messages that are handled can bubble up the stack or not 66 | * @param int $maxColumnWidth The maximum column width that the message lines will have 67 | */ 68 | public function __construct($to, $subject, $level = Logger::ERROR, $bubble = true, $maxColumnWidth = 70) 69 | { 70 | parent::__construct($level, $bubble); 71 | $this->to = is_array($to) ? $to : array($to); 72 | $this->subject = $subject; 73 | $this->maxColumnWidth = $maxColumnWidth; 74 | } 75 | 76 | /** 77 | * Add headers to the message 78 | * 79 | * @param string|array $headers Custom added headers 80 | * @return self 81 | */ 82 | public function addHeader($headers) 83 | { 84 | foreach ((array) $headers as $header) { 85 | if (strpos($header, "\n") !== false || strpos($header, "\r") !== false) { 86 | throw new \InvalidArgumentException('Headers can not contain newline characters for security reasons'); 87 | } 88 | $this->headers[] = $header; 89 | } 90 | 91 | return $this; 92 | } 93 | 94 | /** 95 | * Add parameters to the message 96 | * 97 | * @param string|array $parameters Custom added parameters 98 | * @return self 99 | */ 100 | public function addParameter($parameters) 101 | { 102 | $this->parameters = array_merge($this->parameters, (array) $parameters); 103 | 104 | return $this; 105 | } 106 | 107 | /** 108 | * {@inheritdoc} 109 | */ 110 | protected function send($content, array $records) 111 | { 112 | $content = wordwrap($content, $this->maxColumnWidth); 113 | $headers = ltrim(implode("\r\n", $this->headers) . "\r\n", "\r\n"); 114 | $headers .= 'Content-type: ' . $this->getContentType() . '; charset=' . $this->getEncoding() . "\r\n"; 115 | if ($this->getContentType() == 'text/html' && false === strpos($headers, 'MIME-Version:')) { 116 | $headers .= 'MIME-Version: 1.0' . "\r\n"; 117 | } 118 | 119 | $subject = $this->subject; 120 | if ($records) { 121 | $subjectFormatter = new LineFormatter($this->subject); 122 | $subject = $subjectFormatter->format($this->getHighestRecord($records)); 123 | } 124 | 125 | $parameters = implode(' ', $this->parameters); 126 | foreach ($this->to as $to) { 127 | wp_mail($to, $subject, $content, $headers, $parameters); 128 | } 129 | } 130 | 131 | /** 132 | * @return string $contentType 133 | */ 134 | public function getContentType() 135 | { 136 | return $this->contentType; 137 | } 138 | 139 | /** 140 | * @return string $encoding 141 | */ 142 | public function getEncoding() 143 | { 144 | return $this->encoding; 145 | } 146 | 147 | /** 148 | * @param string $contentType The content type of the email - Defaults to text/plain. Use text/html for HTML 149 | * messages. 150 | * @return self 151 | */ 152 | public function setContentType($contentType) 153 | { 154 | if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) { 155 | throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection'); 156 | } 157 | 158 | $this->contentType = $contentType; 159 | 160 | return $this; 161 | } 162 | 163 | /** 164 | * @param string $encoding 165 | * @return self 166 | */ 167 | public function setEncoding($encoding) 168 | { 169 | if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) { 170 | throw new \InvalidArgumentException('The encoding can not contain newline characters to prevent email header injection'); 171 | } 172 | 173 | $this->encoding = $encoding; 174 | 175 | return $this; 176 | } 177 | } 178 | --------------------------------------------------------------------------------