Magento – Pass Data to PHTML for Custom Email Template

emailemail-templatesphtml

I am customizing my email by writing a block in template file,

in email template

{{block type='core/template' area='frontend' template='email/external/customer.phtml'}}

and template/email/external/customer.phtml is

$fieldsName = Mage::getSingleton('core/session')->getFields();
Mage::getSingleton('core/session')->unsFields();

$message = Mage::getSingleton('core/session')->getMessage();
Mage::getSingleton('core/session')->unsMessage();
$emailTemplateData = Mage::getSingleton('core/session')->getEmailTemplateData();
Mage::getSingleton('core/session')->unsEmailTemplateData();
?>
<p style="padding: 5px;"><?php echo $message; ?></p>
<?php if ($emailTemplateData) : ?>
    <table style="width:75%; padding: 5px;">
        <tr style="padding: 5px;">
            <td> <strong><?php echo "Field"; ?></strong></td>
            <td> <strong><?php echo "Data"; ?></strong></td>
        </tr>
        <?php foreach ($emailTemplateData as $key => $value): ?>

            <tr>
                <td> <?php echo $fieldsName[$key] . ":"; ?></td>
                <td> <?php echo $value; ?></td>
            </tr>

        <?php endforeach; ?>

    </table>

<?php endif; ?>

and email is being send like

public function sendEmail($subject, $fieldsName, $message, $emailTemplateData) {
        //Want to send these not this way
        Mage::getSingleton('core/session')->setFields($fieldsName);
        Mage::getSingleton('core/session')->setMessage($message);
        Mage::getSingleton('core/session')->setEmailTemplateData($emailTemplateData);

        $emailTemplateVariables = array();
        $emailTemplateCode = "External Customer Creation"; //Email Template Code
        $emailTemplate = Mage::getModel('core/email_template')->loadByCode($emailTemplateCode);
        $emailSubject = $subject; //Email Subject
        $emailTemplateVariables['templateMessage'] = ""; //Variables for Confirmation Mail. 

        $senderName = Mage::getStoreConfig('trans_email/ident_general/name'); //Getting the Store E-Mail Sender Name.
        $senderEmail = Mage::getStoreConfig('trans_email/ident_general/email'); //Getting the Store General E-Mail.
        //Appending the Custom Variables to Template.

        $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

        //Sending email to listed id under 
        //"Admin >> System >> Configuration >> Customer >> Customer Configuration >> External Source Customer Creation >> Confirmation email recipients"
        $emailIds = Mage::getStoreConfig('customer/external_source_customer/confirmation_email_to');
        $toEmailList = explode(",", $emailIds);

        foreach ($toEmailList as $toEmail) {

            $mail = Mage::getModel('core/email')
                    ->setToName($toName)
                    ->setToEmail($toEmail)
                    ->setBody($processedTemplate)
                    ->setSubject($emailSubject)
                    ->setFromEmail($senderEmail)
                    ->setFromName($senderName)
                    ->setType('html');

            try {
                $mail->send();
            } catch (Exception $e) {
                Mage::logException($e);
            }
        }
    }

And my email template is like…

<style>
ul {
    margin-left: 0;
    padding-left: 14px;
}
</style>

<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<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;font-size:12px;">
            <!-- [ header starts here] -->
            <tr>
                <td colspan='2' valign="top"><a href="{{store url=""}}"><img src="{{skin url="images/abc-logo-225x83.png" _area="frontend" _package="default" _theme="abc"}}" alt="{{var logo_alt}}" style="margin-bottom:10px;" border="0"/></a></td>
            </tr>
            <!-- [ middle starts here] -->
            <tr>
                <td valign="top" colspan='2'>
                    <p style="line-height:16px; margin:0;">
                     {{block type='core/template' area='frontend' mydata=$fieldsName template='email/external/customer.phtml'}}
                    </p>
                </td>
            </tr>
            <tr>
                <td colspan='2' bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you, <strong>abc Team</strong></p></center></td>
            </tr>
        </table>
    </td>
</tr>
</table>
</div>
</body>

This is working fine. But this not a good way, as you can see I am passing data to session to get in my phtml file, what I want is that is there a way pass this data to phtml file in a proper way.

Best Answer

In Your block write like this and

{{block type='core/template' area='frontend' mydata="1" template='email/external/customer.phtml'}}

Your phtml you get this data like this

echo $this->mydata;

please check and let me know it help you or not.