i am trying to add an attachment to the order complete email that is sent right after the order is set to status complete i am using the event observer to catch the event checkout_submit_all_after
and created a custom function to process the event and get the order but after that i need help or a lead to attach a file to the email and send it , any leads would be helpful.
1 Answer
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
$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);
}
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();
public function addAttachment($fileContents,$fileName)
{
$tmp = array();
$tmp['fileContents'] = $fileContents;
$tmp['fileName'] = $fileName;
$this->_afile = $tmp;
return $this;
}
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();
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;
}
Add below mentioned code in send() method just before $mail->send() as shown
$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'];
}
try {
$mail->send();
$this->_mail = null;
}