Merge pull request #3152 from MGatner/email-bugfix

Revamp Email archive
This commit is contained in:
MGatner 2020-06-23 11:46:23 -04:00 committed by GitHub
commit fdc1b8295a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 29 deletions

View File

@ -58,9 +58,15 @@ class Email
/**
* Properties from the last successful send.
*
* @var array|null
*/
public $archive;
/**
* Properties to be added to the next archive.
*
* @var array
*/
public $archive = [];
protected $tmpArchive = [];
/**
* @var string
*/
@ -427,7 +433,6 @@ class Email
$this->BCCArray = [];
$this->headers = [];
$this->debugMessage = [];
$this->archive = [];
$this->setHeader('Date', $this->setDate());
if ($clearAttachments !== false)
{
@ -460,9 +465,9 @@ class Email
}
}
// Archive the plain text values
$this->archive['fromEmail'] = $from;
$this->archive['fromName'] = $name;
// Store the plain text values
$this->tmpArchive['fromEmail'] = $from;
$this->tmpArchive['fromName'] = $name;
// prepare the display name
if ($name !== '')
@ -481,7 +486,7 @@ class Email
$this->setHeader('From', $name . ' <' . $from . '>');
isset($returnPath) || $returnPath = $from;
$this->setHeader('Return-Path', '<' . $returnPath . '>');
$this->archive['returnPath'] = $returnPath;
$this->tmpArchive['returnPath'] = $returnPath;
return $this;
}
@ -506,7 +511,7 @@ class Email
}
if ($name !== '')
{
$this->archive['replyName'] = $name;
$this->tmpArchive['replyName'] = $name;
// only use Q encoding if there are characters that would require it
if (! preg_match('/[\200-\377]/', $name))
@ -520,10 +525,8 @@ class Email
}
}
$this->setHeader('Reply-To', $name . ' <' . $replyto . '>');
$this->replyToFlag = true;
// Archive the plain text values
$this->archive['replyTo'] = $replyto;
$this->replyToFlag = true;
$this->tmpArchive['replyTo'] = $replyto;
return $this;
}
@ -570,7 +573,7 @@ class Email
{
$this->CCArray = $cc;
}
$this->archive['CCArray'] = $cc;
$this->tmpArchive['CCArray'] = $cc;
return $this;
}
//--------------------------------------------------------------------
@ -601,7 +604,7 @@ class Email
else
{
$this->setHeader('Bcc', implode(', ', $bcc));
$this->archive['BCCArray'] = $bcc;
$this->tmpArchive['BCCArray'] = $bcc;
}
return $this;
}
@ -615,8 +618,9 @@ class Email
*/
public function setSubject($subject)
{
$this->archive['subject'] = $subject;
$subject = $this->prepQEncoding($subject);
$this->tmpArchive['subject'] = $subject;
$subject = $this->prepQEncoding($subject);
$this->setHeader('Subject', $subject);
return $this;
}
@ -1573,17 +1577,14 @@ class Email
$result = $this->spoolEmail();
if ($result)
{
// Determine the correct properties to archive
$archive = array_merge(get_object_vars($this), $this->archive);
unset($archive['archive']);
$this->setArchiveValues();
if ($autoClear)
{
$this->clear();
}
Events::trigger('email', $archive);
$this->archive = $archive;
Events::trigger('email', $this->archive);
}
return $result;
@ -1631,10 +1632,8 @@ class Email
}
// Update the archive
$archive = array_merge(get_object_vars($this), $this->archive);
unset($archive['archive']);
Events::trigger('email', $archive);
$this->archive = $archive;
$this->setArchiveValues();
Events::trigger('email', $this->archive);
}
//--------------------------------------------------------------------
/**
@ -2171,4 +2170,21 @@ class Email
}
return isset($length) ? substr($str, $start, $length) : substr($str, $start);
}
//--------------------------------------------------------------------
/**
* Determines the values that should be stored in $archive.
*
* @return array The updated archive values
*/
protected function setArchiveValues(): array
{
// Get property values and add anything prepped in tmpArchive
$this->archive = array_merge(get_object_vars($this), $this->tmpArchive);
unset($this->archive['archive']);
// Clear tmpArchive for next run
$this->tmpArchive = [];
return $this->archive;
}
}

View File

@ -16,17 +16,14 @@ class MockEmail extends Email
{
if ($this->returnValue)
{
// Determine the correct properties to archive
$archive = array_merge(get_object_vars($this), $this->archive);
unset($archive['archive']);
$this->setArchiveValues();
if ($autoClear)
{
$this->clear();
}
Events::trigger('email', $archive);
$this->archive = $archive;
Events::trigger('email', $this->archive);
}
return $this->returnValue;

View File

@ -68,6 +68,23 @@ class EmailTest extends \CodeIgniter\Test\CIUnitTestCase
$this->assertNotEmpty($email->archive);
}
public function testEmailSendRepeatUpdatesArchive()
{
$config = config('Email');
$email = new \CodeIgniter\Test\Mock\MockEmail($config);
$email->setTo('foo@foo.com');
$email->setFrom('bar@foo.com');
$this->assertTrue($email->send());
$email->setFrom('');
$email->setSubject('Archive Test');
$this->assertTrue($email->send());
$this->assertEquals('', $email->archive['fromEmail']);
$this->assertEquals('Archive Test', $email->archive['subject']);
}
public function testSuccessDoesTriggerEvent()
{
$config = config('Email');