MDL-41196 phpmailer: fixed unit test detection
[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
DP
19 *
20 * @package moodle
21 * @subpackage lib
22 * @author Dan Poltawski <talktodan@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
8843e0bb
DP
24 */
25
0747daba
PS
26defined('MOODLE_INTERNAL') || die();
27
8843e0bb
DP
28// PLEASE NOTE: we use the phpmailer class _unmodified_
29// through the joys of OO. Distros are free to use their stock
30// version of this file.
31require_once($CFG->libdir.'/phpmailer/class.phpmailer.php');
32
33/**
34 * Moodle Customised version of the PHPMailer class
35 *
36 * This class extends the stock PHPMailer class
37 * in order to make sensible configuration choices,
38 * and behave in a way which is friendly to moodle.
39 *
40 * @copyright 2009 Dan Poltawski <talktodan@gmail.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 * @since Moodle 2.0
43 */
44class moodle_phpmailer extends PHPMailer {
45
46 /**
47 * Constructor - creates an instance of the PHPMailer class
48 * with Moodle defaults.
49 */
50 public function __construct(){
51 global $CFG;
52 $this->Version = 'Moodle '.$CFG->version; // mailer version
53 $this->PluginDir = $CFG->libdir.'/phpmailer/'; // plugin directory (eg smtp plugin)
54 $this->CharSet = 'UTF-8';
55 }
56
57 /**
58 * Extended AddCustomHeader function in order to stop duplicate
59 * message-ids
60 * http://tracker.moodle.org/browse/MDL-3681
61 */
62 public function AddCustomHeader($custom_header) {
63 if(preg_match('/message-id:(.*)/i', $custom_header, $matches)){
64 $this->MessageID = $matches[1];
65 return true;
66 }else{
67 return parent::AddCustomHeader($custom_header);
68 }
69 }
70
71 /**
2f1e464a 72 * Use internal moodles own core_text to encode mimeheaders.
8843e0bb
DP
73 * Fall back to phpmailers inbuilt functions if not
74 */
0747daba 75 public function EncodeHeader($str, $position = 'text') {
2f1e464a 76 $encoded = core_text::encode_mimeheader($str, $this->CharSet);
8843e0bb
DP
77 if ($encoded !== false) {
78 $encoded = str_replace("\n", $this->LE, $encoded);
79 if ($position == 'phrase') {
80 return ("\"$encoded\"");
81 }
82 return $encoded;
83 }
84
85 return parent::EncodeHeader($str, $position);
86 }
87
88 /**
89 * Replaced function to fix tz bug:
90 * http://tracker.moodle.org/browse/MDL-12596
8843e0bb 91 */
f180f99a 92 public static function RFCDate() {
8843e0bb
DP
93 $tz = date('Z');
94 $tzs = ($tz < 0) ? '-' : '+';
95 $tz = abs($tz);
96 $tz = (($tz - ($tz%3600) )/3600)*100 + ($tz%3600)/60; // fixed tz bug
97 $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
98
99 return $result;
100 }
e83b7a20
DM
101
102 /**
103 * This is a temporary replacement of the parent::EncodeQP() that does not
104 * call quoted_printable_encode() even if it is available. See MDL-23240 for details
105 *
106 * @see parent::EncodeQP() for full documentation
107 */
108 public function EncodeQP($string, $line_max = 76, $space_conv = false) {
109 //if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
110 // return quoted_printable_encode($string);
111 //}
112 $filters = stream_get_filters();
113 if (!in_array('convert.*', $filters)) { //Got convert stream filter?
114 return $this->EncodeQPphp($string, $line_max, $space_conv); //Fall back to old implementation
115 }
116 $fp = fopen('php://temp/', 'r+');
117 $string = preg_replace('/\r\n?/', $this->LE, $string); //Normalise line breaks
118 $params = array('line-length' => $line_max, 'line-break-chars' => $this->LE);
119 $s = stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_READ, $params);
120 fputs($fp, $string);
121 rewind($fp);
122 $out = stream_get_contents($fp);
123 stream_filter_remove($s);
124 $out = preg_replace('/^\./m', '=2E', $out); //Encode . if it is first char on a line, workaround for bug in Exchange
125 fclose($fp);
126 return $out;
127 }
1aba6b2b 128
93564564
SH
129 /**
130 * Sends this mail.
131 *
132 * This function has been overridden to facilitate unit testing.
133 *
134 * @return bool
135 */
1aba6b2b
AN
136 protected function PostSend() {
137 // Now ask phpunit if it wants to catch this message.
ba214fa5 138 if (PHPUNIT_TEST && phpunit_util::is_redirecting_messages()) {
1aba6b2b
AN
139 $mail = new stdClass();
140 $mail->header = $this->MIMEHeader;
141 $mail->body = $this->MIMEBody;
142 $mail->subject = $this->Subject;
143 $mail->from = $this->From;
144 phpunit_util::phpmailer_sent($mail);
145 return true;
146 } else {
147 return parent::PostSend();
148 }
149 }
8843e0bb 150}