-2
<html>
<body>
<?php

include 'index.php';
include '../db.php';
$query_email = "SELECT email FROM email_addresses"; 
$result_query_email = mysqli_query($query_email)
or die(mysqli_error());

while ($row = mysqli_fetch_array()($result_query_email)) {

//$email_user = $row[10];
$email_user = $row['email'];

$data = date("Y.m.d");
$subject = "Good news";
$message = ("Hello!");

$sentemail = mail($email_user, $subject, $message, 
"From: abc <xyz.com>\nX-Mailer: PHP/" . phpversion());
}
if($sentemail) 
{

    print("Email has been sent successfully.<br>");
}
else
{
  print("There was a problem while sending email.<br>");
}

?>

</body>
</html>

This code is to select email addresses from the database and to send them an email. what is wrong with my code, i can't get any email, Help will be appreciated. Thanks!

  • are you running this script in your local server? – Muthu Kumaran Nov 27 '12 at 16:28
  • 2
    Is `$sentemail` true or false? – ServerBloke Nov 27 '12 at 16:28
  • 1
    are you running it on localhost? if thats the case you need th setup and SMTP –  Nov 27 '12 at 16:29
  • 1
    Have you tried debugging it yourself? Do either of the `print`s work? Don't just post code and say "it's broken, fix it". Try to fix it yourself, tell us what you tried, and we'll help. – gen_Eric Nov 27 '12 at 16:32
  • Not directly related, though quite important: PHP's `mysql_*` extension is being deprecated. [Note the red warning box](http://www.php.net/mysql_connect). Switch to `PDO` (or `mysqli_*`) ASAP, and learn about [the advantages of prepared statements](http://bobby-tables.com/) – Elias Van Ootegem Nov 27 '12 at 16:37

3 Answers3

3

Set up error_reporting and display_errors to find out that there's a missing ) in

while (mysql_fetch_array($result_query_email) {

steffen
  • 16,138
  • 4
  • 42
  • 81
  • I have my own server, and i have tried with the same code without querying and i am getting the emails. My doubt is on $email_user = $result_query_email['email']; – user142397 Nov 27 '12 at 16:33
  • Check all the variables with `var_dump()` (this helps you finding logical errors such as wrong values in database) and activate `error_reporting` (this will help you fix your local setup and syntax errors). – steffen Nov 27 '12 at 17:19
3

This line here

while (mysql_fetch_array($result_query_email)

you need to assign the return value of mysql_fetch_array() to a variable to be able to use it in the while() loop as well as the missing closing )

for instance

while ($emaildata = mysql_fetch_array($result_query_email)) {
{
    $email_user = $emaildata['email'];
 //.....rest of code

also

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun, see the red box. Learn about prepared statements instead, and use PDO or MySQLi; this article will help you decide which. If you choose PDO, here is a good tutorial.

EDIT

Also it seems like you are trying to use the variable as a associative array you need to use mysql_fetch_assoc() instead of mysql_fetch_array() to do that

Community
  • 1
  • 1
Geek Num 88
  • 5,264
  • 2
  • 22
  • 35
0

Please set correct SMTP server address in your server PHP.ini if you do not provide in sources code.