I-AM-OBODO Posted November 13, 2018 Share Posted November 13, 2018 Hi guys, Whats the best way of sending mails from multiple table? This is what i did, but i feel there should be a better way of doing it cos sometime it delivers and sometimes it doesn't (don't know why though). Thanks $subj="New Sign up Notification"; $header="MIME-Version: 1.0" . "\r\n"; $header .="Content-type:text/html;charset=UTF-8" . "\r\n"; $header .="From: GOODGUYS<noreply@yoyo.com>"; //TABLE 1 $t1 = "table1"; $stmt = $pdo->query(" SELECT t1_email, t1_name FROM $t1 "); while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){ $t1_name = $rows['t1_name']; $t1_email = $rows['t1_email']; $t1_msg =" <html> <body> <h3>Subject: News</h3> <p> Hi $t1_name, <br> This is a message to you blah blah blah </p> <p> Thank you.<br> </p> </body> </html> "; mail($t1_email,$subj,$t1_msg,$header); } //TABLE 2 $t2 = "table2"; $stmt = $pdo->query(" SELECT t2_email, t2_name FROM $t2 "); while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){ $t2_name = $rows['t2_name']; $t2_email = $rows['t2_email']; $t2_msg =" <html> <body> <h3>Subject: News</h3> <p> Hi $t2_name, <br> This is a message to you blah blah blah </p> <p> Thank you.<br> </p> </body> </html> "; mail($t2_email,$subj,$t2_msg,$header); } //TABLE 3 $t3 = "table3"; $stmt = $pdo->query(" SELECT t3_email, t3_name FROM $t3 "); while($rows = $stmt->fetch(PDO::FETCH_ASSOC)){ $t3_name = $rows['t3_name']; $t3_email = $rows['t3_email']; $t2_msg =" <html> <body> <h3>Subject: News</h3> <p> Hi $t3_name, <br> This is a message to you blah blah blah </p> <p> Thank you.<br> </p> </body> </html> "; mail($t3_email,$subj,$t3_msg,$header); } Quote Link to comment Share on other sites More sharing options...
requinix Posted November 14, 2018 Share Posted November 14, 2018 Mail doesn't care. Get the data you need. If you have to query multiple tables then do that. Maybe I don't understand what your question is. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted November 14, 2018 Author Share Posted November 14, 2018 2 hours ago, requinix said: Mail doesn't care. Get the data you need. If you have to query multiple tables then do that. Maybe I don't understand what your question is. Was thinking there's a way i could do a select and send mails instead multiple sendmail Quote Link to comment Share on other sites More sharing options...
requinix Posted November 14, 2018 Share Posted November 14, 2018 Your emails are different so you can't merge all three queries together. Or rather you can but there's nothing to gain from it. If you mean sending one email listing multiple people, times three for the three emails, then sure: look into BCC. Currently each query gets you multiple recipients and you loop over it to send each email, what you need is that each query gets you multiple recipients and you loop over it to gather them all together into an array (which you'll use for the BCC), and the difference between those isn't complicated. Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted November 15, 2018 Author Share Posted November 15, 2018 21 hours ago, requinix said: Your emails are different so you can't merge all three queries together. Or rather you can but there's nothing to gain from it. If you mean sending one email listing multiple people, times three for the three emails, then sure: look into BCC. Currently each query gets you multiple recipients and you loop over it to send each email, what you need is that each query gets you multiple recipients and you loop over it to gather them all together into an array (which you'll use for the BCC), and the difference between those isn't complicated. Thanks Quote Link to comment Share on other sites More sharing options...
maxxd Posted November 15, 2018 Share Posted November 15, 2018 22 hours ago, requinix said: Your emails are different so you can't merge all three queries together. Or rather you can but there's nothing to gain from it. Yes if you're trying to send three different emails to three different groups then you're pretty much out of luck. Run three queries to get the recipient pool for each email. That having been said, even if that is the goal you can definitely save some resources by using PHPMailer instead of PHP's native mail() function. You can add multiple recipients with the AddAddress() method, so instead of trying to send an email on each iteration of the while() loop, you can instead create a single instance of PHPMailer, call AddAddress on each iteration through the loop of recipients, then send them all in one call. Also, PHPMailer is more reliable and far more robust than PHP's native mail() function (unless there's been some major work to the mail() function while I wasn't watching). Quote Link to comment Share on other sites More sharing options...
I-AM-OBODO Posted November 16, 2018 Author Share Posted November 16, 2018 On 11/15/2018 at 5:44 AM, maxxd said: Yes if you're trying to send three different emails to three different groups then you're pretty much out of luck. Run three queries to get the recipient pool for each email. That having been said, even if that is the goal you can definitely save some resources by using PHPMailer instead of PHP's native mail() function. You can add multiple recipients with the AddAddress() method, so instead of trying to send an email on each iteration of the while() loop, you can instead create a single instance of PHPMailer, call AddAddress on each iteration through the loop of recipients, then send them all in one call. Also, PHPMailer is more reliable and far more robust than PHP's native mail() function (unless there's been some major work to the mail() function while I wasn't watching). Yeah. I know PHPMailer will save resource, that is what i initially planned to use but the loop is where i have problem so i just decided to use the mail() function. How will the loop look like? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 16, 2018 Share Posted November 16, 2018 foreach (email type) { create message etc get recipients foreach (recipient) { add to BCC list } send email } Quote Link to comment Share on other sites More sharing options...
maxxd Posted November 17, 2018 Share Posted November 17, 2018 Pretty much exactly what @Barand said. Quote Link to comment 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.