PHP mail function how to set recipient email id based on if condition?

I am using magento for sending mail with condition,

My code:

<?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();
          $extra = $order->getIncrementId();
          $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);
     }

 }

 public function sendMail($message) {
   $body ="$message";
   $emailTemplate = Mage::getModel('core/email');
   $emailTemplate->setFromName('abc');
   $emailTemplate->setBody($body);
   $emailTemplate->setSubject("Custom Email from observer");
   $emailTemplate->setType('html');
   $emailTemplate->setToEmail('abc@gmail.com');
   $emailTemplate->send();

}
}

?>

Output :

If order placed mail send to abc@gmail.com.
How to set recipient email based on SKU $sku from order.

I want :

  1. If SKU starts with 2, email should go to the mail id abc@gmail.com,
    screenshot :

  1. If SKU starts with 3, email should go to the mail id xyz@gmail.com,
    screenshot :

  1. If SKU starts with 4, email should go to the mail id qwe@gmail.com,
    screenshot :

FYI - If an order contains 10 items email should go separately based on SKU. But an order id the same must include all the emails.

You would need to create additional methods that

  1. decide whom to send the email to based on a rules factory.
  2. in that or another method get the count and type of product so the email function sends to the appropriate entity.
    You will also need to modify the sendMail method so that the sendTo is a parameter rather than hard coded in.
1 Like

Any help with my error,

Code :

<?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);

    }

  }

  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');
// $emailTemplate->setToEmail('abc@gmail.com');

    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($sku)
  {

    $emailTemplate = Mage::getModel('core/email');

    $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');
    }

    try{
    return $emailTemplate->send();
    Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
  }

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

  }



}

?>

Error :

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?

How to solve the error?

is the sku a single digit or multiple digits? And wouldn’t the $sku be an array not a single item?

<?php


function sendEmail($to, $msg) {
	echo "$msg $to\n";
}



$skus = [
	'1234',
	'2341',
	'43253',
	'82344',
	'0001',
];
	
$dept = [
	2 => '2@email.com',
	3 => '3@email.com',
	4 => '4@email.com',
];
	
	
foreach($skus as $sku) {
	$digit = substr($sku, 0,1);
	if(array_key_exists($digit, $dept)) {
		sendEmail($dept[$digit], 'Send fulfillment order to ');	
	}
	else {
		sendEmail('general@email.com', 'No specific department handles this. Send it to the general email.');
    }
}

My sku like,

23776557,
348u8573,
45986857,
58868876,
and so on…

And I showed how to do it, with a working example…

Now mail send with the body of the message but multiple recipient mail not sending,

eg: If an order contains, 2 series of sku and 3 series of sku order mail received only by 3 series of sku mail 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

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

like

2 series of sku :

3 series of sku :

4 series of sku

Code:

<?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());
        // }
      }
    }
    ?>

In the case of needing to send to multiple recipients, your email code will overwrite the previous value rather than adding to it. Can you add multiple “setToEmails”?

Sponsor our Newsletter | Privacy Policy | Terms of Service