jruzafa
6/20/2016 - 7:26 AM

Send Magento template email with attachment.

Send Magento template email with attachment.

<?

// using model magento\app\code\core\Mage\Core\Model\Email\Template.php

public function send_error_email($error = NULL) {
  $mailTemplate = Mage::getModel('core/email_template');
  $mailTemplate->setSenderName('Insert Sender Name'); // use general Mage::getStoreConfig('trans_email/ident_general/name');
  $mailTemplate->setSenderEmail('insert@sender.email'); // use general Mage::getStoreConfig('trans_email/ident_general/email')
  $mailTemplate->setTemplateSubject('Insert Subject Title');
  $mailTemplate->setTemplateText('Insert Body Text');
  // add attachment
  $mailTemplate->getMail()->createAttachment(
    	  file_get_contents(Mage::getBaseDir('base') . '/var/log/file.log'),
		  Zend_Mime::TYPE_OCTETSTREAM,
		  Zend_Mime::DISPOSITION_ATTACHMENT,
		  Zend_Mime::ENCODING_BASE64,
		  'file.log'
  );
  $mailTemplate->send($to_email_arr, $to_name_arr);
}