Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom email sending in magento custom module

Tags:

email

magento

I am working on a module that will send an email after 7 days of order completion. I'm stuck on sending emails. I can see the email template in transactional emails drop down in admin. But the email is not being sent.

Here is my confix.xml part for including email template.

<template>
        <email>
            <recurring_order_email_template translate="label">
                <label>Recurring order email</label>
                <file>coeus_recurring_order_email.html</file>
                <type>html</type>
            </recurring_order_email_template>
        </email>
    </template>

and this is how I am sending email in controller action

 $emailTemplate = Mage::getModel('core/email_template')
            ->loadDefault('coeus_recurring_order_email');

    $emailTemplateVariables = array();
    $emailTemplateVariables['var1'] = 'var1 value';
    $emailTemplateVariables['var2'] = 'var 2 value';
    $emailTemplateVariables['var3'] = 'var 3 value';

    $emailTemplate->getProcessedTemplate($emailTemplateVariables);

   $emailTemplate->setSenderName('sender name');
    $emailTemplate->setSenderEmail('[email protected]');
    try {
        $emailTemplate->send('[email protected]', 'bla bla',$emailTemplateVariables);
    } catch (Exception $e) {
        echo $e->getMessage();
    }  

I don't know why its not working.

like image 595
murtza gondal Avatar asked Feb 28 '14 05:02

murtza gondal


3 Answers

 $emailTemplate = Mage::getModel('core/email_template')->loadDefault('recurring_order_email_template');

//Getting the Store E-Mail Sender Name.
$senderName = Mage::getStoreConfig('trans_email/ident_general/name');

//Getting the Store General E-Mail.
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

//Variables for Confirmation Mail.
$emailTemplateVariables = array();
$emailTemplateVariables['name'] = $customerName;
$emailTemplateVariables['email'] = $customerEmail;

//Appending the Custom Variables to Template.
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);

//Sending E-Mail to Customers.
$mail = Mage::getModel('core/email')
 ->setToName($senderName)
 ->setToEmail($customerEmail)
 ->setBody($processedTemplate)
 ->setSubject('Subject :')
 ->setFromEmail($senderEmail)
 ->setFromName($senderName)
 ->setType('html');
 try{
 //Confimation E-Mail Send
 $mail->send();
 }
 catch(Exception $error)
 {
 Mage::getSingleton('core/session')->addError($error->getMessage());
 return false;
 }
like image 197
Maniprakash Chinnasamy Avatar answered Oct 18 '22 02:10

Maniprakash Chinnasamy


Change your etc/config.xml code to below:

<template>
        <email>
            <recurring_order_email_template>
                <label>Recurring order email</label>
                <file>coeus_recurring_order_email.html</file>
                <type>html</type>
            </recurring_order_email_template>
        </email>
    </template>

Change your controller code to below:

$emailTemplate = Mage::getModel('core/email_template')
            ->loadDefault('recurring_order_email_template');

    $emailTemplateVariables = array();
    $emailTemplateVariables['var1'] = 'var1 value';
    $emailTemplateVariables['var2'] = 'var 2 value';
    $emailTemplateVariables['var3'] = 'var 3 value';

   $emailTemplate->getProcessedTemplate($emailTemplateVariables);

   $emailTemplate->setSenderName('sender name');
   $emailTemplate->setSenderEmail('[email protected]');
    try {
   $emailTemplate->send($recipientEmail, $senderName, $emailTemplateVariables);
    } catch (Exception $e) {
        echo $e->getMessage();
    } 

Change your $recipientEmail, $senderName & $emailTemplateVariables as per your need.

To load a email template, you must specifiy the tag name after

<template>
            <email>
       </email>
</template>

that you provided in the config.xml

like image 4
Slimshadddyyy Avatar answered Oct 18 '22 04:10

Slimshadddyyy


I think you made a mistake here.

$emailTemplate = Mage::getModel('core/email_template')->loadDefault('coeus_recurring_order_email');

try this

$emailTemplate = Mage::getModel('core/email_template')
            ->loadDefault('recurring_order_email_template');

to load a email template you have to give the tag name that you provide in the config.xml

eg: in you code

use

<recurring_order_email_template>

to load email template

like image 1
MeenakshiSundaram R Avatar answered Oct 18 '22 02:10

MeenakshiSundaram R