cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 1.9 Observer - PHP 7 mail function how to set multiple recipient mail id based on condition?

Magento 1.9 Observer - PHP 7 mail function how to set multiple recipient mail id based on condition?

Following code for sending multiple recipient mail based on first digit of sku from order received,

 

<?php
class Gta_MerchantNotification_Model_Observer {
  public function merchantremainder($Observer) {

    $order = $Observer->getEvent()->getOrder();
    $order_details = $order->getAllVisibleItems();

    $itemData = array();

    foreach ($order_details as $list) {
      $incrementid = $order->getIncrementId();
      $sku = $list->getsku();
      $name = $list->getName();
      $price = $list->getPrice();
      $Qty = $list->getQtyOrdered();

      // $this->sendMailbasedOnSku($sku);

      $message = 
      "<tr>
      <!-- <td>$incrementid</td> -->
      <td>$sku</td>
      <td>$name</td>
      <td>$price</td>
      <td>$Qty</td>
      </tr>";

      $itemData[$list->getId()] = $message;

    }

    $finalMessage = 
    "<p>Order Id : $incrementid</p>
    <table border='1'>
    <tr>
    <!-- <th>Id</th> -->
    <th>Sku</th>
    <th>Product name</th>
    <th>Price</th>
    <th>Qty Ordered</th>
    </tr>";

    if (!empty($itemData)) {
      foreach ($itemData as $data) {
        $finalMessage .= $data;
      }

      $finalMessage .= "</table>";

      // $this->sendMail($finalMessage);

      $this->sendMailbasedOnSku($finalMessage,$sku);

    }

  }

  public function sendMail($message) {

    $body = "$message";
    $emailTemplate = Mage::getModel('core/email');
    $emailTemplate->setFromName('Test mail');
    $emailTemplate->setBody($body);
    $emailTemplate->setSubject("Custom Email from observer");
    $emailTemplate->setType('html');

    if($sku == '2')
    {
      $emailTemplate->setToEmail('abc@gmail.com');

    }

    elseif($sku == '3')
    {
      $emailTemplate->setToEmail('xyz@gmail.com');  

    }

    elseif($sku == '4')
    {
      $emailTemplate->setToEmail('qwe@gmail.com');   
    }

    else
    {
      $emailTemplate->setToEmail('ewq@gmail.com');
    }

    $emailTemplate->send();
  }


  public function sendMailbasedOnSku($message, $sku)
  {

    $body = "$message";
    $emailTemplate = Mage::getModel('core/email');
    $emailTemplate->setFromName('Test mail');
    $emailTemplate->setBody($body);
    $emailTemplate->setSubject("Custom Email from observer");
    $emailTemplate->setType('html');

    $chk_sku=(int)substr($sku, 0, 1);

    if($chk_sku == '2')
    {
      $emailTemplate->setToEmail('abc@gmail.com');
    }

    elseif($chk_sku == '3')
    {
      $emailTemplate->setToEmail('xyz@gmail.com');    
    }

    elseif($chk_sku == '4')
    {
      $emailTemplate->setToEmail('qwe@gmail.com');    
    }

    else{
      $emailTemplate->setToEmail('ewq@gmail.com');
    }
    return $emailTemplate->send();

    // try{
    //   return $emailTemplate->send();
    //   Mage::getSingleton('core/session')->addSuccess('Success message');
    // }catch (Exception $e)
    // {
    //   Mage::getSingleton('core/session')->addError($e->getMessage());
    // }
  }
}
?>

eg: If an order contains both 2 series of sku and 3 series of sku, order mail received only by 3 series of sku mail and not received 2 series of sku mail.

another eg. if order contains 22 (abc@gmail.com),33 (xyz@gmail.com),44 (qwe@gmail.com) order received only qwe@gmail.com

 

https://i.stack.imgur.com/qhI7i.png

 

My condition, if the order contains 2 products means to send a separate mail.

like

2 series of sku :

 

https://i.stack.imgur.com/eEoba.png

 

3 series of sku :

 

https://i.stack.imgur.com/McPVH.png

 

4 series of sku

 

https://i.stack.imgur.com/NZtQ0.png

 

 

3 REPLIES 3

Re: Magento 1.9 Observer - PHP mail function how to set sender mail id based on if condition?

Hi @Aveeva 

You are missing following code line in your sendMailbasedOnSku method.

 public function sendMailbasedOnSku($sku)
  {

   // sku is 22302

    $chk_sku=(int)substr($sku, 0, 1);
    $emailTemplate = Mage::getModel('core/email');   // This line is missing

 

 

---
Problem Solved Click Accept as Solution!:Magento Community India Forum

Re: Magento 1.9 Observer - PHP mail function how to set sender mail id based on if condition?

@Mukesh Tiwari  After added $emailTemplate = Mage::getModel('core/email');  mail working, but if condition not working. As per my condition if sku start with 2 mail should go to abc@gmail.com but here mail received ewq@gmail.com (Final email id in my if condition). How to correct my script?

Re: Magento 1.9 Observer - PHP mail function how to set sender mail id based on if condition?

@Mukesh Tiwari  Check with my updated post.