Added Email Logging for Emailnotification class

This commit is contained in:
Frank Schubert
2025-08-22 17:49:54 +02:00
parent 866c3b2ae1
commit 3a86160b38
26 changed files with 1636 additions and 46 deletions

View File

@@ -12,19 +12,29 @@ class Emailnotification {
private $email_from = false;
private $email_to = false;
private $attachments = [];
private $object_type = "";
private $object_id = 0;
private $object_data = "";
private $_mimeheaders;
public function __construct() {
public function __construct($object_type = false, $object_id = false, $object_data = false) {
$this->headers = [
'X-Mailer' => MFAPPNAME.' Mailer',
];
$this->email_from = TT_OUTGOING_EMAIL;
$this->from_name = TT_OUTGOING_EMAIL_NAME;
if($object_type !== false) $this->object_type = $object_type;
if($object_id !== false) $this->object_id = $object_id;
if($object_data !== false) $this->object_data = $object_data;
}
public function addAttachment($file = null, $content = null, $name = false, $c_type = "application/octet-stream", $disposition = "attachment", $encoding = "base64" , $charset = "utf-8") {
public function addAttachment($filepath = null, $content = null, $name = false, $c_type = "application/octet-stream", $disposition = "attachment", $encoding = "base64" , $charset = "utf-8") {
$attachment = [
"file" => $file,
"file" => $filepath,
"content" => $content,
"name" => $name,
"c_type" => $c_type,
@@ -34,7 +44,7 @@ class Emailnotification {
];
$attachment["isfile"] = false;
if($file) {
if($filepath) {
$attachment['isfile'] = true;
}
@@ -119,6 +129,7 @@ class Emailnotification {
$body = $mime->get($mimeparams);
$headers = $mime->headers($this->headers);
$this->_mimeheaders = $headers;
//$log->debug(print_r($headers, true));
$mail =& Mail::factory('mail', ["-f ".$this->email_from]);
@@ -135,6 +146,85 @@ class Emailnotification {
*/
$mail->sep = "\r\n";
$mail->send($this->email_to, $headers, $body);
$this->_createEmailLog();
return true;
}
private function _createEmailLog() {
if(!$this->object_id || !$this->object_type) return true;
$log = mfLoghandler::singleton();
if(array_key_exists("To", $this->_mimeheaders)) {
$this->_mimeheaders['To'] = $this->email_to;
}
FronkDB::singleton()->startTransaction();
$el = EmailLog::create([
'object_type' => $this->object_type,
'object_id' => $this->object_id,
'object_data' => $this->object_data,
'from' => $this->email_from,
'to' => $this->email_to,
'headers' => json_encode($this->_mimeheaders),
'subject' => $this->subject,
'body_text' => $this->body,
'body_html' => $this->html,
]);
if(!$el->save()) {
$log->error(__METHOD__.": Error saving EmailLog. {$this->object_type}: {$this->object_id} from: '{$this->email_from}' | to: '{$this->email_to}' | subject: '{$this->subject}'");
FronkDB::singleton()->rollbackTransaction();
return false;
}
if(is_array($this->attachments) && count($this->attachments)) {
foreach($this->attachments as $att) {
if($att['isfile']) {
if(!file_exists($att["file"])) {
$log->error(__METHOD__.": File not found. ".$this->object_type.": ".$this->object_id." / Filename: ".$att['name']);
continue;
}
$sha256_hash = hash_file("sha256", $att["file"]);
$filesize = filesize($att["file"]);
$file_content = file_get_contents($att["file"]);
} else {
$sha256_hash = hash("sha256", $att["content"]);
$filesize = strlen($att["content"]);
$file_content = $att["content"];
}
// find attachment content or create it
$ela_content = EmailLogAttachmentContent::getFirst(['sha256' => $sha256_hash]);
if(!$ela_content) {
$ela_content = EmailLogAttachmentContent::create([
'mimetype' => $att['c_type'],
'filesize' => $filesize,
'sha256' => $sha256_hash,
'content' => $file_content,
]);
if (!$ela_content->save()) {
$log->error(__METHOD__ . ": Error saving EmailLogAttachmentContent. " . $this->object_type . ": " . $this->object_id . " / Filename: " . $att['name']);
continue;
}
}
$ela = EmailLogAttachment::create([
'emaillog_id' => $el->id,
'content_id' => $ela_content->id,
'filename' => $att['name'],
]);
if(!$ela->save()) {
$log->error(__METHOD__.": Error saving EmailLogAttachment. ".$this->object_type.": ".$this->object_id." / Filename: ".$att['name']);
}
}
}
FronkDB::singleton()->commitTransaction();
}
}