Reputation: 243
I want to send emails to the appropriate users when a button is clicked. The email addresses are stored in my database where I have set them to a variable, so it can dynamically send to the right users when button is clicked. I have used the following code
$mailquery = "SELECT email FROM tbl_users INNER JOIN tbl_bides ON tbl_users.email = tbl_bides.bidder_id";
while ($mailentry = mysql_fetch_array($mailquery)) {
$mail = $mailentry['email'];
$to = $mail;
$subject = "My subject";
$txt = "Right User!";
$headers = "From: [email protected]" . "\r\n" ."CC:[email protected]";
mail($to, $subject, $txt, $headers);
}
This doesn't work.
Please help!!
Upvotes: 2
Views: 58
Reputation: 74217
Your query isn't being executed.
$mailquery = "SELECT email...
It needs to contain mysql_query()
.
$mailquery = mysql_query("SELECT email FROM tbl_users INNER JOIN tbl_bides ON tbl_users.email = tbl_bides.bidder_id");
Or, if your query requires a connection:
$mailquery = mysql_query("SELECT email FROM tbl_users INNER JOIN tbl_bides ON tbl_users.email = tbl_bides.bidder_id", $connection);
while replacing $connection
with the variable you may be using to connect with, if that is mysql_
to start with and that is unknown.
Make sure you are successfully connected using mysql_
(although you should move to mysqli_
or PDO).
Different MySQL APIs do NOT intermix, so make sure you are using the same one to connect with.
Plus, I've placed a comment under your question about how you are using this "dynamic" method, but failed to respond/updated your question.
You will need to use a WHERE
clause, and using the table/column that you are querying.
I.e.: WHERE table.column = '$email'
Your present code is open to SQL injection. Use mysqli_*
with prepared statements, or PDO with prepared statements.
Then consult:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Also add or die(mysql_error())
to mysql_query()
in case your query may have failed.
If you get a notice about deprecation, then you will need to use either mysqli_
or PDO, which you should be switching to, as those mysql_
functions are deprecated and will be removed in future PHP releases such as PHP 7.0
Remember to use the same MySQL API from connection to query.
Change:
mail($to, $subject, $txt, $headers);
to: (and using a conditional statement)
if(mail($to, $subject, $txt, $headers)){
echo "Mail was sent and has done its job.";
}
else{
echo "Error, check your logs.";
}
If you see Mail was sent and has done its job.
, then mail()
has done just that, its job.
Upvotes: 2