Back to Factory class

Method createMailer

protected static \Joomla\CMS\Mail\Mail
createMailer
()
Create a mailer object
Returns
  • \Joomla\CMS\Mail\Mail object
Since
  • 1.7.0
-
  • \Joomla\CMS\Mail\Mail
Class: Factory
Project: Joomla

Method createMailer - Source code

/**
 * Create a mailer object
 *
 * @return  Mail object
 *
 * @see     Mail
 * @since   1.7.0
 */
protected static function createMailer()
{
    $conf = self::getConfig();
    $smtpauth = $conf->get('smtpauth') == 0 ? null : 1;
    $smtpuser = $conf->get('smtpuser');
    $smtppass = $conf->get('smtppass');
    $smtphost = $conf->get('smtphost');
    $smtpsecure = $conf->get('smtpsecure');
    $smtpport = $conf->get('smtpport');
    $mailfrom = $conf->get('mailfrom');
    $fromname = $conf->get('fromname');
    $mailer = $conf->get('mailer');
    // Create a Mail object
    $mail = Mail::getInstance();
    // Clean the email address
    $mailfrom = MailHelper::cleanLine($mailfrom);
    // Set default sender without Reply-to if the mailfrom is a valid address
    if (MailHelper::isEmailAddress($mailfrom)) {
        // Wrap in try/catch to catch phpmailerExceptions if it is throwing them
        try {
            // Check for a false return value if exception throwing is disabled
            if ($mail->setFrom($mailfrom, MailHelper::cleanLine($fromname), false) === false) {
                Log::add(__METHOD__ . '() could not set the sender data.', Log::WARNING, 'mail');
            }
        } catch (phpmailerException $e) {
            Log::add(__METHOD__ . '() could not set the sender data.', Log::WARNING, 'mail');
        }
    }
    // Default mailer is to use PHP's mail function
    switch ($mailer) {
        case 'smtp':
            $mail->useSmtp($smtpauth, $smtphost, $smtpuser, $smtppass, $smtpsecure, $smtpport);
            break;
        case 'sendmail':
            $mail->isSendmail();
            break;
        default:
            $mail->isMail();
            break;
    }
    return $mail;
}