正如@BentCoder建议的那样,您可以使用将邮件存储在spool目录中的方法,并检查邮件是否存在。为了识别使用自定义头。在我的例子中,我只是在
X-Agent
头球。
我的发送电子邮件服务代码是:
namespace AppBundle\Services;
use AppBundle\Interfaces\EmailSenderInterface;
use \Swift_Mailer;
use \Swift_Message;
class NormalEmailSend implements EmailSenderInterface
{
/**
* @var Swift_Mailer
*/
private $mailer=null;
public function __construct(Swift_Mailer $mailer)
{
$this->mailer=$mailer;
}
/**
* @inheritdoc
*/
public function send($from,$to,$title="",$bodyPlain="",$bodyHtml="",array $cc=[],array $bcc=[])
{
$message=new Swift_Message($title);
$message->setFrom($from)->setTo($to)->setBody($bodyPlain,'text/plain');
if($bodyHtml){
$message->addPart($bodyHtml,'text/html');
}
$headers = $message->getHeaders();
$headers->addTextHeader('X-Agent','ellakcy_member_app');
return $this->mailer->send($message);
}
}
测试是:
namespace Tests\AppBundle\Services;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use \Swift_Message;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;
use AppBundle\Services\NormalEmailSend;
/**
* @testype functional
*/
class ContactEmailSendTest extends KernelTestCase
{
/**
* @var AppBundle\Services\NormalEmailSend
*/
private $service;
/**
* @var String
*/
private $spoolPath=null;
/**
* {@inheritDoc}
*/
protected function setUp()
{
$kernel = self::bootKernel();
$container = $kernel->getContainer();
$this->service = $container->get(NormalEmailSend::class);
$this->spoolPath = $container->getParameter('swiftmailer.spool.default.file.path');
}
public function testSendEmail()
{
$from='sender@example.com';
$to='receiver@example.com';
$this->service->send($from,$to,'Hello','Hello','Hello');
$this->checkEmail();
}
private function checkEmail()
{
$spoolDir = $this->getSpoolDir();
$filesystem = new Filesystem();
if ($filesystem->exists($spoolDir)) {
$finder = new Finder();
$finder->in($spoolDir)
->ignoreDotFiles(true)
->files();
if(!$finder->hasResults()){
$this->fail('No email has been sent');
}
$counter=0;
foreach ($finder as $file) {
/** @var Swift_Message $message */
$message = unserialize(file_get_contents($file));
$header = $message->getHeaders()->get('X-Agent');
$this->assertEquals($header->getValue(),'ellakcy_member_app');
$counter++;
}
//@todo Possibly Consinder not doing this check
if($counter===0){
$this->fail('No email has been sent');
}
} else {
$this->fail('On test environment the emails should be spooled and not actuallt be sent');
}
}
/**
* {@inheritDoc}
*/
public function tearDown()
{
$spoolDir = $this->getSpoolDir();
$filesystem = new Filesystem();
$filesystem->remove($spoolDir);
}
/**
* @return string
*/
private function getSpoolDir()
{
return $this->spoolPath;
}
}
希望它有帮助,也可以修改上面看到的代码,以便通过
X-Mail-Id
并将其作为可选参数放在函数的发送参数上,如果数字不为空,则可以将其设置为头。
因此,当你遍历spool目录时,你可以标记将通过其跟踪号发送的电子邮件。