2

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: webmaster@example.com" . "\r\n" ."CC:somebodyelse@example.com";
mail($to, $subject, $txt, $headers);
}

This doesn't work.
Please help!!

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Andrew
  • 223
  • 1
  • 3
  • 12
  • Please update your code. `mysql_*` functions have been deprecated for over 2 years now. They tend to leave you wide open to MySQL injection. It's strongly adviced to use [MySQLi()](http://php.net/manual/en/book.mysqli.php) or [PDO()](http://php.net/manual/en/book.pdo.php) in combination with Prepared Statements. Aside from that: How does it not work? Explain your problem in more detail so we have a general idea about what's going on. – icecub Dec 25 '15 at 04:05
  • *"so it can dynamically send to the right users when button is clicked"* - you're going to need to elaborate on that. I've posted an answer below and outlined what you're not doing. If that doesn't work, then you'll need to show us more code and how exactly that dynamic stuff is happening. You may need to use a `WHERE` clause. – Funk Forty Niner Dec 25 '15 at 04:29

1 Answers1

2

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).

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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • It's Christmas Eve and I have to go now. I'll be back tomorrow to check on what's going on, or not going on. – Funk Forty Niner Dec 25 '15 at 04:36
  • Thank you Very Much... Now its working. I have not entered the right select statement. It has to be $mailquery = mysql_query("SELECT * FROM tbl_users INNER JOIN tbl_bides ON tbl_users.users_id = tbl_bides.bidder_id WHERE bidder_id = $playerid", $con) or die(mysql_error()); Thanks again :) – Andrew Dec 25 '15 at 05:19
  • 1
    @Andrew Thanks and Merry Christmas to you and plenty of Joy and Happiness also, cheers! You're welcome. I'm glad to hear that it was resolved. I had made an edit to the effect of adding a `WHERE` clause and that was the clincher. All the best Andrew! – Funk Forty Niner Dec 25 '15 at 14:08