2

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".

1
  • Why not configure it in the backend as every other mail too? Feb 22, 2015 at 22:27

1 Answer 1

3

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.

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.