I have a PHP-script that import data from a MySQL database and send send an email to said persons with the help of a while loop. Everything works fine except that I can't use variable values in the email body (instead the name of the variable is printed out). Example, if I write ".$first_name." I want the body of the email to say "Johan" if that's the name of the entry in the database.
if (isset($_POST['submit'])){
// About database
$user = "user";
$password = "password";
$host = "host";
$dbase = "dbase";
$table = "table";
// Connection to database
$dbc= mysqli_connect($host,$user,$password, $dbase)
or die("Unable to select database");
// Retrieve emails from database
$country = $_POST['country'];
$query= "SELECT email, firstname FROM $table WHERE country = '$country'";
$result= mysqli_query ($dbc, $query)
or die ('Error querying database.');
// Send the email
while ($row = mysqli_fetch_array($result)) {
// Write $first_name for recipients name
$first_name = $row['firstname'];
$last_name = $row['lastname'];
$email = $row['email'];
// About the email
$from = 'info@email.com';
$subject = $_POST['heading'];
$body = $_POST['message'];
$speed = $_POST['speed'];
mail($email, $subject, $body, 'From:' . $from);
echo 'Email sent to: ' . $email. '<br>';
// Delay for speed regulation
sleep(3600/$speed);
}
}
mysqli_close($dbc);
I guess the problem must be either here:
$query= "SELECT email, firstname FROM $table WHERE country = '$country'";
$result= mysqli_query ($dbc, $query)
or die ('Error querying database.');
Or here:
$first_name = $row['firstname'];
$last_name = $row['lastname'];
$email = $row['email'];