Smarty Template Not Going Through Email

I am trying to send mail on some condition. This is the code

public function sendMail() {
    $email = oxNew(Email::class);
    $email->setBody('this is body');
    $email->setSubject('this is subject');
    $email->setRecipient('[email protected]');
    $email->send();
}

With this code above email is going correctly to mailtrap. But when i am trying to pass template in $email->setBody(); it doesn’t send email anymore and there comes an error in EXCEPTION_LOG.txt

[19 Aug 13:13:33.136970 2019] [exception] [type 
OxidEsales\Eshop\Core\Exception\StandardException] [code 0] [file 
/usr/www/users/asign/visana/vendor/oxid-esales/oxideshop-ce/source/Core/UtilsObject.php] [line 
231] [message Message body empty]

This is what i do for sending template:

Module Metadata:
‘templates’ => array(
‘low_bonus_points.tpl’ => ‘views/custom/tpl/email/html/low_bonus_points.tpl’
),

Controller:
protected $_lowBonusPointsTemplate = "low_bonus_points.tpl";

public function sendMail() {
    $email = oxNew(Email::class);
    $smarty = $this->_getSmarty();`
    $email->setBody($smarty->fetch($this->_lowBonusPointsTemplate));
    $email->setSubject('this is subject');
    $email->setRecipient('[email protected]');
    $email->send();
}

protected function _getSmarty()
{
    if ($this->_oSmarty === null) {
        $this->_oSmarty = Registry::getUtilsView()->getSmarty();
    }
    //setting default view
    $this->_oSmarty->assign("oEmailView", $this);
    return $this->_oSmarty;
}

Hi,
I think you should define the template path:

$email->setBody($oSmarty->fetch(Registry::getConfig()->getTemplatePath($this->_lowBonusPointsTemplate, false)));
1 Like

I had to it this way and it worked:

$email->setBody($smarty->fetch(Registry::getConfig()->getTemplatePath('email/html/low_bonus_points.tpl',false)));

But shouldn’t this work?

$email->setBody($smarty->fetch($this->_lowBonusPointsTemplate));

Considering i have mentioned the path in module metadata?

Should work, but you have to define the path completely, e.g.:

‘low_bonus_points.tpl’ => ‘Vendor/Modul/views/custom/tpl/email/html/low_bonus_points.tpl’
1 Like

Yes it did worked got me some minutes to do it: Here’s what i did.

Module metadata file:

'templates'     =>  [
    'low_bonus_points.tpl'  => 'basket_calc_points/views/custom/tpl/email/html/low_bonus_points.tpl',
],

Controller:
protected $_lowBonusPointsTemplate = 'low_bonus_points.tpl';

public function sendMail() {
    $email = oxNew(Email::class);
    $smarty = $this->_getSmarty();`
    $email->setBody($smarty->fetch($this->_lowBonusPointsTemplate));
    $email->setSubject('this is subject');
    $email->setRecipient('[email protected]');
    $email->send();
}

After changing metadata.php file i had to deactivate the module. Clear tmp folder. Then activate the module again.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.