2

This question already has an answer here:

I have been working on a custom extension and I need a function to send the generated CSV file. Can you please let me know what might be the possible error in it.

protected function _sendCSV($filename, $subject, $last_update, $now) {
    $template_id = "svm_export_email";
    $email_template =  Mage::getModel('core/email_template')->loadDefault($template_id)->setSubject($subject." [".$now."]");
    $template_variables = array(
        'file_name' => $filename,
        'last_update' => $last_update,
        'now' =>$now,
        );
    $email_template->getProcessedTemplate($template_variables);

    $sender = array('My Team' => Mage::getStoreConfig('trans_email/ident_general/email'));
    $email_template->setFrom($sender);


    $file = Mage::getBaseDir('var') . DS .'SVM'. DS .date('Y'). DS .date('m'). DS .$filename;
    $attachment = $email_template->getMail()->createAttachment(file_get_contents($file));
    $attachment->filename = $filename;

    try {
        $email_template->send('thecoderin@gmail.com','My Gmail');
    } catch (Exception $e) {
        Mage::log($e->getMessage(), NULL, self::LOG);
        return false;
    }
    Mage::log($filename." File sent through mail", NULL, self::LOG);
    return true;
}

I am getting the following error on invoking the function:

exception 'Exception' with message 'This letter cannot be sent.' in /my/magento/root/app/code/core/Mage/Core/Model/Email/Template.php:398

Thanks!

marked as duplicate by Keyul Shah, Siarhey Uchukhlebau, Amit Bera, brentwpeterson, liyakat May 29 '17 at 3:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Check the createAttachment function from the file lib/Zend/Mail.php. See the function from below:

public function createAttachment($body,
                                 $mimeType    = Zend_Mime::TYPE_OCTETSTREAM,
                                 $disposition = Zend_Mime::DISPOSITION_ATTACHMENT,
                                 $encoding    = Zend_Mime::ENCODING_BASE64,
                                 $filename    = null)
{

    $mp = new Zend_Mime_Part($body);
    $mp->encoding = $encoding;
    $mp->type = $mimeType;
    $mp->disposition = $disposition;
    $mp->filename = $filename;

    $this->addAttachment($mp);

    return $mp;
}

We have to pass the extra parameters to the function. See example at https://stackoverflow.com/questions/17164077/magento-send-transactional-email-with-pdf-attachment

  • Thanks, Somehow createAttachment seems not working for me. I have tried commenting out createAttachment, then too the mail is not sending. – Anish Karim Apr 7 '15 at 3:48
3

I found an alternate solution using Zend Mail and it works for me.

protected function _sendCSV($filename, $subject, $last_update, $now) {
    $template_id = "svm_export_email";
    $emailTemplate = Mage::getModel('core/email_template')->loadDefault($template_id);
    $template_variables = array(
        'file_name' => $filename,
        'last_update' => $last_update,
        'now' =>$now,
        );
    $storeId = Mage::app()->getStore()->getId();
    $processedTemplate = $emailTemplate->getProcessedTemplate($template_variables);

    $recipients = array(
        "SVM" => Mage::getStoreConfig('svm_modules/svm_export/email'),
        );

    $subject .= " [".$now."]";
    $sender = Mage::getStoreConfig('trans_email/ident_general/email');

    $file = Mage::getBaseDir('var') . DS .'SVM'. DS .date('Y'). DS .date('m'). DS .$filename;
    $attachment = file_get_contents($file);

    try {
        $z_mail = new Zend_Mail('utf-8');

        $z_mail->setBodyHtml($processedTemplate)
            ->setSubject($subject)
            ->addTo($recipients)
            ->setFrom($sender, "My Team");

        $attach = new Zend_Mime_Part($attachment);
        $attach->type = 'application/csv';
        $attach->disposition = Zend_Mime::DISPOSITION_INLINE;
        $attach->encoding    = Zend_Mime::ENCODING_8BIT;
        $attach->filename    = $filename;

        $z_mail->addAttachment($attach);
        $z_mail->send();
        Mage::log("Mail Sent", NULL, self::LOG);
    } catch (Exception $e) {
        Mage::log($e->getMessage(), NULL, self::LOG);
        return false;
    }
    return true;

}

Thanks for all the support. I believe this may help someone.

1

Copy this code in any phtml or controller to send mail with attachment file:

  $mailTemplate = Mage::getModel('core/email_template');
  $mailTemplate->setSenderName('Sender Name'); 
  $mailTemplate->setSenderEmail('sender@sender.email');
  $mailTemplate->setTemplateSubject('Subject Title');
  $mailTemplate->setTemplateText('Body Text');
  // add attachment
  $mailTemplate->getMail()->createAttachment(
          file_get_contents(Mage::getBaseDir('base') . '/media/file/file.pdf'), //location of file
          Zend_Mime::TYPE_OCTETSTREAM,
          Zend_Mime::DISPOSITION_ATTACHMENT,
          Zend_Mime::ENCODING_BASE64,
          'file.pdf'
  );
  $mailTemplate->send('toemail@email.com','subject','set message');

Not the answer you're looking for? Browse other questions tagged or ask your own question.