0

I need to attach HTML file. I have dynamic HTML content and attach the HTMl content in the email

10
  • Try this magento.stackexchange.com/questions/257117/…
    – Msquare
    Mar 26 '20 at 7:50
  • how to create dynamic html file and upload in the attachment of the email
    – Divya
    Mar 26 '20 at 8:22
  • Please Describe more for better Understanding. and also add code you try.
    – Msquare
    Mar 26 '20 at 8:23
  • I have add the content of html content of product name and price and qty and conert this content into html and send the email
    – Divya
    Mar 26 '20 at 8:27
  • your email is works ? please check it by add static content and check it. and also check attechment is works or not ? after all this information i give you solution
    – Msquare
    Mar 26 '20 at 8:31
0

Please add one di.xml preference according to your Magento version Click here

then use below code to send Email with dynamic HTML Attechment file.

    protected $_storeManager;
    protected $_transportBuilder;
    protected $directoryList;
    protected $file_io;

    public function __construct(
        ............................................................
        ............................................................
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
        \Magento\Framework\App\Filesystem\DirectoryList $directoryList,
        \Magento\Framework\Filesystem\Io\File $file_io,
        ............................................................
        ............................................................
    ) {
        ............................................................
        ............................................................
        $this->_storeManager = $storeManager;
        $this->_transportBuilder = $transportBuilder;
        $this->directoryList = $directoryList;
        $this->file_io = $file_io;
        ............................................................
        ............................................................
    }

    public function execute()
    {   
        ............................................................
        ............................................................
        ............................................................
        // create  html file 

        $filePath = "/attachment/file/";
        $pdfPath = $this->directoryList->getPath('media').$filePath;

        $ioAdapter = $this->file_io;
        if (!is_dir($pdfPath)) {
            $ioAdapter->mkdir($pdfPath, 0775);
        }
        $fileName = "FileName.html";
        $pdfContent = "<!DOCTYPE html><html><head><title>HTML CONTENT</title></head>
        <body><table>
            <tr>
                <td>Product Name</td>
                <td>Product Price</td>
                <td>Product Qty</td>
            </tr>";

        /// get your products using for loop
        $pdfContent .= "<tr>
                    <td>TEst Product3333</td>
                    <td>2345</td>
                    <td>233333</td>
                </tr>";

        /*
        foreach ($variable as $key => $value) {
                $pdfContent .= "<tr>
                    <td>".$value->getName()."</td>
                    <td>".$value->getPrice()."</td>
                    <td>".$value->getQty()."</td>
                </tr>";
        }
        */ 

        $pdfContent .= "</table></body></html>";
        $ioAdapter->open(array('path'=>$pdfPath));
        $ioAdapter->write($fileName, $pdfContent, 0666);


        // send Email 

        $template_name = "your Email Template Name";

        $template_var = [];
        $template_var['var1'] = "value1";

        $sender_email = "Sender Email";

        $reciever_email['email'] = "Reciever Email";
        $reciever_email['name'] = "Reciever Name";


        $store = $this->_storeManager->getStore()->getId();

        try {
            $transport = $this->_transportBuilder->setTemplateIdentifier($template_name)
                ->setTemplateOptions(['area' => 'frontend', 'store' => $store])
                ->setTemplateVars($template_var)
                ->setFrom($sender_email)
                ->addTo($reciever_email['email'], $reciever_email['name']);

            // get email attechment path
            $media = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
            $media .= "/attachment/file/";
            $file_url = $media."".$fileName;
            $file_content = file_get_contents($file_url);
            $file_info = $this->file_io->getPathInfo($file_url);
            $file_name = $file_info['basename'];
            $file_type = "text/html";

            $transport = $this->_transportBuilder->addAttachment($file_content, $file_name, $file_type);

            $transport = $this->_transportBuilder->getTransport();

            $transport->sendMessage();

        } catch (\Exception $e) {
            $this->messageManager->addErrorMessage(__('Email send fail'));
        }

        ............................................................
        ............................................................
        ............................................................
    }
}

NOTE: without adding preference in di.xml to this link Attechemnt not work, so please add this from this link

I Hope This Helps You.

8
  • I have to avoid Prefernce I have to use Plugin , but it shows the error
    – Divya
    Mar 27 '20 at 6:26
  • you must have to use preference and add like this magento.stackexchange.com/questions/257117/…
    – Msquare
    Mar 27 '20 at 6:28
  • that's why a added note in my answer becuse without your attechment not working
    – Msquare
    Mar 27 '20 at 6:29
  • Instead of prefernce I have to use plugin
    – Divya
    Mar 27 '20 at 6:34
  • <type name="Magento\Framework\Mail\Template\TransportBuilder"> <plugin name="Vendor_module::attachment" type="Vendor\module\Model\Mail\TransportBuilder"/>
    – Divya
    Mar 27 '20 at 6:35

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.