MDL-57474 mailer: Make sure that our exlicit MessageID is trimmed
[moodle.git] / lib / phpmailer / moodle_phpmailer.php
CommitLineData
8843e0bb 1<?php
0747daba
PS
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
8843e0bb
DP
16
17/**
0747daba 18 * Customised version of phpmailer for Moodle
8843e0bb 19 *
29bf99fd 20 * @package core
8843e0bb
DP
21 * @author Dan Poltawski <talktodan@gmail.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8843e0bb
DP
23 */
24
0747daba
PS
25defined('MOODLE_INTERNAL') || die();
26
8843e0bb
DP
27// PLEASE NOTE: we use the phpmailer class _unmodified_
28// through the joys of OO. Distros are free to use their stock
29// version of this file.
29bf99fd 30// NOTE: do not rely on phpmailer autoloader for performance reasons.
8843e0bb 31require_once($CFG->libdir.'/phpmailer/class.phpmailer.php');
29bf99fd 32require_once($CFG->libdir.'/phpmailer/class.smtp.php');
8843e0bb
DP
33
34/**
35 * Moodle Customised version of the PHPMailer class
36 *
37 * This class extends the stock PHPMailer class
38 * in order to make sensible configuration choices,
39 * and behave in a way which is friendly to moodle.
40 *
41 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 * @since Moodle 2.0
44 */
45class moodle_phpmailer extends PHPMailer {
46
47 /**
48 * Constructor - creates an instance of the PHPMailer class
49 * with Moodle defaults.
50 */
51 public function __construct(){
52 global $CFG;
53 $this->Version = 'Moodle '.$CFG->version; // mailer version
8843e0bb 54 $this->CharSet = 'UTF-8';
074b3dce
MS
55 // MDL-52637: Disable the automatic TLS encryption added in v5.2.10 (9da56fc1328a72aa124b35b738966315c41ef5c6).
56 $this->SMTPAutoTLS = false;
6226cc30 57
2108ac8a
DN
58 if (!empty($CFG->smtpauthtype)) {
59 $this->AuthType = $CFG->smtpauthtype;
60 }
61
6226cc30
PS
62 // Some MTAs may do double conversion of LF if CRLF used, CRLF is required line ending in RFC 822bis.
63 if (isset($CFG->mailnewline) and $CFG->mailnewline == 'CRLF') {
64 $this->LE = "\r\n";
65 } else {
66 $this->LE = "\n";
67 }
8843e0bb
DP
68 }
69
70 /**
71 * Extended AddCustomHeader function in order to stop duplicate
72 * message-ids
73 * http://tracker.moodle.org/browse/MDL-3681
74 */
29bf99fd
PS
75 public function addCustomHeader($custom_header, $value = null) {
76 if ($value === null and preg_match('/message-id:(.*)/i', $custom_header, $matches)) {
657fa9db 77 $this->MessageID = trim($matches[1]);
8843e0bb 78 return true;
29bf99fd 79 } else if ($value !== null and strcasecmp($custom_header, 'message-id') === 0) {
657fa9db 80 $this->MessageID = trim($value);
29bf99fd
PS
81 return true;
82 } else {
83 return parent::addCustomHeader($custom_header, $value);
8843e0bb
DP
84 }
85 }
86
87 /**
2f1e464a 88 * Use internal moodles own core_text to encode mimeheaders.
8843e0bb
DP
89 * Fall back to phpmailers inbuilt functions if not
90 */
29bf99fd 91 public function encodeHeader($str, $position = 'text') {
2f1e464a 92 $encoded = core_text::encode_mimeheader($str, $this->CharSet);
8843e0bb 93 if ($encoded !== false) {
29bf99fd 94 if ($position === 'phrase') {
ed63718c
MG
95 // Escape special symbols in each line in the encoded string, join back together and enclose in quotes.
96 $chunks = preg_split("/\\n/", $encoded);
97 $chunks = array_map(function($chunk) {
98 return addcslashes($chunk, "\0..\37\177\\\"");
99 }, $chunks);
100 return '"' . join($this->LE, $chunks) . '"';
8843e0bb 101 }
ed63718c 102 return str_replace("\n", $this->LE, $encoded);
8843e0bb
DP
103 }
104
29bf99fd 105 return parent::encodeHeader($str, $position);
8843e0bb
DP
106 }
107
108 /**
109 * Replaced function to fix tz bug:
110 * http://tracker.moodle.org/browse/MDL-12596
8843e0bb 111 */
29bf99fd 112 public static function rfcDate() {
8843e0bb
DP
113 $tz = date('Z');
114 $tzs = ($tz < 0) ? '-' : '+';
115 $tz = abs($tz);
116 $tz = (($tz - ($tz%3600) )/3600)*100 + ($tz%3600)/60; // fixed tz bug
117 $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
118
119 return $result;
120 }
e83b7a20
DM
121
122 /**
123 * This is a temporary replacement of the parent::EncodeQP() that does not
124 * call quoted_printable_encode() even if it is available. See MDL-23240 for details
125 *
126 * @see parent::EncodeQP() for full documentation
127 */
29bf99fd 128 public function encodeQP($string, $line_max = 76) {
e83b7a20
DM
129 //if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
130 // return quoted_printable_encode($string);
131 //}
132 $filters = stream_get_filters();
133 if (!in_array('convert.*', $filters)) { //Got convert stream filter?
29bf99fd 134 return parent::encodeQP($string, $line_max); //Fall back to old implementation
e83b7a20
DM
135 }
136 $fp = fopen('php://temp/', 'r+');
137 $string = preg_replace('/\r\n?/', $this->LE, $string); //Normalise line breaks
138 $params = array('line-length' => $line_max, 'line-break-chars' => $this->LE);
139 $s = stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_READ, $params);
140 fputs($fp, $string);
141 rewind($fp);
142 $out = stream_get_contents($fp);
143 stream_filter_remove($s);
144 $out = preg_replace('/^\./m', '=2E', $out); //Encode . if it is first char on a line, workaround for bug in Exchange
145 fclose($fp);
ccff58da 146 return $this->fixEOL($out);
e83b7a20 147 }
1aba6b2b 148
93564564
SH
149 /**
150 * Sends this mail.
151 *
152 * This function has been overridden to facilitate unit testing.
153 *
154 * @return bool
155 */
29bf99fd 156 public function postSend() {
1aba6b2b 157 // Now ask phpunit if it wants to catch this message.
90930acf
PS
158 if (PHPUNIT_TEST) {
159 if (!phpunit_util::is_redirecting_phpmailer()) {
f007e899 160 debugging('Unit tests must not send real emails! Use $this->redirectEmails()');
90930acf
PS
161 return true;
162 }
1aba6b2b
AN
163 $mail = new stdClass();
164 $mail->header = $this->MIMEHeader;
165 $mail->body = $this->MIMEBody;
166 $mail->subject = $this->Subject;
167 $mail->from = $this->From;
90930acf 168 $mail->to = $this->to[0][0];
1aba6b2b
AN
169 phpunit_util::phpmailer_sent($mail);
170 return true;
171 } else {
29bf99fd 172 return parent::postSend();
1aba6b2b
AN
173 }
174 }
8843e0bb 175}