search
top

Add attachment to order email in Magento

Many times there arise the need to attach files to the order mail.

I am giving here a brief explanation for this:

I have used Magento ver. 1.7.0.0 ( In previous version there was no mailer class).

1) Rewrite Sales Class

Copy the file found at app\code\core\Mage\Sales\Model\Order.php to app\code\local\Mage\Sales\Model\Order.php
Overwrite the `sendNewOrderEmail()’ method found.
Here you need to compose the attachment
Copy the below code and paste it just before $mailer->send();
I am attaching a simple pdf file which is inside var/docs/abc.pdf


 		//(by dw)
        //start
        $dir = Mage::getBaseDir();
		$file_name = $dir.DS.'var'.DS.'docs'.DS.'abc.pdf'; //file path
		if(file_exists($file_name))
		{
			$fileContents = file_get_contents($file_name);
			$fileName = 'abc.pdf';
			$mailer->addAttachment($fileContents, $fileName);
		}
		//end

2) Rewrite mailer
Copy the file found at app\code\core\Mage\Core\Model\Email\Template\Mailer.php to app\code\local\Mage\Core\Model\Email\Template\Mailer.php

Add a function and define a protected variable in class Mage_Core_Model_Email_Template_Mailer

	protected $_afile = array(); //(by dw)

 	//custom (by dw)
    //start
    public function addAttachment($fileContents,$fileName)
    {
    	$tmp = array();
    	$tmp['fileContents'] = $fileContents;
    	$tmp['fileName'] = $fileName;
        $this->_afile = $tmp;
        return $this;
    }
    //end

Add code in send() method. In this method you will need to pass array of attachements
Add below metioned lines just before $emailTemplate->setDesignConfig…

    if(!empty($this->_afile))
    {
    	$emailTemplate->setEmAttachments($this->_afile); //(by dw)
    }

3) Rewrite Template
Copy the file found at app\code\core\Mage\Core\Model\Email\Template.php to app\code\local\Mage\Core\Model\Email\Template.php

Add below mentioned methods and define a protected variable.

    protected $_filedata = array(); //(by dw)        
    //(by dw)
    //start

    public function setEmAttachments($attachments)
    {
    	$this->setOrderAttachments($attachments);
    }

    public function getEmAttachments()
    {
        return $this->getOrderAttachments();
    }

    public function setOrderAttachments($attachments)
    {   
    	$this->_filedata = $attachments;        
        return $this;
    }

    public function getOrderAttachments()
    {    	
        return $this->_filedata;
    }
    //end

Add below mentioned code in send() method just before $mail->send() as shown.


 	//(by dw)
	//start
	$atInfo = $this->getEmAttachments();
	if(!empty($atInfo))
	{
		$_file = $mail->createAttachment($atInfo['fileContents']);
		$_file->type = 'application/pdf'; //the type should be as per your file
		$_file->filename = $atInfo['fileName'];
	}
	//end

	try {
            $mail->send();
            $this->_mail = null;
        }

GitHub : Email Attachment

Put the code in site_root/app/code/

Hope this is helpful.

For further read – Email with attachment in Magento

41 Responses to “Add attachment to order email in Magento”

  1. Ajay Tiwari says:

    Hi, I have used this code and sending attached pdf in new order email everything going good the mail i got is without attachment. I am uanable to find out the root cause
    $dir = Mage::getBaseDir();
    $file_name = $dir.DS.’var’.DS.’export’.DS.’orderpdf’.DS.$this->getIncrementId().’.pdf'; //file path

    if(file_exists($file_name))
    {

    $fileContents = file_get_contents($file_name);
    $fileName = $this->getIncrementId().’.pdf';

    try{
    $mailer->addAttachment($fileContents, $fileName);
    }catch(exception $e){
    echo $e;
    die();
    }
    file is there and folder is thre but attachement not working

    • Decrypt Web says:

      Ajay, by this code snippet I am not able to conclude anything, request you to check the github link for my module, also you can send me link to your complete module to check

Leave a Reply to shiks

top