Jump to content

Sending mail from multiple table


I-AM-OBODO

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

Link to comment
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.

Link to comment
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

Link to comment
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).

Link to comment
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?

Link to comment
Share on other sites

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.