0

I am trying to add image in my custom email template. I want to know how to define image section in email template and how to set image PARAMS of email sending action using TransportBuilder. Images exist of media path.

2 Answers 2

0

To add an image to your custom email template in Magento 2 and set the image parameters using TransportBuilder, you can follow these steps:

1.Create or modify your custom email template. You can do this by creating a new HTML file or modifying an existing one. For example, you can use the following code to display an image:

<img src="{{var image_url}}" alt="Image Description" height="" width="">

2.In your custom module or code where you are sending the email using TransportBuilder, you need to set the image parameters.

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\TemplateTypesInterface;
use Magento\Framework\Mail\Template\FactoryInterface;
use Magento\Framework\Mail\MessageInterface;
use Magento\Framework\Mail\TransportInterface;
use Magento\Store\Model\StoreManagerInterface;

class YourCustomClass
{
    protected $transportBuilder;
    protected $templateFactory;
    protected $message;
    protected $storeManager;
    
    public function __construct(
        TransportBuilder $transportBuilder,
        FactoryInterface $templateFactory,
        MessageInterface $message,
        StoreManagerInterface $storeManager
    ) {
        $this->transportBuilder = $transportBuilder;
        $this->templateFactory = $templateFactory;
        $this->message = $message;
        $this->storeManager = $storeManager;
    }
    
    public function sendCustomEmail()
    {
        // Set image parameters
        $mediaUrl = $this->storeManager
            ->getStore()
            ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
        
        $imagePath = 'path_to_image'; // Replace with the actual path to your image file
        
        // Prepare image URL
        $imageUrl = $mediaUrl . $imagePath;
        
        // Build email
        $templateVars = [
            // Other template variables
            'image_url' => $imageUrl,
        ];
        
        $templateOptions = [
            'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
            'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
        ];
        
        $transport = $this->transportBuilder
            ->setTemplateIdentifier('your_template_identifier') // Replace with your actual template identifier
            ->setTemplateOptions($templateOptions)
            ->setTemplateVars($templateVars)
            ->setFrom('[email protected]')
            ->addTo('[email protected]')
            ->getTransport();
        
        $transport->sendMessage();
    }
}

Note: Replace path_to_image with the actual path to your image file. Make sure the image file is located within the Magento media directory.

0

You can use the @charmi Patel's answer if you want to pass a variable in email templates or you can directly call your image path in the magento2 email template like


<img src="{{view url='Magento_Customer/images/icn_checkout.png'}}" height="30" width="30" alt="{{trans 'Quick Checkout'}}" />

The above code i got from the below module's path.

vendor/magento/theme-frontend-luma/Magento_Customer/email/account_new.html

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.