mythri Posted February 8, 2023 Share Posted February 8, 2023 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 Quote Link to comment https://forums.phpfreaks.com/topic/315894-sending-emails-to-multiple-email-ids-with-nested-loop/ Share on other sites More sharing options...
Solution mac_gyver Posted February 8, 2023 Solution Share Posted February 8, 2023 (edited) 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 February 8, 2023 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/315894-sending-emails-to-multiple-email-ids-with-nested-loop/#findComment-1605494 Share on other sites More sharing options...
mythri Posted February 8, 2023 Author Share Posted February 8, 2023 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); Quote Link to comment https://forums.phpfreaks.com/topic/315894-sending-emails-to-multiple-email-ids-with-nested-loop/#findComment-1605498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.