Get Transactional Email ID by Name in Magento

collection;emailmagento-1.9transactional-mail

Does someone know how do I get the id of an email by name set on backend? I have an email created in backend in transactional emails that I want to send it programatically, but the id of it may differ depending on the instance that I'm on (local, live, stage), and I can only provide the same name for it.
I have this:

Mage::getModel('core/email_template')->sendTransactional(
$templateId, 
$sender, 
$recepientEmail, 
$recepientName, 
$vars, 
$store);

And I need to find out $templateId and I only know that I saved the mail with name "Tests".

Best Answer

Try calling: Mage::getModel('core/email_template')->loadByCode('Tests') if Tests is the name of the template you set up.

After that you can set the sender name, sender email etc and send it. Here is some sample code:

     $emailTemplate = Mage::getModel('core/email_template');
                    $emailTemplate->loadByCode($mailTemplate);
                    if(!$emailTemplate->getTemplateId()){
                        continue;
                    }
                    $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
                    $mail = Mage::getModel('core/email')
                        ->setToName($customerName)
                        ->setToEmail($customerEmail)
                        ->setFromEmail(Mage::getStoreConfig('trans_email/ident_sales/email'))
                        ->setFromName(Mage::getStoreConfig('trans_email/ident_sales/name'))
                        ->setBody($processedTemplate)
                        ->setSubject($emailTemplate->getTemplateSubject())
                        ->setType('html');

         try {
                    $mail->send();
                } catch (Exception $error) {
                    Mage::log($error->getMessage(), null, 'auto_order_emails.log');
                    continue;
                }

$mailTemplate in above sample would be 'Tests' from your example.

Related Topic