1

I am using Magento 1.9.2.1. I have created one custom form using CMS page.when the form will submit it will send email to respective customer. so i have tried below code to achieve that:

controller.php

<?php

class Shasha_CorporateAccount_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        if ($data = $this->getRequest()->getPost())
        {
            //echo '<pre>';print_r($data);die;
            try
            {
            $msg_html=  "<table>";
                $msg_html.= "<tr><td>Name</td><td>".$_POST['first_name']." ".$_POST['last_name']."</td></tr>";
                $msg_html.= "<tr><td>Email</td><td>".$_POST['email']."</td></tr>";
                $msg_html.= "<tr><td>Phone</td><td>".$_POST['phone']."</td></tr>";
                $msg_html.= "<tr><td>Company</td><td>".$_POST['company']."</td></tr>";
                ///$msg_html.=  "<tr><td colspan='2'>Note<br>".$_POST['comment']."</td></tr>";
                $msg_html.= "</table>"; 

                $to_email = "shasha@gmail.com";
                $to_name  = "Shasha";
                $subject  = 'Corporate Account Request';
                $Body     = $msg_html;
                $sender_email = $_POST['email'];
                $sender_name = $_POST['first_name'];

                //Sending E-Mail to Customers.
                $mail1 = Mage::getModel('core/email')
                ->setToName($to_name)
                ->setToEmail($to_email)
                ->setBody($Body)
                ->setSubject('Subject :'.$subject)
                ->setFromEmail($sender_email)
                ->setFromName($sender_name)
                ->setType('html');
                try{
                //Confimation E-Mail Send
                $mail1->send();

                }
                catch(Exception $error)
                {
                Mage::getSingleton('core/session')->addError($error->getMessage());

                }



                $message = $this->__('Thank you for the request, Will get back to you asap.');
                Mage::getSingleton('core/session')->addSuccess($message);
                $this->_redirectReferer();
                return;
            } catch (Exception $e) {
                $message = $this->__('Error to send request.');
                Mage::getSingleton('core/session')->addError($message);
                //$this->_redirect();
                $this->_redirectReferer();
                return;
            }


        }

    }

}
?>

if i put this code

if($mail1->send()){
   die(Email is sent successfully);
}

in this response i also get Email is sent successfully but unfortunately customer is not getting any email.

I am unable to find out the correct solution.

Please if anyone can help me on this.

Thanks in advance...!!!

0

I would suggest check contact us or newsletter emails are working if yes then it might be an issue with your code.

Are you getting any exception error or any logs? If yes then you should fix the error and try again.

Compare your code with the below websites.

http://sapnandu-magento.blogspot.com/2011/09/send-mail-from-magento.html

https://magecomp.com/blog/send-email-with-attachment-magento-without-saving-file-to-webserver/

Hope this will help you.

  • i have used the exact same code which is mentioned in above urls. also i have check after execute this code there is no exception in logs. – Balwant Singh Sep 26 at 18:57
  • @BalwantSingh have you checked contact us email and newsletter email are working fine? – Nits Sep 27 at 5:57

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.