Skip to content

Commit

Permalink
#17 Add MailLogger Code to mailing.log
Browse files Browse the repository at this point in the history
  • Loading branch information
pbatroff committed May 29, 2024
1 parent 57ca278 commit 41a079d
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
84 changes: 84 additions & 0 deletions CRM/Mailingtools/MailLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/*-------------------------------------------------------+
| SYSTOPIA Mailingtools Extension |
| Copyright (C) 2024 SYSTOPIA |
| Author: P. Batroff ([email protected]) |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/

use CRM_Mailingtools_ExtensionUtil as E;


/**
* Class for Logger
*
* Shamelessly stolen from APILogger
* https://lab.civicrm.org/BjoernE/org.civicoop.logapirequests
*/
class CRM_Mailingtools_MailLogger {

private $_logFile = null;

/**
* CRM_Mailingtools_MailLogger constructor.
*
*
*/
function __construct() {
$file = CRM_Core_Config::singleton()->configAndLogDir . 'mailing.log';
$this->_logFile = fopen($file, 'a');
}

public function logMailInfo($recipients, $header, $body) {
$config = CRM_Mailingtools_Config::singleton();
if ($config->getSetting('mailing_debugging_short')) {
$short_info = [];
$short_info['FROM'] = $header['From'];
$short_info['TO'] = $header['To'];
$short_info['SUBJECT'] = $header['Subject'];
$this->addMessage(json_encode($short_info), "SHORT");
}
if ($config->getSetting('mailing_debugging_header')) {
$this->addMessage(json_encode($header), "HEADER");
}
if ($config->getSetting('mailing_debugging_recipients')) {
$this->addMessage(json_encode($recipients), "RECIPIENTS");
}
if ($config->getSetting('mailing_debugging_body')) {
$this->addMessage(json_encode($body), "BODY");
}
// add empty line for better readablility if debugging is active
if ($config->getSetting('mailing_debugging_short')
|| $config->getSetting('mailing_debugging_header')
|| $config->getSetting('mailing_debugging_recipients')
|| $config->getSetting('mailing_debugging_body')) {
fputs($this->_logFile, "\n");
}
}


/**
* Method to log the message
*
* @param $message
*/
private function addMessage($message, $info) {
fputs($this->_logFile, date('Y-m-d H:i:s'));
if (!empty($info)) {
fputs($this->_logFile, ' [');
fputs($this->_logFile, $info);
fputs($this->_logFile, '] ');
} else {
fputs($this->_logFile, ' ');
}
fputs($this->_logFile, $message);
fputs($this->_logFile, "\n");
}
}
10 changes: 9 additions & 1 deletion CRM/Mailingtools/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ public static function isNeeded(): bool {
$config = CRM_Mailingtools_Config::singleton();
return ($config->getSetting('anonymous_open_enabled') && $config->getSetting('anonymous_open_url'))
|| ($config->getSetting('anonymous_link_enabled') && $config->getSetting('anonymous_link_url'))
|| CRM_Mailingtools_RegexToken::isEnabled();
|| CRM_Mailingtools_RegexToken::isEnabled()
|| $config->getSetting('mailing_debugging_short')
|| $config->getSetting('mailing_debugging_header')
|| $config->getSetting('mailing_debugging_recipients')
|| $config->getSetting('mailing_debugging_body')
;
}

/**
Expand All @@ -57,6 +62,9 @@ function send($recipients, $headers, $body) {
$body = CRM_Mailingtools_RegexToken::tokenReplace($body, $context);
$headers = CRM_Mailingtools_RegexToken::tokenReplace($headers, $context);
}
// Debug output
$mail_logger = new CRM_Mailingtools_MailLogger();
$mail_logger->logMailInfo($recipients, $headers, $body);

$this->mailer->send($recipients, $headers, $body);
}
Expand Down

0 comments on commit 41a079d

Please sign in to comment.