I need to attach a file to the email Magento sends when a client places an order.
This attachment can be either a PDF, a HTML or a simple TXT, and it must have the summary of the order (SKU开发者_如何学运维, Quantity, Unit Price, Total Price).
How can I go about making this happen?
Thanks in advance!
Solution is not very complex, although you'll need some time to implement it. I'll give there brief explanation of all the steps required.
The main steps are:
- Compose your attachment in Order mail and pass it to the mailer
- Transit it to Email template
- Add it to actual letter sent as attachment
1) You need to rewrite the Mage_Sales_Model_Order
class. Overwrite the `sendNewOrderEmail()' method in that class.
There you need to compose the attachment, that you want to send to the customer. Copy the original sendNewOrderEmail()
method source code to your overwriting method and put the following lines just before $mailer->send()
(for our example we will take the easy case - we will send a text file, that will contain only Grand Total of an order, attachment will be named 'summary.txt')
$fileContents = "Hello, here is the copy of your invoice:\n";
$fileContents .= sprintf("Grand total: %.2f", $this->getGrandTotal()) . "\n";
$fileContents .= "Thank you for your visit!";
$fileName = 'summary.txt';
$mailer->addAttachment($fileContents, $fileName);
2) Rewrite Mage_Core_Model_Email_Template_Mailer
- add there method addAttachment($fileContents, $fileName)
, that will add passed attachments to protected variable, that stores array of attachments.
Overwrite send()
method in this class. In that method you will need to pass array of attachments to every email template sent. E.g. add lines like
$emailTemplate->setAttachments($this->getAttachments());
right before line $emailTemplate->setDesignConfig...
3) Rewrite Mage_Core_Model_Email_Template
.
Add there method setAttachments($attachments)
, that must set incoming attachments to some protected variable.
Overwrite send()
method in this class. In that method you will need to add attachments to the sent letter. Put the lines like
foreach ($this->getAttachments() as $atInfo) {
$attachment = $mail->createAttachment($atInfo['fileContents']);
$attachment->filename = $atInfo['fileName'];
}
right before $mail->send()
line there.
That's all. It is really not very hard to do this task for a Magento developer. It just requires some time to compose contents, rewrite classes and complete interfaces.
精彩评论