Send Email after mysql insert

Discuss coding issues, and scripts related to PHP and MySQL.
mluci12
Posts: 39

Send Email after mysql insert

Hello!How can i sent am email after mysql insert?

Code: Select all

<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');

// check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
}

// sql query for INSERT INTO users
$sql = "INSERT INTO `users` (`name`, `pass`, `email`)
VALUES ('Marius', 'faith', 'name@domain.net')"; 

// Performs the $sql query on the server to insert the values
if ($conn->query($sql) === TRUE) {
  echo 'users entry saved successfully';
}
else {
 echo 'Error: '. $conn->error;
}

$conn->close();
?>

Admin Posts: 805
Hello
Just add the code with mail() function in the if() that performs and checks the query.

Code: Select all

// Performs the $sql query on the server to insert the values
if ($conn->query($sql) === TRUE) {
  echo 'users entry saved successfully<br>';

  $from = 'From: sender@domain.ext';
  $to = 'receiver@domain.net';
  $subject = 'Subject ...';
  $msg = 'Message ...';

  // send the email
  if (mail($to, $subject, $msg, $from)) echo 'email sent';
  else echo 'Error, the email can not be sent';
}