Magento SendMail with attachment [duplicate]
-
12-12-2019 - |
Question
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!
Solution
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
OTHER TIPS
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.
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');
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange