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 = "test@gmail.com";
$from_name = "testt";
$processedTemplate = $emailTemplate->getProcessedTemplate($email_template_variables);

$mail = Mage::getModel('core/email');
$mail->setToName('nume');
$mail->setTemplateParam($email_template_variables);
$mail->setToEmail('test@gmail.com');
$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?

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.

  • 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? – Alin Lupoiu May 29 '17 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. – Tobias Hagenbeek Aug 6 '17 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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