mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-07 02:31:40 +09:00
Implement force_sender, exception handling, and logging with triggers
This commit is contained in:
parent
73d940cd08
commit
194ef11de7
4 changed files with 134 additions and 5 deletions
|
|
@ -12,6 +12,7 @@ class Mail
|
|||
*/
|
||||
public $message = null;
|
||||
public $driver = null;
|
||||
public $caller = '';
|
||||
protected $content_type = 'text/html';
|
||||
protected $attachments = array();
|
||||
public $errors = array();
|
||||
|
|
@ -143,8 +144,8 @@ class Mail
|
|||
*/
|
||||
public function getFrom()
|
||||
{
|
||||
$list = $this->formatAddresses($this->message->getFrom());
|
||||
return $list ? array_first($list) : null;
|
||||
$list = $this->message->getFrom();
|
||||
return $list ? array_first($this->formatAddresses($list)) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -556,6 +557,13 @@ class Mail
|
|||
*/
|
||||
public function send()
|
||||
{
|
||||
// Get caller information.
|
||||
$backtrace = debug_backtrace(0);
|
||||
if(count($backtrace) && isset($backtrace[0]['file']))
|
||||
{
|
||||
$this->caller = $backtrace[0]['file'] . ($backtrace[0]['line'] ? (' line ' . $backtrace[0]['line']) : '');
|
||||
}
|
||||
|
||||
// Reset Message-ID in case send() is called multiple times.
|
||||
$random = substr(hash('sha256', mt_rand() . microtime() . getmypid()), 0, 32);
|
||||
$sender = $this->message->getFrom(); reset($sender);
|
||||
|
|
@ -598,6 +606,16 @@ class Mail
|
|||
return $this->sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get caller information.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCaller()
|
||||
{
|
||||
return $this->caller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get errors.
|
||||
*
|
||||
|
|
|
|||
|
|
@ -254,13 +254,15 @@ class Advanced_MailerAdminController extends Advanced_Mailer
|
|||
return;
|
||||
}
|
||||
|
||||
$oAdvancedMailerController = getController('advanced_mailer');
|
||||
$sending_method = $oAdvancedMailerController->getSendingMethodForEmailAddress($recipient_email) ?: config('mail.type');
|
||||
|
||||
try
|
||||
{
|
||||
$oMail = new Rhymix\Framework\Mail();
|
||||
$oMail->setTitle('Advanced Mailer Test : ' . strtoupper(config('mail.type')));
|
||||
$oMail->setTitle('Advanced Mailer Test : ' . strtoupper($sending_method));
|
||||
$oMail->setContent('<p>This is a <b>test email</b> from Advanced Mailer.</p><p>Thank you for trying Advanced Mailer.</p>' .
|
||||
'<p>고급 메일 발송 모듈 <b>테스트</b> 메일입니다.</p><p>메일이 정상적으로 발송되고 있습니다.</p>');
|
||||
$oMail->setFrom($advanced_mailer_config->sender_email, $advanced_mailer_config->sender_name);
|
||||
$oMail->addTo($recipient_email, $recipient_name);
|
||||
$result = $oMail->send();
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,26 @@ class Advanced_MailerController extends Advanced_Mailer
|
|||
*/
|
||||
public function triggerBeforeMailSend($mail)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (!$mail->getFrom())
|
||||
{
|
||||
$mail->setFrom($config->sender_email, $config->sender_name ?: null);
|
||||
}
|
||||
elseif (toBool($config->force_sender))
|
||||
{
|
||||
$mail->setFrom($config->sender_email, $config->sender_name ?: null);
|
||||
}
|
||||
|
||||
$first_recipient = array_first_key($mail->message->getTo());
|
||||
if ($exception_driver = $this->getSendingMethodForEmailAddress($first_recipient, $config))
|
||||
{
|
||||
$driver_class = '\\Rhymix\\Framework\\Drivers\Mail\\' . $exception_driver;
|
||||
if (class_exists($driver_class))
|
||||
{
|
||||
$mail->driver = $driver_class::getInstance(config("mail.$exception_driver"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -21,6 +40,96 @@ class Advanced_MailerController extends Advanced_Mailer
|
|||
*/
|
||||
public function triggerAfterMailSend($mail)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (toBool($config->log_sent_mail) || (toBool(self::$config->log_errors) && count($mail->errors)))
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$obj->mail_srl = getNextSequence();
|
||||
$obj->mail_from = '';
|
||||
$obj->mail_to = '';
|
||||
|
||||
if ($real_sender = $mail->message->getFrom())
|
||||
{
|
||||
foreach($real_sender as $email => $name)
|
||||
{
|
||||
$obj->mail_from .= (strval($name) !== '' ? "$name <$email>" : $email) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ($real_to = $mail->message->getTo())
|
||||
{
|
||||
foreach($real_to as $email => $name)
|
||||
{
|
||||
$obj->mail_to .= (strval($name) !== '' ? "$name <$email>" : $email) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ($real_cc = $mail->message->getCc())
|
||||
{
|
||||
foreach($real_cc as $email => $name)
|
||||
{
|
||||
$obj->mail_to .= (strval($name) !== '' ? "$name <$email>" : $email) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ($real_bcc = $mail->message->getBcc())
|
||||
{
|
||||
foreach($real_bcc as $email => $name)
|
||||
{
|
||||
$obj->mail_to .= (strval($name) !== '' ? "$name <$email>" : $email) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
$obj->mail_from = trim($obj->mail_from);
|
||||
$obj->mail_to = trim($obj->mail_to);
|
||||
$obj->subject = $mail->message->getSubject();
|
||||
$obj->calling_script = $mail->getCaller();
|
||||
$obj->sending_method = strtolower(class_basename($mail->driver));
|
||||
$obj->status = !count($mail->errors) ? 'success' : 'error';
|
||||
$obj->errors = count($mail->errors) ? implode("\n", $mail->errors) : null;
|
||||
$output = executeQuery('advanced_mailer.insertLog', $obj);
|
||||
if (!$output->toBool())
|
||||
{
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an email address is on a list of exceptions.
|
||||
*
|
||||
* @param string $email
|
||||
* @param object $config (optional)
|
||||
* @return string|null
|
||||
*/
|
||||
public function getSendingMethodForEmailAddress($email, $config = null)
|
||||
{
|
||||
if (!$config)
|
||||
{
|
||||
$config = $this->getConfig();
|
||||
}
|
||||
|
||||
if (!isset($config->exceptions) || !is_array($config->exceptions) || !count($config->exceptions))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$email = Rhymix\Framework\URL::encodeIdna($email);
|
||||
|
||||
foreach ($config->exceptions as $exception)
|
||||
{
|
||||
$domains = array();
|
||||
foreach ($exception['domains'] as $domain)
|
||||
{
|
||||
$domains[] = preg_quote($domain, '/');
|
||||
}
|
||||
if (count($domains) && preg_match('/\b(?:' . implode('|', $domains) . ')$/i', $email))
|
||||
{
|
||||
return $exception['method'];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@
|
|||
<td class="nowr">{htmlspecialchars($val->subject)}</td>
|
||||
<td class="nowr">
|
||||
{@ if($val->sending_method === 'mail') $val->sending_method = 'mailfunction'}
|
||||
{isset($sending_methods[$val->sending_method]['name']) ? $sending_methods[$val->sending_method]['name'] : $val->sending_method}
|
||||
{strval(isset($sending_methods[$val->sending_method]['name']) ? $sending_methods[$val->sending_method]['name'] : $val->sending_method)}
|
||||
</td>
|
||||
<td class="nowr">{(zdate($val->regdate, "Y-m-d\nH:i:s"))}</td>
|
||||
<td class="nowr">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue