/** * Get mailer instance, enable buffering, flush buffer or disable buffering. * * @param string $action 'get', 'buffer', 'close' or 'flush' * @return moodle_phpmailer|null mailer instance if 'get' used or nothing */ function get_mailer($action = 'get') { global $CFG; /** @var moodle_phpmailer $mailer */ static $mailer = null; static $counter = 0; if (!isset($CFG->smtpmaxbulk)) { $CFG->smtpmaxbulk = 1; } if ($action == 'get') { $prevkeepalive = false; if (isset($mailer) and $mailer->Mailer == 'smtp') { if ($counter < $CFG->smtpmaxbulk and !$mailer->isError()) { $counter++; // Reset the mailer. $mailer->Priority = 3; $mailer->CharSet = 'UTF-8'; // Our default. $mailer->ContentType = "text/plain"; $mailer->Encoding = "8bit"; $mailer->From = "root@localhost"; $mailer->FromName = "Root User"; $mailer->Sender = ""; $mailer->Subject = ""; $mailer->Body = ""; $mailer->AltBody = ""; $mailer->ConfirmReadingTo = ""; $mailer->clearAllRecipients(); $mailer->clearReplyTos(); $mailer->clearAttachments(); $mailer->clearCustomHeaders(); return $mailer; } $prevkeepalive = $mailer->SMTPKeepAlive; get_mailer('flush'); } require_once $CFG->libdir . '/phpmailer/moodle_phpmailer.php'; $mailer = new moodle_phpmailer(); $counter = 1; if ($CFG->smtphosts == 'qmail') { // Use Qmail system. $mailer->isQmail(); } else { if (empty($CFG->smtphosts)) { // Use PHP mail() = sendmail. $mailer->isMail(); } else { // Use SMTP directly. $mailer->isSMTP(); if (!empty($CFG->debugsmtp)) { $mailer->SMTPDebug = true; } // Specify main and backup servers. $mailer->Host = $CFG->smtphosts; // Specify secure connection protocol. $mailer->SMTPSecure = $CFG->smtpsecure; // Use previous keepalive. $mailer->SMTPKeepAlive = $prevkeepalive; if ($CFG->smtpuser) { // Use SMTP authentication. $mailer->SMTPAuth = true; $mailer->Username = $CFG->smtpuser; $mailer->Password = $CFG->smtppass; } } } return $mailer; } $nothing = null; // Keep smtp session open after sending. if ($action == 'buffer') { if (!empty($CFG->smtpmaxbulk)) { get_mailer('flush'); $m = get_mailer(); if ($m->Mailer == 'smtp') { $m->SMTPKeepAlive = true; } } return $nothing; } // Close smtp session, but continue buffering. if ($action == 'flush') { if (isset($mailer) and $mailer->Mailer == 'smtp') { if (!empty($mailer->SMTPDebug)) { echo '<pre>' . "\n"; } $mailer->SmtpClose(); if (!empty($mailer->SMTPDebug)) { echo '</pre>'; } } return $nothing; } // Close smtp session, do not buffer anymore. if ($action == 'close') { if (isset($mailer) and $mailer->Mailer == 'smtp') { get_mailer('flush'); $mailer->SMTPKeepAlive = false; } $mailer = null; // Better force new instance. return $nothing; } }
/** * Tests the static encode_mimeheader method. * This also tests method moodle_phpmailer::encodeHeader that calls core_text::encode_mimeheader */ public function test_encode_mimeheader() { global $CFG; require_once $CFG->libdir . '/phpmailer/moodle_phpmailer.php'; $mailer = new moodle_phpmailer(); // Encode short string with non-latin characters. $str = "Žluťoučký koníček"; $encodedstr = '=?utf-8?B?xb1sdcWlb3XEjWvDvSBrb27DrcSNZWs=?='; $this->assertSame($encodedstr, core_text::encode_mimeheader($str)); $this->assertSame($encodedstr, $mailer->encodeHeader($str)); $this->assertSame('"' . $encodedstr . '"', $mailer->encodeHeader($str, 'phrase')); // Encode short string without non-latin characters. Make sure the quotes are escaped in quoted email headers. $latinstr = 'text"with quotes'; $this->assertSame($latinstr, core_text::encode_mimeheader($latinstr)); $this->assertSame($latinstr, $mailer->encodeHeader($latinstr)); $this->assertSame('"text\\"with quotes"', $mailer->encodeHeader($latinstr, 'phrase')); // Encode long string without non-latin characters. $longlatinstr = 'This is a very long text that still should not be split into several lines in the email headers because ' . 'it does not have any non-latin characters. The "quotes" and \\backslashes should be escaped only if it\'s a part of email address'; $this->assertSame($longlatinstr, core_text::encode_mimeheader($longlatinstr)); $this->assertSame($longlatinstr, $mailer->encodeHeader($longlatinstr)); $longlatinstrwithslash = preg_replace(['/\\\\/', "/\"/"], ['\\\\\\', '\\"'], $longlatinstr); $this->assertSame('"' . $longlatinstrwithslash . '"', $mailer->encodeHeader($longlatinstr, 'phrase')); // Encode long string with non-latin characters. $longstr = "Неопознанная ошибка в файле C:\\tmp\\: \"Не пользуйтесь виндоуз\""; $encodedlongstr = "=?utf-8?B?0J3QtdC+0L/QvtC30L3QsNC90L3QsNGPINC+0YjQuNCx0LrQsCDQsiDRhNCw?=\n =?utf-8?B?0LnQu9C1IEM6XHRtcFw6ICLQndC1INC/0L7Qu9GM0LfRg9C50YLQtdGB?=\n =?utf-8?B?0Ywg0LLQuNC90LTQvtGD0Lci?="; $this->assertSame($encodedlongstr, $mailer->encodeHeader($longstr)); $this->assertSame('"' . $encodedlongstr . '"', $mailer->encodeHeader($longstr, 'phrase')); }
/** * Get mailer instance, enable buffering, flush buffer or disable buffering. * * @global object * @param string $action 'get', 'buffer', 'close' or 'flush' * @return object|null mailer instance if 'get' used or nothing */ function get_mailer($action = 'get') { global $CFG; static $mailer = null; static $counter = 0; if (!isset($CFG->smtpmaxbulk)) { $CFG->smtpmaxbulk = 1; } if ($action == 'get') { $prevkeepalive = false; if (isset($mailer) and $mailer->Mailer == 'smtp') { if ($counter < $CFG->smtpmaxbulk and !$mailer->IsError()) { $counter++; // reset the mailer $mailer->Priority = 3; $mailer->CharSet = 'UTF-8'; // our default $mailer->ContentType = "text/plain"; $mailer->Encoding = "8bit"; $mailer->From = "root@localhost"; $mailer->FromName = "Root User"; $mailer->Sender = ""; $mailer->Subject = ""; $mailer->Body = ""; $mailer->AltBody = ""; $mailer->ConfirmReadingTo = ""; $mailer->ClearAllRecipients(); $mailer->ClearReplyTos(); $mailer->ClearAttachments(); $mailer->ClearCustomHeaders(); return $mailer; } $prevkeepalive = $mailer->SMTPKeepAlive; get_mailer('flush'); } include_once $CFG->libdir . '/phpmailer/moodle_phpmailer.php'; $mailer = new moodle_phpmailer(); $counter = 1; $mailer->Version = 'Moodle ' . $CFG->version; // mailer version $mailer->PluginDir = $CFG->libdir . '/phpmailer/'; // plugin directory (eg smtp plugin) $mailer->CharSet = 'UTF-8'; // some MTAs may do double conversion of LF if CRLF used, CRLF is required line ending in RFC 822bis if (isset($CFG->mailnewline) and $CFG->mailnewline == 'CRLF') { $mailer->LE = "\r\n"; } else { $mailer->LE = "\n"; } if ($CFG->smtphosts == 'qmail') { $mailer->IsQmail(); // use Qmail system } else { if (empty($CFG->smtphosts)) { $mailer->IsMail(); // use PHP mail() = sendmail } else { $mailer->IsSMTP(); // use SMTP directly if (!empty($CFG->debugsmtp)) { $mailer->SMTPDebug = true; } $mailer->Host = $CFG->smtphosts; // specify main and backup servers $mailer->SMTPKeepAlive = $prevkeepalive; // use previous keepalive if ($CFG->smtpuser) { // Use SMTP authentication $mailer->SMTPAuth = true; $mailer->Username = $CFG->smtpuser; $mailer->Password = $CFG->smtppass; } } } return $mailer; } $nothing = null; // keep smtp session open after sending if ($action == 'buffer') { if (!empty($CFG->smtpmaxbulk)) { get_mailer('flush'); $m = get_mailer(); if ($m->Mailer == 'smtp') { $m->SMTPKeepAlive = true; } } return $nothing; } // close smtp session, but continue buffering if ($action == 'flush') { if (isset($mailer) and $mailer->Mailer == 'smtp') { if (!empty($mailer->SMTPDebug)) { echo '<pre>' . "\n"; } $mailer->SmtpClose(); if (!empty($mailer->SMTPDebug)) { echo '</pre>'; } } return $nothing; } // close smtp session, do not buffer anymore if ($action == 'close') { if (isset($mailer) and $mailer->Mailer == 'smtp') { get_mailer('flush'); $mailer->SMTPKeepAlive = false; } $mailer = null; // better force new instance return $nothing; } }
/** * Send confirmation email when the cron has send all the coupons * * @param int $ownerid * @param int $timecreated * @return bool */ public static final function confirm_coupons_sent($ownerid, $timecreated) { global $CFG, $DB; require_once $CFG->libdir . '/phpmailer/moodle_phpmailer.php'; $owner = $DB->get_record('user', array('id' => $ownerid)); $supportuser = \core_user::get_support_user(); $mailcontent = get_string("confirm_coupons_sent_body", 'block_coupon', array('timecreated' => date('Y-m-d', $timecreated))); // Send. $phpmailer = new \moodle_phpmailer(); $phpmailer->Body = $mailcontent; $phpmailer->AltBody = strip_tags($mailcontent); $phpmailer->From = $supportuser->email; $phpmailer->FromName = trim($supportuser->firstname . ' ' . $supportuser->lastname); $phpmailer->IsHTML(true); $phpmailer->Subject = get_string('confirm_coupons_sent_subject', 'block_coupon'); $phpmailer->AddAddress($owner->email); return $phpmailer->Send(); }