diff --git a/common/framework/mail.php b/common/framework/mail.php index b7be6075f..3bb50f15a 100644 --- a/common/framework/mail.php +++ b/common/framework/mail.php @@ -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. * diff --git a/modules/advanced_mailer/advanced_mailer.admin.controller.php b/modules/advanced_mailer/advanced_mailer.admin.controller.php index fbbf106f4..aef06e5ab 100644 --- a/modules/advanced_mailer/advanced_mailer.admin.controller.php +++ b/modules/advanced_mailer/advanced_mailer.admin.controller.php @@ -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('

This is a test email from Advanced Mailer.

Thank you for trying Advanced Mailer.

' . '

고급 메일 발송 모듈 테스트 메일입니다.

메일이 정상적으로 발송되고 있습니다.

'); - $oMail->setFrom($advanced_mailer_config->sender_email, $advanced_mailer_config->sender_name); $oMail->addTo($recipient_email, $recipient_name); $result = $oMail->send(); diff --git a/modules/advanced_mailer/advanced_mailer.controller.php b/modules/advanced_mailer/advanced_mailer.controller.php index 6cb8a27ff..58359113a 100644 --- a/modules/advanced_mailer/advanced_mailer.controller.php +++ b/modules/advanced_mailer/advanced_mailer.controller.php @@ -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; } } diff --git a/modules/advanced_mailer/tpl/view_log.html b/modules/advanced_mailer/tpl/view_log.html index fdb0bace6..64f2bee7e 100644 --- a/modules/advanced_mailer/tpl/view_log.html +++ b/modules/advanced_mailer/tpl/view_log.html @@ -33,7 +33,7 @@ {htmlspecialchars($val->subject)} {@ 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)} {(zdate($val->regdate, "Y-m-d\nH:i:s"))}