Skip to main content
2 of 3
added 2779 characters in body
Vaishal Patel
  • 765
  • 11
  • 33

Magento 1.9 Best way to send email within custom module

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?

Vaishal Patel
  • 765
  • 11
  • 33