Home » Magento tutorial for developer » How to send email programmatically in Magento

How to send email programmatically in Magento

When working with Magento, sometimes you want to programmatically send email to your customers after a specific action occurred. For example, you want to send an email from Order View Page in admin panel

how to send email programmatically in magento

Step 1:

First, we need to create the email template that will be used to send out. In this example, assuming that the email template is named “Custom Email”, and this email template contains two vars: user_name and product_name.

Now,load  our email template:

Step 2:

To continue, we will create an array that contains these two values:

In case you want to preview the template before sending the email, you can print the following function:

Otherwise, the next steps would be to set the email sender information. In this example we are pulling in our sending info from the Magento store email address configuration under the general contact tab:

Step 3:

And finally, we send out the email:

As you can see, programmatically sending emails in Magento is a fairly straightforward process.

Drop a line if you have any questions

Happy coding!

About Brian Wilson

avatar
Brian is a freelance Magento developer with 5 years of experience with customizing Magento extensions, magento themes. He's interested in sharing Magento Tutorials to the community

2 comments

  1. avatar

    $emailTemplate->send(‘dts.koushikg@gmail.com’,’Koushik Ghosh’, $emailTemplateVariables);
    Not sending email to the receiver [dts.koushikg@gmail.com]. Please help me out.

    —————————————————————————
    app/etc/config.xml

    ActiveCodeline custom email module
    custom_theme_change_intimation.html
    html

    —————————————————————————
    $emailTemplate = Mage::getModel(‘core/email_template’)->loadDefault(‘custom_theme_change’);

    $emailTemplateVariables = array();
    $emailTemplateVariables['myvar1'] = $var1;
    $emailTemplateVariables['myvar2'] = $var2;
    $emailTemplateVariables['myvar3'] = $var3;
    $emailTemplateVariables['myvar3'] = Mage::getSingleton(‘customer/session’)->getCustomer()->getName();

    $processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

    $emailTemplate->send(‘dts.koushikg@gmail.com’,’Koushik Ghosh’, $emailTemplateVariables);
    —————————————————————————

    app/locale/en_US/template/email/custom_theme_change_intimation.html

    Hello,
    One of a customer requested to change the site theme. Below are the informations:

    Var1:{{var myvar1}}
    Var2:{{var myvar2}}
    Var3:{{var myvar3}}
    Customer Name:{{var myvar4}}

    -Thanks & Regards Team

    —————————————————————————

    Do not understand why mail is not send to the user. Please someone help me out, its very urgent.

  2. avatar

    Can you explain what are the possible values for 3rd parameter $vars?

    i.e.

    $emailTemplate->send($receiveEmail,$receiveName, $vars);

    Can you provide us a guideline to create & register custom email template (should be visible in admin)?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Scroll To Top