3

I have created a custom form that will post to a controller, where it will then send an email via the following snippet in my post action:

$to = "[email protected]";
$subject = "Vish";
$body = "<img src='logo.jpg' >";
$body .= "<p>Name : " . $name . "<p>";
$body .= "<p>Email  : " . $email . "<p>";
$body .= "<p>Address Line 1 : " . $address_line_1 . "<p>";
$body .= "<p>Address Line 2 : " . $address_line_2 . "<p>";
$body .= "<p>Address Line 3 : " . $address_line_3 . "<p>";
$body .= "<p>City : " . $city . "<p>";
$body .= "<p>Post Code : " . $postcode . "<p>";
$body .= "<p>Country : " . $country . "<p>";
$body .= "<br /><p>Kind regards,</p><p>vish</p>"; 
$from = '[email protected]';
            $mail = Mage::getModel('core/email');
            $mail->setToName("Vish");
            $mail->setToEmail($to);
            $mail->setBody($body);
            $mail->setSubject($subject);
            $mail->setFromEmail($from);
            $mail->setType('html');
            $mail->setBodyHTML($body);

            $mail->send();

This works well, but I am not sure if it is the correct way to do this or if there is a better standardised way?

I have seen and attempted this way but the email body is empty:

    $emailTemplate  = Mage::getModel('core/email_template')
                        ->loadDefault('vish_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'] = $name;
$emailTemplateVariables['email'] = $email;

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

//Sending E-Mail to Customers.
$mail = Mage::getModel('core/email')
 ->setToName($senderName)
 ->setToEmail($email)
 ->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());
 }

I have got it working rather splendidly via:

$mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->setType('Html')
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        "Vish Sales",
                        array('data' => $postObject)
                    );

                if (!$mailTemplate->getSentSuccess()) {
                    throw new Exception();
                } else{
                    $subject = "Vish Request";


                $body = "
<p>Thank you for requesting our new catalogue, which will be posted out to you immediately.</p>"

                $sender = Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER);
                $mail = Mage::getModel('core/email');
                $mail->setToName($name);
                $mail->setFromName("no-reply");
                $mail->setToEmail($email);
                $mail->setReplyTo('[email protected]');
                $mail->setBody($body);
                $mail->setSubject($subject);
                $mail->setFromEmail(Mage::getStoreConfig('trans_email/ident_' . $sender . '/email'));
                $mail->setType('html');
                $mail->setBodyHTML($body);

                $mail->send();
                }

However, I am not sure if the section below is done in a correct manner:

    $subject = "Vish Request";


                $body = "
<p>Thank you for requesting our new catalogue, which will be posted out to you immediately.</p>"

                $sender = Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER);
                $mail = Mage::getModel('core/email');
                $mail->setToName($name);
                $mail->setFromName("no-reply");
                $mail->setToEmail($email);
                $mail->setReplyTo('[email protected]');
                $mail->setBody($body);
                $mail->setSubject($subject);
                $mail->setFromEmail(Mage::getStoreConfig('trans_email/ident_' . $sender . '/email'));
                $mail->setType('html');
                $mail->setBodyHTML($body);

                $mail->send();

Thus, could someone confirm if this is an acceptable way or how could I do the same as the above but for multiple templates?

1 Answer 1

0

For your code you have to define email template in config.xml like below:

<global>
...
<template>
    <email>
        <vish_email_template translate="label" module="eztestimonial">
            <label>Custom Email Template</label><!-- this should be shown in the config dropdown-->
            <file>mymodule/custom_email.html</file>
            <type>html</type>
        </vish_email_template>
    </email>
</template>
...
</global>

OR use below code by creating transaction email in admin:

$templateId = 1; //Go to admin >> system >> transaction and create and template and use its id.
$storeId = Mage::app()->getStore()->getId();
$senderName = Mage::getStoreConfig('trans_email/ident_support/name');
$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
$sender = array('name' =>saurabh,'email'=>'[email protected]');
$receipentName = 'Saurabh';
$receipentEmail = '[email protected]';
$vars=array('customername'=>'saurabh',
'customeremail'=>'[email protected]');
$translate = Mage::getSingleton('core/translate');
Mage::getModel('core/email_template')->sendTransactional($templateId, 
$sender, $recepientEmail, $recepientName, $vars, $storeId);
$translate->setTranslateInline(true);

Hope this will helps you !!!

1
  • Please see updated question. Commented Aug 3, 2018 at 10:21

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.