Magento 2 : Custom Email Sending

Updated . Posted . Visible to the public.

Email can be sent in magento customization with following implementation

First, The email template should be created in view section and registered in etc using email_template.xml

email_template.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
    
    <template id="vender_email_paymentlinkemail" label="Payment Link Email" file="paymentlinkemail.html" type="html" module="Vender_Prescription" area="frontend"/>
</config>

Path of the email template is view/frontend/email/paymentlinkemail.html

Second, The system configuration can be used to change the template in future.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <section id="vender" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
            ....
            <group id="email" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
                
                <field id="paymentlinkemail" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label comment" type="select">
                    <label>Payment Link Email</label>
                    <source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
                    <comment/>
                </field>
            </group>
        </section>
    </system>
</config>


Third, EmailSending class should be created to send the email

private function sendMail($order,$token)
    {
        try {
            $sender = [
                'name' => $this->getStorename(),
                'email' => $this->getStoreEmail()
            ];


            $url = $this->urlBuilder->getBaseUrl() . 'routename/index/index/token/' . $token;
            $storeId = $this->storeManager->getStore()->getId();
            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $templateId = $this->scopeConfig->getValue ( 'vender/email/paymentlinkemail', $storeScope, $storeId );

            if ($order->getCustomerIsGuest()) {
                $customerName = $order->getBillingAddress()->getName();
            } else {
                $customerName = $order->getCustomerName();
            }

            $customerEmail = $order->getCustomerEmail();
            $transport = $this->transportBuilder
                ->setTemplateIdentifier($templateId)
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars([
                    'order' => $order,
                    'billing' => $order->getBillingAddress(),
                    'payment_html' => $this->getPaymentHtml($order),
                    'store' => $order->getStore(),
                    'formattedShippingAddress' => $this->getFormattedShippingAddress($order),
                    'formattedBillingAddress' => $this->getFormattedBillingAddress($order),
                    'name'  => $customerName,
                    'email'  => $customerEmail,
                    'increment_id'  => $order->getIncrementId(),
                    'url'  => $url,

                ])
                ->setFrom($sender)
                ->addTo($customerEmail,$customerName)
                ->getTransport();

            $transport->sendMessage();

        } catch (\Exception $e) {
            $this->logger->warning('Payment Link email is not sent to customer');
        }
    }
vasan
Last edit
vasan
Posted by vasan to vasan's deck (2020-05-30 16:31)