Jump to content
I-AM-OBODO

Sending mail from multiple table

Recommended Posts

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);
	}
	

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
foreach (email type) {
    create message etc
    get recipients
    foreach (recipient) {
        add to BCC list
    }
    send email
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.