Jump to content

Sending emails to multiple email ids with nested loop


Go to solution Solved by mac_gyver,

Recommended Posts

I am trying to send email from multiple email addresses using mail() function and it is working fine. 

But email ids are coming from database as array. 

Here is my code

if(isset($_POST['Submit']))
    { 

 foreach($_POST['vendor'] as $k)        //$_POST['vendor'] will be having multiple vendor ids
      {    
      
       $vw = $con->prepare("SELECT email FROM ven_contacts WHERE vendor=?");   //getting all the email ids of vendors
    $vw->execute([$k]);
foreach($vw as $ems)
    {
      $output = $ems;  
       echo implode(', ', $output); 

    }
      } 

  When i echo $output it shows each email id   2 times for eg: abc@abc.com, abc@abc.comxyz@xyz.com, xyz@xyz.com So my email script doesn't work.

if try to get $output outside the loop like this

foreach($vw as $ems)
    {
      $output = $ems;  
     

    }

 echo implode(', ', $output);  
      } 

still the same problem. 

I want email ids to be

abc@abc.com, xyz@xyz.com

Then only my email script

         

 $to = $emailids;  
         $subject = "Email to multiple vendors";
         
         $message = "<p>Hello All";
         $message .= "Welcome to our Portal";
         $message .= "Thank you";
         
        $headers = "";
        $header = "From:info@mydomain.com \r\n";
        $headers .= "From: Info <info@mydomain.com> \r\n";
        $headers .= "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html\r\n";
         
        $retval = mail ($to,$subject,$message,$headers);

This email script works if i put email ids directly like this 

mail("abc@abc.com, xyz@xyz.com",$subject, $message, $headers);

Can somebody suggest me how to get the array outside the loop

 

  • Solution
35 minutes ago, mythri said:
SELECT email FROM ven_contacts WHERE vendor=?

how many email addresses per vendor? if there's only one, you don't need the 2nd foreach() loop to fetch it, just directly fetch the single row without using a loop.

next you don't even need the 1st foreach() loop, just implode $_POST['vendor'] to make a comma separated list, then use FIND_IN_SET(vendor,?) in the WHERE clause to match all the rows in one query, then just use the result set from that single query. if you use PDO fetchAll() with the PDO::FETCH_COLUMN fetch mode, you will get a one-dimensional array of just the email addresses, that you can directly implode() to produce the comma separated list of email addresses.

Edited by mac_gyver
  • Like 1

Thank you @mac_gyver

I tried like this and it worked as i wanted.    

$ven = implode(',', $_POST['vendor']);
   
     $vw = $con->prepare("SELECT email FROM ven_contacts WHERE vendor IN ($ven)");
    $vw->execute();
   
   while ($email = $vw->fetchAll(PDO::FETCH_COLUMN)) {
  $output1 =  implode(',', $email);
  
  $to =  '"'.$output1.'"';  
  
        $subject = "Email to multiple vendors";  

         $message = "<p>Hello All</p>";
         $message .= "<p>Welcome to our Portal</p>";
         $message .= "<p>Thank you</p>";
         
        $headers = "";
         $header = "From:info@mydomain.com \r\n";
        $headers .= "From: Info <info@mydomain.com> \r\n";
         $headers .= "MIME-Version: 1.0\r\n";
         $headers .= "Content-type: text/html\r\n";
         
         $retval = mail ($to,$subject,$message,$headers);

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.