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('[email protected]');
   $emailTemplate->send();

}
}

?>

Output :

If order placed mail send to [email protected].
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 [email protected],
    screenshot :

  1. If SKU starts with 3, email should go to the mail id [email protected],
    screenshot :

  1. If SKU starts with 4, email should go to the mail id [email protected],
    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('[email protected]');

    if($sku == '2')
    {
      $emailTemplate->setToEmail('[email protected]');
    }

    elseif($sku == '3')
    {
      $emailTemplate->setToEmail('[email protected]');    
    }

    elseif($sku == '4')
    {
      $emailTemplate->setToEmail('[email protected]');    
    }

    else
    {
      $emailTemplate->setToEmail('[email protected]');
    }



    $emailTemplate->send();


  }

  public function sendMailbasedOnSku($sku)
  {

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

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

    if($chk_sku == '2')
    {
      $emailTemplate->setToEmail('[email protected]');
    }

    elseif($chk_sku == '3')
    {
      $emailTemplate->setToEmail('[email protected]');    
    }

    elseif($chk_sku == '4')
    {
      $emailTemplate->setToEmail('[email protected]');    
    }

    else{
      $emailTemplate->setToEmail('[email protected]');
    }

    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 [email protected] but here mail received [email protected] (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 => '[email protected]',
	3 => '[email protected]',
	4 => '[email protected]',
];
	
	
foreach($skus as $sku) {
	$digit = substr($sku, 0,1);
	if(array_key_exists($digit, $dept)) {
		sendEmail($dept[$digit], 'Send fulfillment order to ');	
	}
	else {
		sendEmail('[email protected]', '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 ([email protected]),33 ([email protected]),44 ([email protected]) order received only [email protected]

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('[email protected]');
          
        }

        elseif($sku == '3')
        {
          $emailTemplate->setToEmail('[email protected]');  
          
        }

        elseif($sku == '4')
        {
          $emailTemplate->setToEmail('[email protected]');   
        }

        else
        {
          $emailTemplate->setToEmail('[email protected]');
        }

        $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('[email protected]');
        }

        elseif($chk_sku == '3')
        {
          $emailTemplate->setToEmail('[email protected]');    
        }

        elseif($chk_sku == '4')
        {
          $emailTemplate->setToEmail('[email protected]');    
        }

        else{
          $emailTemplate->setToEmail('[email protected]');
        }
        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”?

@astonecipher Really sorry for the late reply, multiple “setToEmails” mean?

Can you send it to multiple recipients or can you only add one email to it?

If an order consists of 3 different SKU like 2,3,4 multiple email should go based on 22 ([email protected]),33 ([email protected]),44 ([email protected])

I understand that. What I don’t understand is the library you are using to send the emails…

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

The last line says whom to send it to. Can that only be one person or is an an array?

How to edit my code if i need to send multiple mail recipient?
eg: If single order consists 4 items like wise 22,33,44,55 , 22 send to [email protected], 33 send to [email protected], 44 send to [email protected], 55 send to [email protected].

I’m out. Either you aren’t listening or are making no attempt to get through the language barrier. I have asked a question multiple times and the only answer I get is what you want to do rather than an actual answer.

Sponsor our Newsletter | Privacy Policy | Terms of Service