0

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"}}
2

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?

| improve this answer | |
  • Tam its already html <config xmlns:xsi="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" /> </config> – summu Aug 13 '19 at 10:06
  • @summu can you add your email template content here? – Toan Tam Aug 13 '19 at 10:09
  • added see updated question – summu Aug 13 '19 at 10:12
  • @summu sorry, I mean the content of this file order/declined.html – Toan Tam Aug 13 '19 at 10:14
  • Tam updated in question – summu Aug 13 '19 at 10:15
1

You need to inject these below classes in your construct. Add below code in your construct :

/**
 * @var \Magento\Cms\Model\Template\FilterProvider
 */
protected $filterProvider;

/**
 * @var \Magento\Store\Model\StoreManagerInterface
 */
protected $storeManager;

/**
 * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider  [description]
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager    [description]
 */
public function __construct(
    .......
    \Magento\Cms\Model\Template\FilterProvider $filterProvider,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    .......
) {
    .......
    $this->filterProvider = $filterProvider;
    $this->storeManager = $storeManager;
    .......
}

/**
 * Return filter html data
 *
 * @return \Magento\Cms\Model\Template\FilterProvider
 */
public function getCmsFilterContent($contentData)
{
    $storeId = $this->storeManager->getStore()->getId();
    return $this->filterProvider->getBlockFilter()->setStoreId($storeId)->filter($contentData);
}

Now, call function getCmsFilterContent() in your phml file and pass html content varible in getCmsFilterContent() function as arguement.

or if you want to change in email template then, you need to use '|raw' with your param variable.

example: '{{var data|raw}}'

Hope, it will helpful for you.

| improve this answer | |
  • 1
    thanks your answer helped too – summu Aug 13 '19 at 13:56
0

basically, it should work without any issue if you just write

echo $stringHtml;

Please send me your screenshot if you are seeing no tags while echoing.

| improve this answer | |
  • I know that but this is riskified decline email, and whatever content i add in its admin panel it prints as it is in email, see my updated question – summu Aug 13 '19 at 9:22
  • If you are using html_entity_decode then try inserting your "<",">" signs as "&lt;" and "&gt;" once, where html_entity_decode will convert it into proper signs. – Rahul Anand Aug 13 '19 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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