public function delete(Mage_Core_Model_Abstract $template)
 {
     $templateId = $template->getId();
     // removing attachments
     $attachmentCollection = Mage::getModel('aitemails/aitattachment')->getTemplateAttachments($templateId);
     if (count($attachmentCollection) > 0) {
         foreach ($attachmentCollection as $attachment) {
             @unlink(Aitoc_Aitemails_Model_Aitattachment::getBasePath() . $attachment->getAttachmentFile());
             @unlink(Aitoc_Aitemails_Model_Aitattachment::getBaseTmpPath() . $attachment->getAttachmentFile());
             Mage::getModel('aitemails/aitattachment')->load($attachment->getId())->delete();
         }
     }
     return parent::delete($template);
 }
 /**
  * Send mail to recipient
  *
  * @param   string      $email          E-mail
  * @param   string|null $name         receiver name
  * @param   array       $variables    template variables
  * @return  boolean
  **/
 public function send($email, $name = null, array $variables = array())
 {
     if (!$this->isValidForSend()) {
         return false;
     }
     if (version_compare(Mage::getVersion(), '1.4.2', '>=')) {
         $emails = array_values((array) $email);
         $names = is_array($name) ? $name : (array) $name;
         $names = array_values($names);
         foreach ($emails as $key => $email) {
             if (!isset($names[$key])) {
                 $names[$key] = substr($email, 0, strpos($email, '@'));
             }
         }
         $variables['email'] = $emails;
         $variables['name'] = $names;
     } else {
         if (is_null($name)) {
             $name = substr($email, 0, strpos($email, '@'));
         }
         $variables['email'] = $email;
         $variables['name'] = $name;
     }
     ini_set('SMTP', Mage::getStoreConfig('system/smtp/host'));
     ini_set('smtp_port', Mage::getStoreConfig('system/smtp/port'));
     $mail = $this->getMail();
     $setReturnPath = Mage::getStoreConfig(self::XML_PATH_SENDING_SET_RETURN_PATH);
     switch ($setReturnPath) {
         case 1:
             $returnPathEmail = $this->getSenderEmail();
             break;
         case 2:
             $returnPathEmail = Mage::getStoreConfig(self::XML_PATH_SENDING_RETURN_PATH_EMAIL);
             break;
         default:
             $returnPathEmail = null;
             break;
     }
     if ($returnPathEmail !== null) {
         if (version_compare(Mage::getVersion(), '1.4.2', '>=')) {
             $mailTransport = new Zend_Mail_Transport_Sendmail($returnPathEmail);
             Zend_Mail::setDefaultTransport($mailTransport);
         } else {
             $mail->setReturnPath($returnPathEmail);
         }
     }
     if (version_compare(Mage::getVersion(), '1.4.2', '>=')) {
         foreach ($emails as $key => $email) {
             $mail->addTo($email, '=?utf-8?B?' . base64_encode($names[$key]) . '?=');
         }
     } else {
         if (is_array($email)) {
             foreach ($email as $emailOne) {
                 $mail->addTo($emailOne, $name);
             }
         } else {
             $mail->addTo($email, '=?utf-8?B?' . base64_encode($name) . '?=');
         }
     }
     $this->setUseAbsoluteLinks(true);
     $text = $this->getProcessedTemplate($variables, true);
     if ($this->isPlain()) {
         $mail->setBodyText($text);
     } else {
         $html2plain = new Aitoc_Html2Text($text);
         $textplain = $html2plain->get_text();
         $mail->setBodyText($textplain);
         // adding html tag
         if (false === strpos($text, '<html>')) {
             $text = '<html>' . $text;
         }
         if (false === strpos($text, '</html>')) {
             $text = $text . '</html>';
         }
         $mail->setBodyHTML($text);
     }
     $mail->setSubject('=?utf-8?B?' . base64_encode($this->getProcessedTemplateSubject($variables)) . '?=');
     $mail->setFrom($this->getSenderEmail(), $this->getSenderName());
     // adding attachments
     $attachmentCollection = Mage::getModel('aitemails/aitattachment')->getTemplateAttachments($this->getId());
     if (count($attachmentCollection) > 0) {
         foreach ($attachmentCollection as $attachment) {
             if ($attachment->getAttachmentFile()) {
                 $sFileExt = substr($attachment->getAttachmentFile(), strrpos($attachment->getAttachmentFile(), '.'));
                 if ($attachment->getData('store_title')) {
                     $sFileName = $this->normalizeFilename($attachment->getData('store_title')) . $sFileExt;
                 } else {
                     $sFileName = substr($attachment->getAttachmentFile(), 1 + strrpos($attachment->getAttachmentFile(), '/'));
                 }
                 $att = $mail->createAttachment(file_get_contents(Aitoc_Aitemails_Model_Aitattachment::getBasePath() . $attachment->getAttachmentFile()));
                 $att->filename = $sFileName;
             }
         }
     }
     try {
         $mail->send();
         // Zend_Mail warning..
         $this->_mail = null;
     } catch (Exception $e) {
         return false;
     }
     return true;
 }