3

I created a new module to overwrite the AddressController.php controller.

Here I make some changes and i receive some data from the customer address form. What I want to do now is to take all the data and send it via email.

My code for sending mail from controller looks like this:

$templateId = 17;
$emailTemplate = Mage::getModel('core/email_template')->load($templateId);
$mail = Mage::getModel('core/email');
$mail->setToName($customer->getId());

$email_template_variables = array(
   'first' => $first_name,
   'middlename' => $middle_name,
   'lastname' => $last_name,
   'company' => $company,
   'telephone' => $phone,
   'fax' => $fax,
   'street' => $street[],
   'city' => $city,
   'country_id' => $contry,
   'region' => $state
);
$mailSubject = 'Change billing address';
$from_email = "[email protected]";
$from_name = "testt";
$processedTemplate = $emailTemplate->getProcessedTemplate($email_template_variables);

$mail = Mage::getModel('core/email');
$mail->setToName('nume');
$mail->setTemplateParam($email_template_variables);
$mail->setToEmail('[email protected]');
$mail->setBody($processedTemplate);
$mail->setSubject($mailSubject);
$mail->setFromEmail($from_email);
$mail->setFromName($from_name);
$mail->setType('Html');

try {
    $mail->send();
    Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
} catch (Exception $ex) {
    Mage::getSingleton('core/session')->addError('Unable to send.');
}

Now my question is:

How can I create a new email template in my custom module and send those data? Because if I try to create a new email template from System > Transactional Email. it asks me to load a existing template. But I don't want this. I want to create a new one in my theme.

Can anybody help me with a solution?

2 Answers 2

6

To define your own custom email template, you need to define it in app/code/<codepool>/<packagename>/<modulename>/etc/config.xml file.

Below is sample code:

<template>
    <email>
        <atwixmodule_email_template translate="label" module="atwixmodule">
            <label>Atwix Email</label>
            <file>atwix_email_template.html</file>
            <type>html</type>
        </atwixmodule_email_template>
    </email>
</template>

After that you would need to create an HTML file under app/locale//template/email folder with name defined under tag as defined above.

Now you can use this file in your module.

There are many reference links to help you.

One is given below:

https://www.atwix.com/magento/email-sending-feature/

Note: Links and samples used in this answer are not for promoting any particular website. This is just for reference and to help faster.

2
  • i already did this. but in my code when i am sendig the email, i use an id. As long i can't see my custom email template in backend, how can i know which ID use my custom template? Commented May 29, 2017 at 13:19
  • 1
    This may help you directly, but either way for others as well, this section belongs within the <global /> section in your config. Commented Aug 6, 2017 at 19:26
0

Step 1. create new file under : app/locale/en_US/template/emai/template/custom_template.html

<body style="font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;"></body>

Step 2. app/code/core/Mage/Customer/etc/config.xml

 <customer_custom_template translate="label" module="customer">
    <label>Custom Template</label>
    <file>custom_template.html</file>
    <type>html</type>
</customer_custom_template>

Step 3. in Controller or model or Observer.php

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

$senderName = Mage::getStoreConfig('trans_email/ident_general/name'); $senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

$emailTemplateVariables = array(); $emailTemplateVariables['name'] = $customerName; $emailTemplateVariables['email'] = $customerEmail; $emailTemplate->setSenderName($senderName); $emailTemplate->setSenderEmail($senderEmail); $emailTemplate->send($reciepentEmail, $reciepentName, $emailTemplateVariables);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.