Skip to content

Instantly share code, notes, and snippets.

@mehdichaouch
Created September 12, 2019 07:10
Show Gist options
  • Save mehdichaouch/ad0b145926cfe61e15524ffa9eb11351 to your computer and use it in GitHub Desktop.
Save mehdichaouch/ad0b145926cfe61e15524ffa9eb11351 to your computer and use it in GitHub Desktop.
Magento 1 - Email notification via Magento email queue
<?php
public function emailNotify($event, $emailTemplate, $data, $toEmail)
{
$data = array_merge($data, array('YYYYMMDDHHMMSS' => date('Ymdhis')));
/* @var $template Mage_Core_Model_Email_Template */
$template = Mage::getModel('core/email_template')->loadDefault($emailTemplate);
$notifyTemplate = $template->getProcessedTemplate($data);
$notifyTemplateSubject = $template->getProcessedTemplateSubject($data);
// Send the mail to customer
//$mail = Mage::getModel('core/email')
// ->setToEmail($emailTo)
// ->setBody($notifyTemplate)
// ->setSubject($notifyTemplateSubject)
// ->setFromEmail(Mage::getStoreConfig(self::EMAIL_FROM))
// ->setFromName(Mage::getStoreConfig(self::EMAIL_FROM_NAME));
try {
//$mail->send();
/** @var $emailQueue Mage_Core_Model_Email_Queue */
$emailQueue = Mage::getModel('core/email_queue');
$emailQueue
->setEntityId($data['entity_id'])
->setEntityType($data['entity_type_id'])
->setEventType($event);
$emailQueue->setMessageBody($notifyTemplate);
$emailQueue
->setMessageParameters(
array(
'subject' => $notifyTemplateSubject,
'is_plain' => true,
'from_email' => Mage::getStoreConfig(self::EMAIL_FROM),
'from_name' => Mage::getStoreConfig(self::EMAIL_FROM_NAME),
'reply_to' => $toEmail,
)
)
->addRecipients($toEmail, $toEmail);
$emailQueue->addMessageToQueue();
return true;
} catch (Exception $error) {
Mage::getSingleton('core/session')->addError($error->getMessage());
//echo $error->getMessage();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment