0

Magento How to send a mail to a custom person after Place order

I tried below code in observer.php but it's not working

 <?php 

class Manoj_Customemail_Model_Observer{
     public function customMail($event)
    {
        $order = $event->getOrder();
       //add your mail send code here

        $html="put your html content here blah blah";
$mail = Mage::getModel('core/email');
$mail->setToName('Your Name');
$mail->setToEmail('[email protected]');
$mail->setBody('Mail Text / Mail Content');
$mail->setSubject('Mail Subject');
$mail->setFromEmail('Sender Mail Id');
$mail->setFromName("Msg to Show on Subject");
$mail->setType('html');// YOu can use Html or text as Mail format

try {
$mail->send();

    }
}

}

confix.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Manoj_Customemail>
          <version>0.1.0</version>
        </Manoj_Customemail>
      </modules>
      <global>
        <helpers>
          <customemail>
            <class>Manoj_Customemail_Helper</class>
          </customemail>
        </helpers>

 <models>
            <customemail>
                <class>Manoj_Customemail_Model</class>
            </customemail>
        </models>



         <events>
                <sales_order_place_after>
                    <observers>
                        <sales_order_observer>
                            <type>singleton</type>
                            <class>Manoj_Customemail_Model_Observer</class>
                            <method>customMail</method>
                        </sales_order_observer>
                    </observers>
                </sales_order_place_after>
            </events>

      </global>
    </config> 

After Place order it redirect to cart page

4
  • do you have a fix custom id or it may be change ? Commented Jun 23, 2016 at 14:10
  • Sorry i am not getting
    – Magento 2
    Commented Jun 23, 2016 at 14:13
  • try public function customMail(Varien_Event_Observer $observer) in place of public function customMail($event) Commented Jun 24, 2016 at 4:59
  • Still same it redirect to shopping cart only
    – Magento 2
    Commented Jun 24, 2016 at 12:25

2 Answers 2

0

You need to observe below event

       <events>
            <sales_order_place_after>
                <observers>
                    <sales_order_observer>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>customMail</method>
                    </sales_order_observer>
                </observers>
            </sales_order_place_after>
        </events>

Create your observer as below:

class Namespace_Modulename_Model_Observer{
     public function customMail($event)
    {
        $order = $event->getOrder();
       //add your mail send code here

    }
}
1
  • check my updated question
    – Magento 2
    Commented Jun 23, 2016 at 14:32
0

You can create a simple magento extension that contains an observer on sales_order_place_after event and then you can use zend_mail to send a custom email. Something like

$fromEmail = "[email protected]"; // sender email address
$fromName = "John Doe"; // sender name

$toEmail = "[email protected]"; // recipient email address
$toName = "Mark Doe"; // recipient name

$body = "This is Test Email!"; // body text
$subject = "Test Subject"; // subject text

$mail = new Zend_Mail();        

$mail->setBodyText($body);

$mail->setFrom($fromEmail, $fromName);

$mail->addTo($toEmail, $toName);

$mail->setSubject($subject);

try {
    $mail->send();
}
catch(Exception $ex) {
    // I assume you have your custom module. 
    // If not, you may keep 'customer' instead of 'yourmodule'.
    Mage::getSingleton('core/session')
        ->addError(Mage::helper('yourmodule')
        ->__('Unable to send email.'));
}

This could be a quick fix but I recommend to create a custom email template in magento. Maybe Inchoo's custom email template post can get you started

5
  • Where i need to add email code
    – Magento 2
    Commented Jun 23, 2016 at 13:52
  • Create a new Magento extension. If you need help, have a look here. At Step 2 change <sales_order_place_before> with <sales_order_place_after>. You email code will go in Step 4 public function example($observer)
    – MMScreenX
    Commented Jun 23, 2016 at 14:06
  • check my updated question
    – Magento 2
    Commented Jun 23, 2016 at 14:32
  • You will have to learn how Magento modules work. It's very clear that you are missing Model and code is incorrect. If you follow the instructions/steps from my previous comment you should be able to do it.
    – MMScreenX
    Commented Jun 23, 2016 at 14:36
  • sorry i missed Model code still same
    – Magento 2
    Commented Jun 23, 2016 at 14:39

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.