0

I created a custom module to send the separate mail for admin and customer but its not working.could you please suggest me regarding this so i can proceed forward.

1
  • What did you tried?
    – HoangHieu
    Mar 29, 2023 at 13:38

1 Answer 1

1

Check this code

   use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Sales\Model\Order;

class MyCustomEventObserver implements ObserverInterface
{
    protected $_transportBuilder;
    protected $_inlineTranslation;
    
    public function __construct(
        TransportBuilder $transportBuilder,
        StateInterface $inlineTranslation
    ) {
        $this->_transportBuilder = $transportBuilder;
        $this->_inlineTranslation = $inlineTranslation;
    }
    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getData('order');
        $storeId = $order->getStore()->getId();
        
        // Send email to customer
        $this->_inlineTranslation->suspend();
        $templateId = 'your_customer_email_template_id';
        $transport = $this->_transportBuilder
            ->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
            ->setTemplateVars(['order' => $order])
            ->setFrom('general')
            ->addTo($order->getCustomerEmail())
            ->addBcc('[email protected]') // add the admin email as a BCC recipient
            ->getTransport();
        $transport->sendMessage();
        $this->_inlineTranslation->resume();
        
        // Send email to admin
        $this->_inlineTranslation->suspend();
        $templateId = 'your_admin_email_template_id';
        $transport = $this->_transportBuilder
            ->setTemplateIdentifier($templateId)
            ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
            ->setTemplateVars(['order' => $order])
            ->setFrom('general')
            ->addTo('[email protected]') // replace with the admin email address
            ->getTransport();
        $transport->sendMessage();
        $this->_inlineTranslation->resume();
        
        return $this;
    }
}
10
  • it's not send the admin mail to my site please suggest Mar 30, 2023 at 10:18
  • Make sure that the email settings in your Magento installation are configured correctly. You can check this by going to Stores > Configuration > General > Store Email Addresses in the Magento Admin panel and verifying that the "Sender Email" and "Sender Name" fields are set correctly
    – Harun
    Mar 30, 2023 at 11:18
  • Sender Email is different from the admin mail I added the adminer mail store>configuration >send a copy to mail and select BCC. to have any idea how I can send the mail copy to the admin Mar 30, 2023 at 11:45
  • check the above code . I made some changes .Try it
    – Harun
    Mar 30, 2023 at 11:57
  • ok, you have added the bcc method but I need the bcc mail for the separate email template. please suggest. thanks for helping Mar 30, 2023 at 12:33

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.