Skip to content
Snippets Groups Projects
Commit 3cfd1b66 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

email catcher class: detect parse error when reading email data

parent 27ef9263
Branches
No related tags found
No related merge requests found
...@@ -12,22 +12,36 @@ ...@@ -12,22 +12,36 @@
* *
* ---------------------------------------------------------------------- * ----------------------------------------------------------------------
* 2024-10-08 v0.1 initial version * 2024-10-08 v0.1 initial version
* 2024-10-16 v0.2 detect parse error when reading email data
* ======================================================================= * =======================================================================
*/ */
class emailcatcher class emailcatcher
{ {
/**
* Filename for email data
* @var string
*/
protected string $sMailData='emaildata.txt'; protected string $sMailData='emaildata.txt';
/**
* Id of selected email
* @var string
*/
protected string $sId=''; protected string $sId='';
/**
* Email data of the selected email
* @var array
*/
protected array $aSelectedEmail=[]; protected array $aSelectedEmail=[];
// ------------------------------------------------------------------
public function __construct() public function __construct()
{ {
$this->sMailData=dirname(__DIR__) . '/data/' . $this->sMailData; $this->sMailData=dirname(__DIR__) . '/data/' . $this->sMailData;
// $this->_readEmails();
} }
// ------------------------------------------------------------------ // ------------------------------------------------------------------
...@@ -37,6 +51,7 @@ class emailcatcher ...@@ -37,6 +51,7 @@ class emailcatcher
/** /**
* Fetch email of a single email from stdin and store it. * Fetch email of a single email from stdin and store it.
* It returns the return value of file_put_contents(). * It returns the return value of file_put_contents().
* used in php-sendmail.php
* *
* @return bool|int * @return bool|int
*/ */
...@@ -70,6 +85,11 @@ class emailcatcher ...@@ -70,6 +85,11 @@ class emailcatcher
// ------------------------------------------------------------------ // ------------------------------------------------------------------
/**
* Read all stored emails and return them as an array
* @param string $sEmail2Show optional: id of email to show
* @return array
*/
protected function _readEmails(string $sEmail2Show=''): array protected function _readEmails(string $sEmail2Show=''): array
{ {
if(!file_exists($this->sMailData)){ if(!file_exists($this->sMailData)){
...@@ -77,7 +97,14 @@ class emailcatcher ...@@ -77,7 +97,14 @@ class emailcatcher
} }
foreach(file($this->sMailData) as $line) { foreach(file($this->sMailData) as $line) {
if (empty(trim($line))) {
continue;
}
$aLinedata=json_decode($line, true); $aLinedata=json_decode($line, true);
if(!is_array($aLinedata)){
// echo "ERROR: unable to parse line as single json object: <code>$line</code>";
continue;
}
[$sHead, $sBody] = explode("\r\n\r\n", $aLinedata['mail']); [$sHead, $sBody] = explode("\r\n\r\n", $aLinedata['mail']);
$sHead="\n$sHead"; $sHead="\n$sHead";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment