Magento: Sending Invoice Emails to Custom Mail Addresses
When you’re working with Magento, there are times when you need to send invoice emails to mail addresses other than the customer’s or admin’s default address. In this post, we’ll explore how to achieve this by creating a custom observer and function.
Understanding the Current Setup
First, let’s examine the current setup in your configuration file:
<sales_order_invoice_save_after>
<observers>
<test>
<type>singleton</type>
<class>test/observer</class>
<method>sentMailToothers</method>
</test>
</observers>
</sales_order_invoice_save_after>
This config tells Magento to call the sentMailToothers method in your observer when an invoice is saved.
The Observer Method
The observer method, sentMailToothers, is responsible for sending the email. Here’s what the current implementation looks like:
public function sentMailToothers($observer)
{
// Load the default order invoice template
$template_id = 'sales_email_invoice_template';
$emailTemplate = Mage::getModel('core/email_template')->loadDefault($template_id);
// Get the invoice and associated order
$invoice = $observer->getEvent()->getInvoice();
$order = $invoice->getOrder();
if ($order->hasInvoices()) {
foreach ($order->getInvoiceCollection() as $inv) {
// Set up payment information block
$paymentBlockHtml = Mage::helper('payment')->getInfoBlock($order->getPayment())
->setIsSecureMode(true)
->toHtml();
// Define email recipient and variables
$email_to = 'test@test.com';
$customer_name = 'Test';
$email_template_variables = [
'order' => $order,
'invoice' => $invoice,
'payment_html' => $paymentBlockHtml
];
// Prepare the sender name and email
$sender_name = Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME);
$sender_email = Mage::getStoreConfig('trans_email/ident_general/email');
$emailTemplate->setSenderName($sender_name)->setSenderEmail($sender_email);
// Process the template variables
$processedTemplate = $emailTemplate->getProcessedTemplate($email_template_variables);
// Send the email!
$emailTemplate->send($email_to, $customer_name, $email_template_variables);
}
}
}
This method loads the default invoice template, retrieves the order and invoice data, prepares the payment information block, sets up the sender details, processes the template variables, and finally sends the email.
The Product Information Issue
You mentioned that product information is not showing in the templates. This is likely due to the way you’re loading the variables for the email template.
To include product information:
- Make sure your invoice template includes the necessary placeholders (like
{{var order.getAllItems()}}). - In your observer, ensure you’re passing all relevant data to the
$email_template_variablesarray. - Check if there are any typos or formatting issues in your template variables.
For example, try updating this section of your code:
$email_template_variables = [
'order' => $order,
'invoice' => $invoice,
'payment_html' => $paymentBlockHtml
];
To include all items in the invoice:
$email_template_variables = [
'order' => $order,
'invoice' => $invoice,
'items' => $invoice->getAllItems(),
'payment_html' => $paymentBlockHtml
];
Conclusion
In this post, we explored how to send Magento invoice emails to custom mail addresses using observers and custom functions. We reviewed the current setup, examined the observer method, identified the issue with product information not showing in templates, and provided a solution.
Remember, when working with Magento email templates:
- Ensure your template placeholders match the variables you’re passing.
- Double-check the format of your
$email_template_variablesarray. - Test thoroughly to ensure all relevant data is being included.
If you’re looking for more advanced techniques or need help with other aspects of Magento development, feel free to explore our resources at Laravel Company. We offer a variety of tutorials and guides to help you get the most out of your Magento store.