0

Hi Can someone help me on this.

I need to send a custom email.

Currently all custom emails are send by setting the template id to a variable named $templateId as

$templateId = 20;

(or)

$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);

I want to be able to get the template id based on the email template name. , say "NewEmailTemplate" is the email template name and the id is 10.

I want the id 10 by giving the template name.

Thanks

3 Answers 3

3

Try

 $mailTemplate = Mage::getModel('core/email_template')

 $localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
 $templateId = $mailTemplate->loadDefault($templateId, $localeCode);

You may want to take a look at sendTransactional in Mage_Core_Model_Email_Template

public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)
{
    $this->setSentSuccess(false);
    if (($storeId === null) && $this->getDesignConfig()->getStore()) {
        $storeId = $this->getDesignConfig()->getStore();
    }

    if (is_numeric($templateId)) {
        $this->load($templateId);
    } else {
        $localeCode = Mage::getStoreConfig('general/locale/code', $storeId);
        $this->loadDefault($templateId, $localeCode);
    }
3
  • in this line did u mean $templateId = $mailTemplate->loadDefault("NewEmail", $localeCode); Commented Oct 15, 2014 at 20:24
  • Yea... the template id can be either numeric or a string Commented Oct 15, 2014 at 20:25
  • Also, what method are you use to send email because if you use sendTransactional and setup your system.xml correctly then you would need to do all this work Commented Oct 15, 2014 at 20:27
0

If anyone is looking for full sample code of how to send a Magento email based on an existing Magento email template, the following works well. It does not require any XML config.

// This is the name that you gave to the template in System -> Transactional Emails
$emailTemplate = Mage::getModel('core/email_template')->loadByCode('My Custom Email Template');

// These variables can be used in the template file by doing {{ var some_custom_variable }}
$emailTemplateVariables = array(
'some_custom_variable' => 'Hello World'
);

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

$emailTemplate->setSenderName('Joe Bloggs');
$emailTemplate->setSenderEmail('[email protected]');
$emailTemplate->setTemplateSubject("Here is your subject");

$emailTemplate->send('[email protected]', 'Joanna Bloggs', $emailTemplateVariables);
1
  • I keep getting some php error when I do Mage::getModel('core/email_template')->loadByCode() or Mage::getModel('core/email_template')->loadByAttribute(). Magento seems to have a lot of control over the error logging so that it does not produce a log despite ini settings and error function calls in the code. The MAGE_IS_DEVELOPER_MODE httpd config setting turns on strict error processing and also can't be regulated with ini settings or function calls, perhaps due to Magento's httpd config overrides. Commented Jan 11, 2017 at 20:03
0

Willster, good job. Your example works well.

Here is my part of code, that is on working shop currently:

$mailTemplate = Mage::getModel('core/email_template');
/* @var $mailTemplate Mage_Core_Model_Email_Template */

$templateId = $mailTemplate->loadByCode('Anfrage (Template)'); // custom transactional email template

$mailTemplate->setDesignConfig(array('area' => 'frontend'))
->setReplyTo($post['email'])
->sendTransactional(
    Mage::getStoreConfig($templateId),
    Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
    Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
    null,
    array('data' => $postObject)
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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