Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/email/src/Charcoal/Email/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,14 @@ protected function logSend(bool $result, string $logId, PHPMailer $mailer): void
$this->bcc()
);

$recipientIndex = 0;

foreach ($recipients as $to) {
$log = $this->logFactory()->create(EmailLog::class);

$log->setId($logId);
// The first recipient keeps the ID used for open/link tracking.
// Subsequent recipients need a distinct primary key.
$log->setId(($recipientIndex === 0) ? $logId : $logId . '-' . $recipientIndex);
$log->setQueueId($this->queueId());
$log->setMessageId($mailer->getLastMessageId());
$log->setCampaign($this->campaign());
Expand All @@ -1040,6 +1044,8 @@ protected function logSend(bool $result, string $logId, PHPMailer $mailer): void
}

$log->save();

$recipientIndex++;
}
}

Expand Down
148 changes: 148 additions & 0 deletions packages/email/tests/Charcoal/Email/EmailLogSendTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

namespace Charcoal\Tests\App\Email;

use PHPMailer\PHPMailer\PHPMailer;

use Charcoal\Email\Email;
use Charcoal\Email\EmailLog;
use Charcoal\Email\Services\Tracker;
use Charcoal\Factory\FactoryInterface;
use Charcoal\Tests\AbstractTestCase;

/**
* Test the Email::logSend() method.
*/
class EmailLogSendTest extends AbstractTestCase
{
public function testLogSendCreatesUniqueLogForEachRecipient()
{
$container = $GLOBALS['container'];
$logFactory = new EmailLogSpyFactory();

$obj = new Email([
'logger' => $container['logger'],
'config' => $container['email/config'],
'view' => $container['email/view'],
'template_factory' => $logFactory,
'queue_item_factory' => $logFactory,
'log_factory' => $logFactory,
'tracker' => new Tracker('', $logFactory)
]);

$obj->setTo([
'first@example.com',
'second@example.com',
'third@example.com'
]);
$obj->setSubject('Test');

$method = new \ReflectionMethod(Email::class, 'logSend');
$method->setAccessible(true);
$method->invoke($obj, true, 'message-log-id', new PHPMailer());

$ids = array_map(function (EmailLogSpy $log) {
return $log->id;
}, $logFactory->logs);

$this->assertSame([
'message-log-id',
'message-log-id-1',
'message-log-id-2'
], $ids);
}
}

/**
* Email log implementation that records the assigned ID without saving.
*/
class EmailLogSpy
{
/**
* @var string|null
*/
public $id;

/**
* @param string $id
* @return void
*/
public function setId($id)
{
$this->id = $id;
}

/**
* Ignore the other EmailLog setters.
*
* @param string $name
* @param array $arguments
* @return void
*/
public function __call($name, $arguments)
{
}

/**
* @return void
*/
public function save()
{
}
}

/**
* Factory that creates EmailLog spies.
*/
class EmailLogSpyFactory implements FactoryInterface
{
/**
* @var EmailLogSpy[]
*/
public $logs = [];

/**
* @param string $type
* @param array|null $args
* @param callable|null $cb
* @return EmailLogSpy
*/
public function create($type, ?array $args = null, ?callable $cb = null)
{
$log = new EmailLogSpy();
$this->logs[] = $log;
return $log;
}

/**
* @return string
*/
public function baseClass()
{
return EmailLog::class;
}

/**
* @return string
*/
public function defaultClass()
{
return EmailLog::class;
}

/**
* @return array
*/
public function arguments()
{
return [];
}

/**
* @return callable|null
*/
public function callback()
{
return null;
}
}
Loading