Send mail from custom module Magento 2

To send custom email in Magento 2 using the module, We need to create a custom module to send email to a specific recipient.

Out of the box, Magento Provides multiple mail template, like Sales Order, Customer mails, Contacts and so on.

If you want to send your custom mail functionality using Magento 2, You need to create a simple module and create a custom template file using Module for mail body.

Continue reading “Send mail from custom module Magento 2”

How to get customer data by customer email in magento 2?

You can get customer information by just passing the customer email using below code snippet, Create Block file,

To Fetch Customer Data in Magento by customer email ID, you required a customer email id and an optional website id to fetch the correct data.

<?php
declare(strict_types=1);

namespace Rbj\Customer\Model;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

class CustomerDetail
{
    public function __construct(CustomerRepositoryInterface $customerRepository )
    {
        $this->customerRepository = $customerRepository;
    }

    /**
     * @param string $email
     * @param ?int $websiteId
     * @return CustomerInterface
     * @throws LocalizedException
     */
    public function getCustomerByEmail(string $email, int $websiteId = null): CustomerInterface
    {
        try {
            $customer = $this->customerRepository->get($email, $websiteId);
        } catch (NoSuchEntityException $exception) {
            throw new LocalizedException(__('Provided Customer no longer exists.'));
        }
        
        return $customer;
    }
}

Continue reading “How to get customer data by customer email in magento 2?”

How to send image attachment with email template in Magento 2.4

Magento 2.4 Send Email with image attachment in the email template file.

This solution should work 2.4.* and a higher version.

For Attach an image in the email template, you need to override TransportBuilder Class from Magento\Framework\Mail\Template\TransportBuilder

For Send Custom Email with simple raw data refer link,  Send Mail from Custom module Magento 2

To Override the TransportBuilder class in Magento 2, you need to create a di.xml file. With this XML file, you can write syntax to override the Model class.

Continue reading “How to send image attachment with email template in Magento 2.4”