Couple of days back I had to work with mail sending in magento e-commerce. Here I am giving a complete example of how to do that:-

Magento sends mails with it’s model “core/email_template”. Here is an example code for your understanding:-

/**
  * $templateId can be set to numeric or string type value.
  * You can use Id of transactional emails (found in
  * "System->Trasactional Emails"). But better practice is
  * to create a config for this and use xml path to fetch
  * email template info (whatever from file/db).
  */
  const EMAIL_TEMPLATE_XML_PATH = 'customer/testemail/email_template';
  $templateId = Mage::getStoreConfig(EMAIL_TEMPLATE_XML_PATH);

  $mailSubject = 'HI this is a test mail.';

  /**
  * $sender can be of type string or array. You can set identity of
  * diffrent Store emails (like 'support', 'sales', etc.) found
  * in "System->Configuration->General->Store Email Addresses"
  */
  $sender = Array('name'  => 'S. M. Asad Rahman',
                  'email' => 'asad.dk.bd@gmail.com');

  /**
  * In case of multiple recipient use array here.
  */
  $email = 'smasadrahman@yahoo.com';

  /**
  * If $name = null, then magento will parse the email id
  * and use the base part as name.
  */
  $name = 'Asad Rahman';

  $vars = Array();
  /* An example how you can pass magento objects and normal variables*/
  /*
  $vars = Array('customer'=>$customer,
                'address' =>$address,
                'varification_data'=>'fake data for example');*/

  /*This is optional*/
  $storeId = Mage::app()->getStore()->getId(); 

  $translate  = Mage::getSingleton('core/translate');
  Mage::getModel('core/email_template')
      ->setTemplateSubject($mailSubject)
      ->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
  $translate->setTranslateInline(true);

Now lets put email template information in config.xml of the corresponding module.

<?xml version="1.0"?>
<config>
<!-- Other config infos 
goes here
 .
 .
 .-->
   <global>
    <!-- Other config infos 
    goes here
    .
    .
    .-->
    <template>
      <email>
        <customer_testemail_email_template translate="label" module="mymodulename">
          <label>Test email sending</label>
          <file>test_email_template.html</file>
          <type>html</type>
          </customer_testemail_email_template>
        </email>
      </template> 
    </global>
 </config>

This config xml says there is a email template in “app/locale/en_US/template/email” there is a email template named “test_email_template.html”, so lets create the template file:-

<div style="font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;">
  <table cellspacing="0" cellpadding="0" border="0" width="98%" style="margin-top:10px; font:11px/1.35em Verdana, Arial, Helvetica, sans-serif; margin-bottom:10px;">
    <tr>
      <td align="center" valign="top">
        <!-- [ header starts here] -->
          <table cellspacing="0" cellpadding="0" border="0" width="650">
            <tr>
              <td valign="top">
                <p>
                  <a href="{{store url=""}}" style="color:#1E7EC8;"><img src="{{skin url="images/logo_email.gif" _area='frontend'}}" alt="Magento" border="0"/></a>
                </p>
              </td>
            </tr>
          </table>

          <!-- [ middle starts here] -->
          <table cellspacing="0" cellpadding="0" border="0" width="650">
            <tr>
              <td valign="top">
                <p>
                <strong>Dear {{var customer.name}}</strong>,<br/>
                This is a test mail.:-)
                </p>
              </td>
            </tr>
          </table>

      </td>
    </tr>
  </table>
</div>

In config.xml we set values for “customer_testemail_email_template”, but in code we use Mage::getStoreConfig(“customer/testemail_email/template”) to set $templateId. So we need to create a relation in between two. To do so we will create a installer to insert a data in db config.

<?php

$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$configValuesMap = array(
  'customer/testemail_email/template' =>
  'customer_testemail_email_template',
);

foreach ($configValuesMap as $configPath=>$configValue) {
    $installer->setConfigData($configPath, $configValue);
}

That’s it. Now this code snippet is good enough to send a customized mail. If you want the facility to set email template from admin panel, use the following system.xml in your module.

<?xml version="1.0"?>
<config>
  <sections>
    <customer>
      <groups>
        <testemail translate="label">
          <label>Apply Non-Profit Account Options</label>
          <sort_order>100</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
          <fields>
            <email_template translate="label">
              <label>Email Template</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>
            </email_template>
          </fields>
        </testemail>
      </groups>
    </customer>
  </sections>
</config>

That’s it !!! I hope this post will help you sending emails from magento. Please leave comments :-) .

About these ads