If you are a webmaster or web developer you are already aware that magento sends email on certain even. For example: on order creation, invoice generation, on customer registration. You can manage these email template using System->Transaction Email section  well. Now  if you are web developer you may face some moment when you need to send email from magento at some custom event or inbuilt event in magento. One way of sending email is just to use PHP script and mailer function. Other way of sending custom email from magento is like creating of your own email template and sending those email template. In my post "Magento Send Custom Email" I will explain how to send email using Custom Email template programmatically . Sending custom email from magento involves two steps, first is creation of your custom email template and other is to write script.

Magento Send Custom Email - Template Creation

First of all question is why you need to create custom email ? It has tow reason. First your webmaster can modify content or design of your email. Other you can add some variable in custom email which your webmaster can use in any place in the template, so it will make your webmaster easy to play with email. Template creation involve 2 work. First create .html format of your email and second is add your email information in your module config.xml. Let us create a custom email template. Create your custom email and place it in folder \app\locale\en_US\template\email. Where en_US is your

<!--@subject custom Email for {{var customVar}} @-->
<!--@vars
{"store url=\"\"":"Store Url",
"var logo_url":"Email Logo Image Url",
"var logo_alt":"Email Logo Image Alt",
}
@-->
<!--@styles
body,td { color:#2f2f2f; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; }
@-->
<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
<div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
Hi {{var custName}}<br>
Thank you for sending email. Your logo path is {{var logo_url}}
</div>
</body>

Tag Description of the code:

  • <!--@subject : This tag define the subject line of your email
  • <!--@vars : In vars section you can define your own constant variable for your email template
  • <!--@styles : In style section you can add custom style for your email template.
  • {{var custName}} : By using {{var customevariable}} you can insert custom variable in your email template. You can replace these custom variable by passing array in email send function. I will explain this process latter in this article.

Code/Script and Config.xml setting for Email Sending

Add following code in your module config.xml under global tag.

<template>
<email>
<test_email_template">
<label>Test Email Template</label>
<file>custom_email.html</file>
<type>html</type>
</test_email_template>
</email>
</template>


After adding above code in config.xml file clear cache of magento.
Now when you will go to System ->Transactional Emails -> Add New Template can load your template. You can see your "Test Email" Template in the Template list and please select default language and click on load button. Below is the screenshot.

Magento Send Custom Email

Add Email template in magento

Save your template and

Now Add following code from where you want to send email:

In My case I will use it in controller:

<?php
$templateId = 10;
$sender = Array('name' => 'Ankur',
'email' => '[email protected]');
//recepient
$email = '[email protected]';
$emaiName = 'Ankur';
$vars = Array();
$vars = Array('customVar'=>$yourvarialbe,
);*/
$storeId = Mage::app()->getStore()->getId();
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')
->sendTransactional($templateId, $sender, $email, $emailName, $vars, $storeId);
$translate->setTranslateInline(true);
?>

Now your email will be sent out.

Here you can download my email sending module.

If you like my article please write your feedback and vote us.

1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 3.33 out of 5)
Loading ... Loading ...
Tagged with:
 

6 Responses to Magento Send Custom Email

  1. Bheem Singh says:

    Good post. Help me while setting up email template in magento. Thanks a lot

  2. prerna says:

    i have download your module and put it in codepool “local” but there is no config.xml file to declare module which i have to placed in app/etc/modules/.

    Please tell me how can i use it.

  3. alok says:

    i want to set subject is ‘ Firstname Name, réinitialisez votre mot de passe CKT ‘.But when i am set the email will not send. i am working on forget password email . i have done from both (admin and from transactional emails) . kindly help

  4. Mobbl says:

    I want to send an additional email when the cutsomer registers – is it possible with your module? If yes, how?

  5. thank You for this post

Leave a Reply