1

My custom module used to send emails until one of the latest Magento Version upgrades.
I have no clue why it stopped sending them.

sendmail

<?php
declare(strict_types=1);

namespace Schott\B2bCustomerSageCrm\Helper;

use Magento\Framework\App\Area;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
    /**
     * @var TransportBuilder
     */
    protected $transportBuilder;
    /**
     * @var StoreManagerInterface
     */
    protected $storeManager;
    /**
     * @var StateInterface
     */
    protected $inlineTranslation;

    /**
     * @param Context $context
     * @param TransportBuilder $transportBuilder
     * @param StoreManagerInterface $storeManager
     * @param StateInterface $state
     */
    public function __construct(
        Context $context,
        TransportBuilder $transportBuilder,
        StoreManagerInterface $storeManager,
        StateInterface $state
    )
    {
        $this->transportBuilder = $transportBuilder;
        $this->storeManager = $storeManager;
        $this->inlineTranslation = $state;
        parent::__construct($context);
    }


    public function sendEmail(
        $id,
        $email,
        $customerFirstName,
        $customerLastName,
        $companyName
    )
    {
        $templateId = 'customer_crm'; // template id
        $fromEmail = 'no-reply@example.com';  // sender Email id
        $fromName = 'Frontname Text';             // sender Name
        $toEmail = 'example@icloud.com'; // receiver email id

        try {
            // template variables pass here
            $templateVars = [
                'customerid' => $id,
                'customeremail' => $email,
                'customerFirstName' => $customerFirstName,
                'customerLastName' => $customerLastName,
                'companyName' => $companyName
            ];

            $storeId = $this->storeManager->getStore()->getId();

            $from = ['email' => $fromEmail, 'name' => $fromName];
            $this->inlineTranslation->suspend();

            $storeScope = ScopeInterface::SCOPE_STORE;
            $templateOptions = [
                'area' => Area::AREA_FRONTEND,
                'store' => $storeId
            ];
            $transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope)
                ->setTemplateOptions($templateOptions)
                ->setTemplateVars($templateVars)
                ->setFromByScope($from)
                ->addTo($toEmail)
                ->getTransport();
            $transport->sendMessage();
            $this->inlineTranslation->resume();
        } catch (\Exception $e) {
            $this->_logger->critical($e->getMessage());
        }
    }
}

email template

<!--@subject {{var companyName}} {{trans "=> Sage CRM success"}}  @-->
<table cellspacing="0" cellpadding="0" border="0" width="100%">
    <tr>
        <td align="center" valign="top" style="padding:20px 0 20px 0">
            <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                <tr>
                    <td valign="top">
                        <h1 style="font-size:22px;font-weight:normal;line-margin:0 0 11px 0;">{{trans "New customer registration to be confirmed in Sage CRM"}} </h1>
                    </td>
                </tr>
                <tr>
                    <td>
                        <table cellspacing="0" cellpadding="0" border="0" width="650">
                            <tbody>
                            <tr>
                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                    {{trans "CustomerId:"}} {{var customerid}}
                                </td>
                            </tr>

                            <tr>
                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                    {{trans "CustomerMail:"}} {{var customeremail}}
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                    {{trans "CompanyName:"}} {{var companyName}}
                                </td>
                            </tr>
                            </tbody>
                            <tr>
                                <td colspan="2" valign="top" style="font-size:12px;padding:7px 9px 9px 9px;border:1px solid #EAEAEA;">
                                    {{trans "CustomerName:"}} {{var customerFirstName}} {{var customerLastName}}
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA;text-align:center;">
                        <center>
                            <p style="font-size:12px;margin:0;">

                            </p>
                        </center>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
</table>

In the log files I do not see anything. Can somebody see a problem why I would not work with my current Magento version?

1 Answer 1

1

The code is actually working, the problem was the sender's email address domain which was not in SendGrid.

Your Answer

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

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