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

Well, your sendmailbasedonsku function doesn’t set the email body text anywhere. Compare:

 public function sendMail($message) {

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

and

public function sendMailbasedOnSku($sku)
  {
    Mage::log('Line no: 94 '. $sku,null,'test_sku.log',true);
    $chk_sku=(int)substr($sku, 0, 1);
    $emailTemplate = Mage::getModel('core/email');

That’s coming from sendMail(), even though you have all the checks to see which email to send it to, your email message body there comes from $finalMessage, which starts with <p>, so all your sku checks drop through to your else. I don’t understand why you look at the sku when you’re sending the final message, because it contains all the skus on the order.

My logic :

All my sku starting digit represent vendor,

2 represent - Metal Items,
3 - represent - Books,
4 - represent - Songs

if order received dedicated products should go to the vendors by mail,

Eg. If order contains 3 products

  1. 235666 (Metal Items)
  2. 34577(Books)
  3. 474637(Songs)

FYI - Order id same.

  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.

Yes, I saw that earlier, but the code to send the email isn’t correct. Compare the two functions.

Now 95% working exactly, if order placed with two products with sku combination like 2 & 3, Now mail received by exact both mail - abc@gmail.com (sku start with - 2) & xyz@gmail.com (sku start with - 3) but empty message, and also as you know last mail id in if condition received with data,

Off Topic:

@aveevan: please either use example.com for your example email addresses, or enclose them in backticks ` to prevent the forum software automatically creating mailto links.

Thank you.

@TechnoBear Okay, i will update my further post, Thank You.

1 Like

FYI - updated 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@example.com');

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

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

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

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

    $emailTemplate->send();
  }


  public function sendMailbasedOnSku($sku)
  {

    // Mage::log('Line no: 94 '. $sku,null,'test_sku.log',true);

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

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

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

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

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

    else{
      $emailTemplate->setToEmail('ewq@example.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());
    // }
  }
}

?>

As I said before, your sendmailbasedonsku() function doesn’t set the message body. Compare the start of the sendmail function to see how it differs in its handling of $emailTemplate.

As I said earlier, that’s because you send $finalMessage using the sendmail() function, and in that function you’re still setting the destination address by looking at $sku, except that you don’t pass $sku into the function. Hence (presumably) it hits the else, though I’d just expect error messages unless the way you call the code is suppressing them somehow.

But you still have a problem - if you have two items in the order that start with the same digit, you’ll get a separate email for each of them, rather than getting them both on the same email.

1 Like

After load message body to sendmailbasedonsku(),
final 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);

    }

  }

  public function sendMail($message) {

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

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

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

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

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

    $emailTemplate->send();
  }


  public function sendMailbasedOnSku($sku)
  {

    // Mage::log('Line no: 94 '. $sku,null,'test_sku.log',true);

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

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

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

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

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

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

  }
}

?>

What is need to further?

Everything I said in the last post is still wrong. You still don’t set the email message body or other settings in the ...bysku() function, and you still have conditional destination addresses in the summary email function that I don’t think you need to have. You now send the final message twice, for some reason.

Where do you do that? This is your function code:

  public function sendMailbasedOnSku($sku)
  {
    // Mage::log('Line no: 94 '. $sku,null,'test_sku.log',true);
    $chk_sku=(int)substr($sku, 0, 1);
    $emailTemplate = Mage::getModel('core/email');

… followed by your if…then section to set the to-address. I don’t see where you specify the message body.

Or, to put it another way, how does it work now? That is, does it do what you want it to? If it does, there’s nothing further to do.

May i know how to load message to sendMailbasedOnSku()? Getting confused thats what wrong code, any help thanks.

Just the same way that you do it in sendMail() presumably - you have these lines at the top of that function:

public function sendMail($message) {

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

so presumably you have to have all of those in sendmailbasedonsku() as well.

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

You’ve changed the code now so that it doesn’t actually send the email for each item on the order based on the sku, now it only sends the mail at the end. By the time it gets there, the value of $sku is whatever it was at the end of the loop, so that’s how the decision is made on where to send it. You were closer with the previous code, in that it would at least send separate messages.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.