Example #1
0
/**
 * 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;
    }
}