I wanna add custom CC email to all transactional emails in Magento 2. But, I can't find any decent event to catch email sending process and add CC to email. Any ideas?
UPDATE
As Keyur Shah suggests, I've created di.xml
<config>
<type name="\Magento\Framework\Mail\Template\TransportBuilder">
<plugin name="test_custompayment_plugin" type="Test\Custompayment\Plugin\TransportBuilder" sortOrder="1" />
</type>
And /Test/Custompayment/Plugin/TransportBuilder.php
<?php
namespace Test\Custompayment\Plugin;
class TransportBuilder
{
public function beforeGetTransport()
{
$this->prepareMessage();
$this->addCc('sevkafaya@gmail.com','');
$mailTransport = $this->mailTransportFactory->create(['message' => clone $this->message]);
$this->reset();
return $mailTransport;
}
}
But I face with error:
Fatal Error: 'Uncaught Error: Call to undefined method
Test\Custompayment\Plugin\TransportBuilder::prepareMessage()
So do I need copy all content from \Magento\Framework\Mail\Template\TransportBuilder
and just replace getTransport()
with beforeGetTransport
?
Best Answer
Magento2 use
\Magento\Framework\Mail\Template\TransportBuilder
to configure and send an email so I would suggest you to write abefore
plugin for this class and and method is getTransport() method.On before plugin you can add your custom email address as addCc().