2

My requirement is capture email subject, from address . and email body content , for all kind of mail sending Which event capture mail sending information in magento 2

1 Answer 1

3

You need to overwrite the Magento\Framework\Mail\Transport class.

Create file app/code/Foo/EmailLogger/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<!-- Overwriting Mail Send Transport-->
<preference for="Magento\Framework\Mail\Transport" type="Foo\EmailLogger\Framework\Mail\Transport"/>

Create file app/code/Foo/EmailLogger/Framework/Mail/Transport.php

<?php

namespace Foo\EmailLogger\Framework\Mail;

use Magento\Framework\Mail\Transport as CoreTransport;
use Magento\Framework\Mail\MessageInterface;
use Foo\EmailLogger\Model\EmailLogger;
use Psr\Log\LoggerInterface;
use Foo\EmailLogger\Helper\Data as EmaiLoggerHelper;

class Transport extends CoreTransport
{
   /**
    * @var LoggerInterface
    */
    protected $logger;

    /**
     * @var EmailLogger
     */
     protected $emailLogger;

     /**
      * @var EmaiLoggerHelper
      */
     protected $helper;

     /**
      * @param LoggerInterface $logger
      * @param EmailLogger $emailLogger
      * @param EmaiLoggerHelper $helper
      * @param MessageInterface $message
      * @param array $parameters
      * @throws \InvalidArgumentException
      */
      public function __construct(
         LoggerInterface $logger,
         EmailLogger $emailLogger,
         MessageInterface $message,
         $parameters = null
      ) {
         $this->logger      = $logger;
         $this->emailLogger = $emailLogger;
         parent::__construct($message, $parameters);
      }


      /**
       * Send a mail using this transport
       *
       * @return void
       * @throws \Exception
       */
       public function sendMessage()
       {
           try {
               /** @var \Zend_Mime_Part $body **/
               $body = $this->_message->getBody();
               $this->emailLogger->log(
                   $this->_message->getRecipients(),
                   $this->_message->getFrom(),
                   $this->_message->getSubject(),
                   $this->_message->getHeaders(),
                   $body->type,
                   $body->getRawContent()
               );
            } catch (\Exception $e) {
                $this->logger->critical($e->getMessage());
            }
        parent::sendMessage();
    }
}//end class

Now depending on your requirement you can write your capture logic in Foo\EmailLogger\Model\EmailLogger::log()

3
  • When send mail event occur, while update getting email details to custom table in database, that's why i prefer for event observer method. suppose you know, how to update this value to custom table I will take this. Apr 28, 2016 at 11:20
  • 1
    There is no event present when a mail is sent, so we cant use observer. Apr 28, 2016 at 15:12
  • @AtishGoswami, is there any way to get template code and template body ? Sep 11, 2020 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.