Magento Sending Custom Emails

In this blog we will see how to send email’s in magento through custom modules.

We will see the correct way of doing through this using magento transactional email system in magento. Before reading this blog, you need to have a basic understanding of magento email system.
Now lets see what steps are required to set up our custom transactional email. First in our module create a system.xml file, if it already doesn’t exist, and add the following code. Here will assume our module name is Excellence_Employee

<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<config>
	<sections>
		<customer translate="label" module="employee">
			<groups>
				<quote_email translate="label">
					<label>Quote Emails</label>
					<frontend_type>text</frontend_type>
					<sort_order>5</sort_order>
					<show_in_default>1</show_in_default>
					<show_in_website>0</show_in_website>
					<show_in_store>0</show_in_store>
					<fields>
							<exist_user_quote_template translate="label">
								<label>Existing User Quote Email</label>
								<frontend_type>select</frontend_type>
						     	<source_model>adminhtml/system_config_source_email_template</source_model>
								<sort_order>3</sort_order>
								<show_in_default>1</show_in_default>
								<show_in_website>1</show_in_website>
								<show_in_store>1</show_in_store>
							</exist_user_quote_template>
					</fields>
				</quote_email>
			</groups>
		</customer>
	</sections>
</config>

In this the only point to notice is this

<source_model>adminhtml/system_config_source_email_template</source_model>

This indicates our drop down will show a list of all our email templates.

Admin Configuration Email Drop Drop

Admin Configuration Email Drop Down

Next we need to make changes to config.xml. Here will add our new template file. Inside our >global< tab in config.xml

<template>
            <email>
                <customer_quote_email_exist_user_quote_template translate="label" module="employee">
                    <label>Existing User Quote Template</label>
                    <file>quote/exist_user.html</file>
                    <type>html</type>
                </customer_quote_email_exist_user_quote_template>
            </email>
        </template>

The above xml basically adds an entry into Magento Transactional Email. This >customer_quote_email_exist_user_quote_template< should be same as the entire path written is system.xml. New we need to add a file at folder app/locale/en_US/template/email/quote/exist_user.html and put your default email content there if any. This file is mentioned in the >file< above. After doing the above steps go to Admin -> System -> Transactional Email and you should be able to see your email template created in the Load Template drop down Magento Admin New Template ScrenShot. You can put in the content and subject of the email in admin. Also, when you go to Admin -> System -> Configuration -> Customer -> Quote Email, the drop down we just created, you will see our new email already selected there. Just save the configuration again at this point.
Next, to send the email from your module this the function to use

const XML_PATH_EMAIL_ADMIN_QUOTE_NOTIFICATION = 'customer/quote_email/exist_user_quote_template';
public function _sendNotificationEmail($to, $templateConfigPath = self::XML_PATH_EMAIL_ADMIN_QUOTE_NOTIFICATION)
	{
		if (! $to) return;

		$translate = Mage::getSingleton('core/translate');
		/* @var $translate Mage_Core_Model_Translate */
		$translate->setTranslateInline(false);

		$mailTemplate = Mage::getModel('core/email_template');
		/* @var $mailTemplate Mage_Core_Model_Email_Template */

		$template = Mage::getStoreConfig($templateConfigPath, Mage::app()->getStore()->getId());
		$sendTo = array();
		foreach ($to as $recipient)
		{
			if (is_array($recipient))
			{
				$sendTo[] = $recipient;
			}
			else
			{
				$sendTo[] = array(
					'email' => $recipient,
					'name' => null,
				);
			}
		}

		foreach ($sendTo as $recipient) {
			$mailTemplate->setDesignConfig(array('area'=>'frontend', 'store'=>Mage::app()->getStore()->getId()))
			->sendTransactional(
			$template,
			Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_IDENTITY,Mage::app()->getStore()->getId()),
			$recipient['email'],
			$recipient['name'],
			array(
					'customer'	=> $customer,
		                         'quote' => $quote
			)
			);
		}

		$translate->setTranslateInline(true);

		return $this;
	}

You need to call this function and call the correct parameters. $to is an array of recipient name and email id, $templateConfigPath is the path of our system.xml drop down.
Call this function from your model or controller, and this would send the email. The mail function used here is sendTransactional()

sendTransactional(
			$template,
			Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_IDENTITY,Mage::app()->getStore()->getId()),
			$recipient['email'],
			$recipient['name'],
			array(
					'customer'	=> $customer,
		            'quote' => $quote
			)
			)

The definition of this function is

 /**
     * Send transactional email to recipient
     *
     * @param   int $templateId
     * @param   string|array $sender sneder informatio, can be declared as part of config path
     * @param   string $email recipient email
     * @param   string $name recipient name
     * @param   array $vars varianles which can be used in template
     * @param   int|null $storeId
     * @return  Mage_Core_Model_Email_Template
     */
    public function sendTransactional($templateId, $sender, $email, $name, $vars=array(), $storeId=null)
Passing Parameters to EMail Template

As you can see above we have passed an array with $customer and $quote variable in the sendTransactional() function. These parameters passed can access in our email template. Like if we want to show the show the customer name and email in our template we need to put this in our template

{{htmlescape var=$customer.name}}

So, this is how we can pass variables to our email templates and access them.

  • Arun D

    Hi..all your articles are really informative and useful.and I’m new to magento..so I’m stuck with creating the file in which path.pls mention the full paths to create the files.

    Now I want to send the second email followed by the order confirmation email.so where to create the files and add new functions..can you pls explain me.it would be very grateful

    thanks a lot

  • Martijn

    is it possible to add a download link with this module? It will be a great learning example for all of us 🙂

    Thanks in advance,

    Martijn

  • I believe your xml path in config is incorrect.

    • Dilip Singh

      Hi

      Just help me out.
      I want to add 2-3 lines message in the standard order email template..which goes to customer when he places the order.

      Kindly let me know how can we do it.

      Thanks

  • make sure it’s in global

  • Learnt a lot from this blog. Thank you !!!

  • Ross P

    Hello,

    I want to send second email with standard order email when order is processed successfully.
    Could anyone help me?

    Thanks.
    Ross

    • dag

      Hi,
      Could you please let me know if you get your answer to this i am looking for the same thing.

      • Ross

        Hi,

        I made myself.
        How can I send file to you?

        Thanks.

        • Todecay

          Can you send me the file too? I’m looking for this for a long time now.

          I just want to send the order email to someone else then the customer nor the admin.

        • dag

          Hi,
          Could you put it in dropBox and post the url here?
          That would be awesome!

  • Sarah Robertson

    I’ve recently found a good extension for sending emails, its Email to Customers by Amasty. I use it for my shop and I’m satisfied with the results

  • Rachel Lancaster

    Nice Article!_