Hi everyone,
Today I share code to send mail attach file in Magento.
Magento supported functions about mail for user and coder can easy to manipulate, config and custom these funstions
Before, we need look back Email Addresses config of Magento:
Here, I will use General Contact ($mailSender and $nameSender)
Code below:
/* * send file to mail * */ public function sendEmail($error = NULL) { $to_email_arr = array( '[email protected]', '[email protected]', ); $to_name_arr = array( 'KhacTung' ); //get sender mail and sender name default from System Magento $mailSender = Mage::getStoreConfig('trans_email/ident_general/mail'); $nameSender = Mage::getStoreConfig('trans_email/ident_general/name'); $mailTemplate = Mage::getModel('core/email_template'); $mailTemplate->setSenderName($nameSender); $mailTemplate->setSenderEmail($mailSender); $mailTemplate->setTemplateSubject('KhacTung Test Attach Mail'); $mailTemplate->setTemplateText('Content Test'); // add attachment $mailTemplate->getMail()->createAttachment( file_get_contents(Mage::getBaseDir('base') . '/var/FileTest.txt'), Zend_Mime::TYPE_OCTETSTREAM, Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, 'FileTest.txt' ); return $mailTemplate->send($to_email_arr, $to_name_arr); }
After call sendEmail() function from anywhere you want. Please check mail inbox and see result
Thanks for watching!