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

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);
    }
  • in this line did u mean $templateId = $mailTemplate->loadDefault("NewEmail", $localeCode); – Magento_Newbie Oct 15 '14 at 20:24
  • Yea... the template id can be either numeric or a string – Renon Stewart Oct 15 '14 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 – Renon Stewart Oct 15 '14 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('test@test.com');
$emailTemplate->setTemplateSubject("Here is your subject");

$emailTemplate->send('recipient@test.com', 'Joanna Bloggs', $emailTemplateVariables);
  • 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. – Spencer Williams Jan 11 '17 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, privacy policy and cookie policy

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