0

I am trying to make a php page where once an item in the database is updated it sends an email to any user linked to that item. The code I have retrieves the email addresses and places them in array. The issue is that the emails are not being sent, where am I going wrong?

<?php
require_once 'config/init.php';
$id = $_GET['id'];

$mysqli_conn = new mysqli($db['hostname'],$db['username'],$db['password'], $db['database']);
        if ($mysqli_conn -> connect_errno) {//check the connection
            print "Failed to connect to MySQL: (" . $mysqli_conn -> connect_errno . ") " . $mysqli_conn -> connect_error;
        }

$result = $mysqli_conn->query("SELECT * From Logins")

$headers.= "\r\n";
$headers.= "Content-type: text/html\r\n";
$to = '';

$headers.="Bcc: ";
while ($row = $result->fetch_array()) {
    $headers.=$row['Email'].", ";
    //$to.=$row['Email'].", ";
}
$subject = "Subject";
$headers.="\r\n";
$mailbody = "Body of email";

$mailResult = @mail($to, $subject, $mailbody, $headers);


print $to;
print $subject;
print $mailbody;
print $headers;


?>
TheKaiser4
  • 149
  • 1
  • 1
  • 11
  • 1
    You need to look in your mail server log, and don't suppress errors (with `@`) when you're debugging! – Synchro Apr 16 '16 at 10:39

1 Answers1

1

I would hazard a guess that it's because your to field is blank, and although the BCC headers are being populated. (hopefully - presumably you've double checked that with the print statements?) the email can't be sent as it has no one to send it to.

Try to put in a dummy email for the to field no-reply@your-domain.com for example, that may fix it.

Ali
  • 1,408
  • 10
  • 17
Fraser G
  • 113
  • 1
  • 6