Last active
October 6, 2018 10:27
-
-
Save standa/718f173a8c0e488b30fb87c190784070 to your computer and use it in GitHub Desktop.
OpenCart Mailgun And Database Mail Logging class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Mail | |
{ | |
// @author standa | additional features related to mailgun and logging | |
use MailgunTrait, LoggedEmailTrait; | |
public function send() | |
{ | |
// ... | |
// todo: place after this line: | |
// $message .= '--' . $boundary . '--' . PHP_EOL; | |
try { | |
$this->logDb($to, $this->subject, $this->text, $this->html, $header); | |
$header = $this->appendCustomHeader($header); | |
} catch (\Throwable $e) { | |
\Tracy\Debugger::log($e); | |
} | |
$this->logTxt($to, $this->subject, $message, $header, $this->parameter); | |
// ... | |
} | |
} | |
/** | |
* @author standa | |
* @var int | |
*/ | |
trait MailgunTrait | |
{ | |
protected function appendCustomHeader(string $header): string | |
{ | |
if (strpos($this->smtp_hostname, 'mailgun') === false) { | |
return $header; | |
} | |
$custom = implode(PHP_EOL, array_filter([ | |
isset($this->order_id) ? 'X-Mailgun-Variables: '.json_encode(['oder_id' => $this->order_id]) : null, | |
isset($this->mail_id) ? 'X-Mailgun-Variables: '.json_encode(['mail_id' => $this->mail_id]) : null, | |
isset($this->event) ? 'X-Mailgun-Variables: '.json_encode(['event' => $this->event]) : null, | |
])); | |
return rtrim($header, PHP_EOL).PHP_EOL.$custom.PHP_EOL.PHP_EOL; | |
} | |
} | |
/* | |
* SQL: | |
* | |
* CREATE TABLE `oc_mail` ( | |
`mail_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
`order_id` int NULL, | |
`to` varchar(512) NOT NULL, | |
`event` varchar(128), | |
`subject` varchar(1024) NOT NULL, | |
`text` text NOT NULL, | |
`html` text NOT NULL, | |
`header` text NOT NULL, | |
`date_added` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
`date_modified` datetime NULL, | |
`message_id` varchar(512) NULL, | |
`date_delivered` datetime NULL, | |
`date_opened` datetime NULL | |
) ENGINE='InnoDB' COLLATE 'utf8_general_ci'; | |
*/ | |
trait LoggedEmailTrait | |
{ | |
protected $order_id; | |
protected $mail_id; | |
protected $event; | |
public function setOrderId(int $order_id): void | |
{ | |
$this->order_id = $order_id; | |
} | |
protected function setMailId(int $mail_id): void | |
{ | |
$this->mail_id = $mail_id; | |
} | |
public function setEvent(?string $event = null, ?string $suffix = null): void | |
{ | |
if ($event === null) { | |
$backtrace = debug_backtrace(); | |
$event = ($backtrace[1]['class'] ?? $backtrace[1]['file'] ?? '').'::'.($backtrace[1]['function'] ?? ''); | |
} | |
if ($suffix !== null) { | |
$event .= '('.$suffix.')'; | |
} | |
$this->event = $event; | |
} | |
protected function logTxt(string $to, string $subject, string $message, string $header, ?string $parameter = null): void | |
{ | |
$log = new Log('mail.txt'); | |
$log->write(func_get_args()); | |
} | |
/** | |
* @param string $to | |
* @param string $subject | |
* @param null|string $text | |
* @param null|string $html | |
* @param string $header | |
* @return int | |
* @throws Exception | |
*/ | |
protected function logDb(string $to, string $subject, ?string $text, ?string $html, string $header): int | |
{ | |
/** @var \DB $db */ | |
$db = $GLOBALS['registry']->get('db'); | |
if ($db === null) { | |
throw new Exception('Db instance not found'); | |
} | |
if (!isset($this->event)) { | |
$backtrace = debug_backtrace(); | |
$event = ($backtrace[2]['class'] ?? $backtrace[2]['file'] ?? '').'::'.($backtrace[2]['function'] ?? ''); | |
} else { | |
$event = $this->event; | |
} | |
$db->query('INSERT INTO oc_mail (order_id, `to`, event, subject, text, html, header) VALUES ('.($this->order_id ?: 'null').", '".$db->escape($to)."', '{$db->escape($event)}', '".$db->escape($subject)."','".$db->escape($text)."', '".$db->escape($html)."', '".$db->escape($header)."')"); | |
$mail_id = (int) $db->getLastId(); | |
$this->setMailId($mail_id); | |
return $mail_id; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment