Magento send mail attach file


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:

Magento send mail attach file

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(
        //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->setTemplateSubject('KhacTung Test Attach Mail');
        $mailTemplate->setTemplateText('Content Test');
        // add attachment
            file_get_contents(Mage::getBaseDir('base') . '/var/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

Magento send mail attach file

Thanks for watching!