Magento 2 – How to Parse PHP Plain Text to HTML

htmlmagento2PHP

I have a string HTML and have to convert(parse) it to HTML in PHP.

$stringHtml

enter image description here

How to convert this to HTML so that it all <p>, <br> tag works

this is Riskified decline email, and whatever content I add in its admin panel it prints as it is in email.
so I have added html_entity_decode in $content

code

        $transport = $this->transportBuilder
            ->setTemplateIdentifier('riskified_order_declined') // this code we have mentioned in the email_templates.xml
            ->setTemplateOptions(
                [
                    'area' => \Magento\Framework\App\Area::AREA_FRONTEND, // this is using frontend area to get the template file
                    'store' => $order->getStoreId(),
                ]
            )
            ->setTemplateVars(
                [
                    "content" => html_entity_decode($content),
                    "subject" => $subject,
                ]
            )
            ->setFrom(
                [
                    "email" => $this->apiConfig->getDeclineNotificationSenderEmail(),
                    "name" => $this->apiConfig->getDeclineNotificationSenderName(),
                ]
            )
            ->addTo($order->getCustomerEmail(), $order->getCustomerName())
            ->getTransport();

This is what my email looks like. no formatting it just prints the

tag.

enter image description here

Riskfied email_template.xml

<config
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
<template
        id="riskified_order_declined"
        label="Notification sent to customer when order was declined"
        file="order/declined.html"
        type="html"
        module="Riskified_Decider"
        area="frontend"
/>

declined.html

<!--@subject {{var subject}} @-->
{{template config_path="design/email/header_template"}}
{{var content}}
{{template config_path="design/email/footer_template"}}

Best Answer

You need to make sure these points below are set up:

  1. Change type="text" to type="html" in email_templates.xml, otherwise you'll get plain HTML in your mails.

  2. Render the $content in the email using this format {{var content|raw}}

Reference post: How to render data type html in template email magento2?

Related Topic