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 text file which is inside var/docs/test.txt
//(by dw)
//start
$dir = Mage::getBaseDir();
$file_name = $dir.DS.'var'.DS.'docs'.DS.'test.txt'; //file path
if(file_exists($file_name))
{
$fileContents = file_get_contents($file_name);
$fileName = 'test.txt';
$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\core\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\core\Mage\Core\Model\Email\Template.php
Add below mentioned methods.
//(by dw)
//start
public function setEmAttachments($attachments)
{
$this->setOrderAttachments($attachments);
}
public function getEmAttachments()
{
return $this->getOrderAttachments();
}
//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';
$_file->filename = $atInfo['fileName'];
}
//end
try {
$mail->send();
$this->_mail = null;
}
Hope this is helpful.