0

Email header logo link uses <a class="logo" href="{{store url=""}}">

Display www.xyz.com/admin instead of www.xyz.com/us

I am using same email header in all email templates and it is working fine for other emails like New Order Email.

1 Answer 1

0

After thorough debugging, I have identified that the issue only affects custom emails, while default emails are functioning correctly.

To resolve this issue for custom emails, you should utilize

Magento\Store\Model\App\Emulation.

Before sending the email, start the environment emulation using:

$this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);

Ensure to pass $storeId as a parameter here. After the email has been sent, stop the environment emulation:

$this->appEmulation->stopEnvironmentEmulation();

For reference, here's how you can integrate this into your code:

public function __construct(
    \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
    \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Escaper $escaper,
    \Magento\Store\Model\App\Emulation $appEmulation,
    \Custom\Module\Logger\Logger $logger
) {
    $this->_transportBuilder = $transportBuilder;
    $this->inlineTranslation = $inlineTranslation;
    $this->scopeConfig = $scopeConfig;
    $this->storeManager = $storeManager;
    $this->_escaper = $escaper;
    $this->appEmulation = $appEmulation;
    $this->logger = $logger;
}

/**
 * Sends an email.
 *
 * @throws \Exception
 */
public function send($param)
{
    $storeId = $this->storeManager->getStore()->getId();
    $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
    $this->inlineTranslation->suspend();
    try {
        $sender = [
            'name' => $this->getConfigValue('trans_email/ident_general/name'),
            'email' => $this->getConfigValue('trans_email/ident_general/email'),
        ];
        $receiver_emails = [$param['receiver']];
        $emailTemplateVars = ['purchase_amount' => $param['purchase_amount']];
        $transport = $this->_transportBuilder
            ->setTemplateIdentifier($this->getEmailTemplate())
            ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
                    'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                ]
            )
            ->setTemplateVars($param)
            ->setFrom($sender)
            ->addTo($receiver_emails)
            ->getTransport();
        $transport->sendMessage();
        $this->inlineTranslation->resume();
        $this->appEmulation->stopEnvironmentEmulation();
        $this->logger->info('Email sent for Purchase Amount.');
        return true;
    } catch (\Exception $e) {
        $this->inlineTranslation->resume();
        $this->logger->info('Email sending failed for Purchase Amount.');
        $this->logger->error($e);
        $this->logger->error($e->getMessage());

        return false;
    }
}

This will fix all your issues related to Store like Store URL, Store Logo, Store Logo size, etc.

It will handle these things based on multistore website automatically.

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.