yf_send_mail class

Mail sender.

Fileplugins/email/classes/yf_send_mail.class.php
Use_class('send_mail')

Public properties

A site sets them in its config with $PROJECT_CONF['send_mail']['NAME'] = ...;

PropertyDefaultDescription
$ALLOW_ATTACHMENTS
bool
true
$DB_LOG_ENV
bool
false
$DEBUG_TEST_ADDRESS
string
''
$DEBUG_TEST_SEND_BULK
bool
true
$DEFAULT_CHARSET
string
'utf-8'
$DEFAULT_MAILER_NAME
string
'YF PHP Mailer'
$DRIVER
string
'phpmailer' Default mail driver
$DRIVERS_WITH_SMTP_SUPPORT
callable
['phpmailer']
$LOG_EMAILS
bool
true
$MAIL_DEBUG
bool
false
$MAIL_DEBUG_ERROR
bool
false
$ON_AFTER_SEND
callable
null
$ON_BEFORE_SEND
callable
'Example Mailer'; $mail->Version = '2.0.0'; $mail->ReturnPath = $params['from_mail']; $mail->AddReplyTo($params['from_mail']); $mail->SMTPOptions = [... Callback example: 'ON_BEFORE_SEND' => function($mail, $params) { $mail->XMailer = 'Example Mailer'; $mail->Version = '2.0.0'; $mail->ReturnPath = $params['from_mail']; $mail->AddReplyTo($params['from_mail']); $mail->SMTPOptions = [ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true, ] ]; },
$REPLACE_FIELD_FROM
bool
true Replaces 'From' with['smtp_from']
$SMTP_OPTIONS
array|callable
['smtp_host' => '', 'smtp_port' => '25', 'smtp_user_name' => '', 'smtp_password' => '', 'smtp_auth' => '', 'smtp_from' => '', ... (7 items)]
array (
  'smtp_host' => '',
  'smtp_port' => '25',
  'smtp_user_name' => '',
  'smtp_password' => '',
  'smtp_auth' => '',
  'smtp_from' => '',
  'smtp_secure' => '',
)
SMTP specific options

Public methods 3

send

public function send($params = [])

Send emails with attachments with DEBUG ability.

    public function send($params = [])
    {
        $name_to = $email_to = null;
        $driver = strtolower(($params['driver'] ?? null) ?: $this->DRIVER);
        if (DEBUG_MODE || $this->LOG_EMAILS) {
            $time_start = microtime(true);
        }
        if ( ! isset($params['on_before_send']) && is_callable($this->ON_BEFORE_SEND)) {
            $params['on_before_send'] = $this->ON_BEFORE_SEND;
        }
        if ( ! isset($params['on_after_send']) && is_callable($this->ON_AFTER_SEND)) {
            $params['on_after_send'] = $this->ON_AFTER_SEND;
        }
        if ($this->MAIL_DEBUG_ERROR && empty($params['email_to'])) {
            trigger_error('SEND_MAIL: Missing \'To\' email address', E_USER_WARNING);
            return false;
        }
        if ($this->MAIL_DEBUG && $this->DEBUG_TEST_ADDRESS) {
            $debug_mail = $this->DEBUG_TEST_ADDRESS;
            if ($driver == 'phpmailer' && is_array($params['email_to'])) {
                $mails = [];
                $debug_name = '';
                foreach ($params['email_to'] as $name => $email) {
                    $debug_name = '(debug: ' . $name . ' - ' . $email . ')';
                    $mails[$debug_name] = $debug_mail;
                }
                if ($this->DEBUG_TEST_SEND_BULK) {
                    $params['email_to'] = $mails;
                } else {
                    $params['email_to'] = $debug_mail;
                    $params['name_to'] = implode(' - ', array_keys($mails));
                }
            } else {
                $debug_name = '(debug: ' . ($params['name_to'] ?? '') . ' - ' . implode(', ', (array) $params['email_to']) . ')';
                $params['email_to'] = $debug_mail;
                $params['name_to'] = $debug_name;
            }
        }
        $smtp = [];
        $driver_supports_smtp = in_array($driver, $this->DRIVERS_WITH_SMTP_SUPPORT);
        if ($driver_supports_smtp) {
            if ( ! $smtp && isset($params['smtp']) && is_callable($params['smtp'])) {
                $callable = $params['smtp'];
                $smtp = $callable($params, $this);
                unset($callable);
            }
            if ( ! $smtp && isset($params['smtp']['smtp_host']) && $params['smtp']['smtp_host']) {
                $smtp = $params['smtp'];
            }
            if ( ! $smtp && isset($this->SMTP_OPTIONS) && is_callable($this->SMTP_OPTIONS)) {
                $callable = $this->SMTP_OPTIONS;
                $smtp = $callable($params, $this);
                unset($callable);
            }
            if ( ! $smtp && isset($this->SMTP_OPTIONS['smtp_host']) && $this->SMTP_OPTIONS['smtp_host']) {
                $smtp = $this->SMTP_OPTIONS;
            }
            if ($this->REPLACE_FIELD_FROM && $smtp['smtp_from_mail']) {
                $params['email_from'] = $smtp['smtp_from_mail'];
                $params['name_from'] = ($smtp['smtp_from_name'] ?? null) ?: $params['name_from'];
            }
        }
        $params['smtp'] = $smtp;

        // Go send with selected driver
        $error_message = '';
        $result = _class('mail_driver_' . $driver, 'classes/mail/')->send($params, $error_message);

        return $result;
    }

Methods starting with _ hooks and helpers; not routable as page actions

__call

public function __call($name, $args)

Catch missing method call.

    public function __call($name, $args)
    {
        return main()->extend_call($this, $name, $args);
    }

_init

public function _init()

Module constructor.

    public function _init()
    {
        define('LOG_MAIL_PATH', PROJECT_PATH . 'logs/email/');
        $mail_debug = conf('mail_debug');
        if (isset($mail_debug)) {
            $this->MAIL_DEBUG = $mail_debug;
        }
        $test_mail = conf('test_mail');
        $test_mail && $this->DEBUG_TEST_ADDRESS = $test_mail;
        $log_emails = conf('log_emails');
        $log_emails && $this->LOG_EMAILS = $log_emails;
        // Backwards compatibility
        if (isset($this->USE_MAILER)) {
            $this->DRIVER = $this->USE_MAILER;
        }
    }