diff --git a/classes/Logging/MailTemplateLog.php b/classes/Logging/MailTemplateLog.php new file mode 100644 index 0000000..3d6aaae --- /dev/null +++ b/classes/Logging/MailTemplateLog.php @@ -0,0 +1,32 @@ +insert('stem_mail_template_log', [ + 'template' => $template, + 'email_to' => serialize($to), + 'email_bcc' => serialize($bcc), + 'status' => (int)1, + 'data' => serialize($data), + 'responseId' => $responseId, + 'response' => $response + ]); + } + + public static function LogError($template, $to, $bcc, $data, $error) { + /** @var \WPDB */ + global $wpdb; + $wpdb->insert('stem_mail_template_log', [ + 'template' => $template, + 'email_to' => serialize($to), + 'email_bcc' => serialize($bcc), + 'status' => (int)0, + 'data' => serialize($data), + 'error' => $error + ]); + } +} \ No newline at end of file diff --git a/classes/Models/MailTemplate.php b/classes/Models/MailTemplate.php index c36e737..0609cf2 100644 --- a/classes/Models/MailTemplate.php +++ b/classes/Models/MailTemplate.php @@ -4,7 +4,9 @@ use Mailgun\Mailgun; use Mailgun\Message\MessageBuilder; +use Mailgun\Model\Message\SendResponse; use Stem\Core\Context; +use Stem\MailTemplates\Logging\MailTemplateLog; use Stem\Models\Attachment; use Stem\Models\Post; use Stem\Models\Utilities\CustomPostTypeBuilder; @@ -177,6 +179,7 @@ public function send($email=null, $data=[], $inline=[], $attachments=[]) { return false; } + $smtpConfig = get_option('wp_mail_smtp'); $mailgunKey = arrayPath($smtpConfig, 'mailgun/api_key', null); @@ -192,25 +195,31 @@ public function send($email=null, $data=[], $inline=[], $attachments=[]) { $messageBldr = new MessageBuilder(); $messageBldr->setFromAddress($fromEmail, ['full_name' => $fromName]); + $logToEmails = []; $toSet = false; if ($email != null) { $toSet=true; if (is_array($email)) { foreach($email as $emailAddress) { + $logToEmails[] = $emailAddress; $messageBldr->addToRecipient($emailAddress); } } else { + $logToEmails[] = $email; $messageBldr->addToRecipient($email); } } foreach($this->toEmails as $toEmail) { $toSet=true; + $logToEmails[] = $toEmail->email; $messageBldr->addToRecipient($toEmail->email); } + $logBccEmails = []; foreach($this->bccEmails as $bccEmail) { - $messageBldr->addToRecipient($bccEmail->email); + $logBccEmails[] = $bccEmail->email; + $messageBldr->addBccRecipient($bccEmail->email); } $messageBldr->setSubject($this->subject); @@ -261,9 +270,13 @@ public function send($email=null, $data=[], $inline=[], $attachments=[]) { if ($toSet) { $mg = Mailgun::create($mailgunKey); - $mg->messages()->send($domain, $messageBldr->getMessage()); + /** @var SendResponse $response */ + $response = $mg->messages()->send($domain, $messageBldr->getMessage()); + MailTemplateLog::LogSuccess($response->getId(), $response->getMessage(), $this->slug, $logToEmails, $logBccEmails, $data); return true; + } else { + MailTemplateLog::LogError($this->slug, $logToEmails, $logBccEmails, $data, 'No email addresses specified.'); } return false; @@ -310,7 +323,11 @@ public static function sendTemplate($slugOrTemplate, $email = null, $data=[], $i $template = Context::current()->modelForPost($templatePost); if(!empty($template)) { return $template->send($email, $data, $inline, $attachments); + } else { + MailTemplateLog::LogError($slugOrTemplate, [$email], null, $data, 'Template not found.'); } + } else { + MailTemplateLog::LogError($slugOrTemplate, [$email], null, $data, 'Template not found.'); } } diff --git a/classes/UI/MailLogList.php b/classes/UI/MailLogList.php new file mode 100644 index 0000000..8c3baf0 --- /dev/null +++ b/classes/UI/MailLogList.php @@ -0,0 +1,71 @@ + 'Mail Log', + 'plural' => 'Mail Logs', + 'ajax' => false, + 'screen' => (!empty($args['screen'])) ? $args['screen'] : null + ]); + } + + public function get_columns() { + return [ + 'date' => 'Date', + 'template' => 'Template', + 'to' => 'To', + 'error' => 'Error', + 'responseId' => 'Response ID', + 'response' => 'Response', + ]; + } + + public function prepare_items() { + /** @var \WPDB $wpdb */ + global $wpdb; + + $offset = ($this->get_pagenum() - 1) * 100; + $this->items = $wpdb->get_results("select * from stem_mail_template_log order by id desc limit 100 offset {$offset}"); + $this->_column_headers = [$this->get_columns(), [], []]; + $total = $wpdb->get_var("select count(id) from stem_mail_template_log"); + $this->set_pagination_args([ + 'total_items' => $total, + 'per_page' => 100, + 'total_pages' => ceil($total / 100) + ]); + } + + protected function column_date($item) { + $date = Carbon::parse($item->created_at); + return $date->format('n/j/Y g:i a'); + } + + protected function column_template($item) { + return $item->template; + } + + protected function column_to($item) { + $toData = unserialize($item->email_to); + + return implode(', ', $toData); + } + + protected function column_error($item) { + return $item->error; + } + + protected function column_responseId($item) { + return $item->responseId; + } + + protected function column_response($item) { + return $item->response; + } +} \ No newline at end of file diff --git a/migrations/20190510002011_mail_template_logging.php b/migrations/20190510002011_mail_template_logging.php new file mode 100644 index 0000000..d040d2f --- /dev/null +++ b/migrations/20190510002011_mail_template_logging.php @@ -0,0 +1,49 @@ +table('stem_mail_template_log', ['id' => false, 'primary_key' => 'id', 'engine' => 'innodb', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci']); + $table + ->addColumn('id', 'biginteger', ['signed' => false, 'null' => false, 'identity' => true]) + ->addColumn('template', 'string', ['length' => 255, 'null' => false, 'collation' => 'utf8mb4_unicode_ci']) + ->addColumn('email_to', 'text', ['collation' => 'utf8mb4_unicode_ci']) + ->addColumn('email_bcc', 'text', ['collation' => 'utf8mb4_unicode_ci']) + ->addColumn('status', 'integer', ['signed' => true, 'null' => false, 'default' => (int)0]) + ->addColumn('data', 'text', ['collation' => 'utf8mb4_unicode_ci']) + ->addColumn('error', 'text', ['collation' => 'utf8mb4_unicode_ci']) + ->addColumn('responseId', 'string', ['length' => 255, 'collation' => 'utf8mb4_unicode_ci']) + ->addColumn('response', 'text', ['collation' => 'utf8mb4_unicode_ci']) + ->addTimestamps() + ->create(); + } +} diff --git a/stem-mail-template.php b/stem-mail-template.php index 06d2de9..bfc24e9 100644 --- a/stem-mail-template.php +++ b/stem-mail-template.php @@ -4,7 +4,7 @@ Plugin URI: https://github.com/jawngee/the-wonder Description: Model for mail templates Author: interfacelab -Version: 0.2.1 +Version: 0.2.2 Author URI: http://interfacelab.io */