Good Afternoon,
My goal is to log all emails sent by cake to a database. Everything is working well but I am filling my debug log with duplicate data. Is there a way to configure my current method to not log to the default debug logger? Failing that do you suggest extending cakeEmail or a component? Thank you.
Cake 2.8.4
bootstrap.php
App::uses('CakeLog', 'Log');
CakeLog::config('debug', array(
'engine' => 'File',
'types' => array('notice', 'info', 'debug'),
'scopes' => array('notice', 'info', 'debug'),
'file' => 'debug',
));
CakeLog::config('error', array(
'engine' => 'File',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'file' => 'error',
));
CakeLog::config('emaillog', array(
'engine' => 'EmailDatabaseLog',
'types' => array('debug'),
'scopes' => array('email')
));
email.php config
public $jms_mail = array(
'transport' => 'Mail',
'from' => 'you@localhost',
'log' => true
);
My engine for reference
<?php
App::uses('EmailLog', 'Model');
App::uses('BaseLog', 'Log/Engine');
class EmailDatabaseLog extends BaseLog {
public $config = array(
//'levels' => array('warning', 'notice', 'debug', 'info', 'error')
);
public function __construct($options = array()) {
parent::__construct($options);
}
public function write($type, $message) {
$cc_field = '';
$bcc_field = '';
$from_field = '';
$body_field = '';
$subject_field = 'test_subject';
if (substr($message, 1,5) == 'From:') {
$message = str_replace(array("\r\n", "\r", "\n"), ' ', $message);
// trim each data item off of string , existence of cc and bcc uncertain.
$body_field = substr($message, strpos($message, '<body>')+6, strlen($message)-14);
$trimmed_string = substr($message, 0, strpos($message, 'DOCTYPE')-3);
if (strpos($trimmed_string, 'To:') > -1 ) {
$to_field = substr($trimmed_string, strpos($trimmed_string, 'To: ')+4, strlen($trimmed_string)+1);
$trimmed_string = substr($message, 0, strpos($trimmed_string, 'To:')-1);
}
if (strpos($trimmed_string, 'Subject:') > -1 ) {
$subject_field = substr($trimmed_string, strpos($trimmed_string, 'Subject: ')+8, strlen($trimmed_string)+1);
$trimmed_string = substr($message, 0, strpos($trimmed_string, 'Subject:')-1);
}
if (strpos($trimmed_string, 'Bcc:') > -1 ) {
$bcc_field = substr($trimmed_string, strpos($trimmed_string, 'Bcc: ')+5, strlen($trimmed_string)+1);
$trimmed_string = substr($message, 0, strpos($trimmed_string, 'Bcc:')-1);
}
if (strpos($trimmed_string, 'Cc:') > -1 ) {
$cc_field = substr($trimmed_string, strpos($trimmed_string, 'Cc: ')+4, strlen($trimmed_string)+1);
$trimmed_string = substr($message, 0, strpos($trimmed_string, 'Cc:')-1);
}
if (strpos($trimmed_string, 'From:') > -1 ) {
$from_field = substr($trimmed_string, strpos($trimmed_string, 'From: ')+6, strlen($trimmed_string)+1);
//$trimmed_string = substr($trimmed_string, 0, strpos($trimmed_string, 'Bcc:')-1);
}
$EmailLogger = new EmailLog();
$EmailLogger->create();
$EmailLogger->save(array('raw' => $message, 'to_field' => $to_field', cc_field' => $cc_field, 'subject_field' => $subject_field, 'from_field' => $from_field, 'bcc_field' => $bcc_field, 'body_field' => $body_field,
}
}
}