yf_send_mail classMail sender.
| File | plugins/email/classes/yf_send_mail.class.php |
|---|---|
| Use | _class('send_mail') |
A site sets them in its config with $PROJECT_CONF['send_mail']['NAME'] = ...;
| Property | Default | Description |
|---|---|---|
$ALLOW_ATTACHMENTSbool |
true |
|
$DB_LOG_ENVbool |
false |
|
$DEBUG_TEST_ADDRESSstring |
'' |
|
$DEBUG_TEST_SEND_BULKbool |
true |
|
$DEFAULT_CHARSETstring |
'utf-8' |
|
$DEFAULT_MAILER_NAMEstring |
'YF PHP Mailer' |
|
$DRIVERstring |
'phpmailer' |
Default mail driver |
$DRIVERS_WITH_SMTP_SUPPORTcallable |
['phpmailer'] |
|
$LOG_EMAILSbool |
true |
|
$MAIL_DEBUGbool |
false |
|
$MAIL_DEBUG_ERRORbool |
false |
|
$ON_AFTER_SENDcallable |
null |
|
$ON_BEFORE_SENDcallable |
'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_FROMbool |
true |
Replaces 'From' with['smtp_from'] |
$SMTP_OPTIONSarray|callable |
|
SMTP specific options |
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;
}
_ hooks and helpers; not routable as page actionspublic function __call($name, $args)
Catch missing method call.
public function __call($name, $args)
{
return main()->extend_call($this, $name, $args);
}
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;
}
}