Send Email Notification with PHP

infoslearning

In this tutorial I will show you on how create contact us form with Email using PHP, generate and send Email to appropriate Email address.

To start, first you need to create PHP file where you can write a code.  Create file called “contact.php” as I use in this lesson.

Then, create contact table inside a database with the following attributes;

--
-- Table structure for table `contact_tbl`
--

CREATE TABLE `contact_tbl` (
  `id` int(11) NOT NULL,
  `phone` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `subject` text NOT NULL,
  `message` text NOT NULL
)

Inside PHP file write the code below.

<?php
include ('includes/connect.php');//Database Connection
if(isset($_POST['contact']))//Submit button
{

$to = "informstech1@gmail.com";// Enter Receiver Email
$from = $_POST['email'];
$name = $_POST['name'];
$subject = $_POST['subject'];
$number = $_POST['number'];
$cmessage = $_POST['message'];


						
$query="INSERT INTO contact_tbl (phone,email,name,subject,message) VALUES 
						('$number',$email','$name','$subject','$message')";
						$result=mysqli_query($conn,$query);

	if($result){
  

    $headers = "From: $from";
	$headers = "From: " . $from . "\r\n";
	$headers .= "Reply-To: ". $from . "\r\n";
	$headers .= "MIME-Version: 1.0\r\n";
	$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

    $subject = "You have a message .";

    $logo = 'img/logo.png'; // Your logo here
    $link = '#';

	$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title> Mail</title> 
     </head><body>";
	$body .= "<table style='width: 100%;'>";
	$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
	$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
	$body .= "</td></tr></thead><tbody><tr>";
	$body .= "<td style='border:none;'><strong>Name:</strong> {$name}</td>";
	$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
	$body .= "</tr>";
	$body .= "<tr><td style='border:none;'><strong>Subject:</strong> {$csubject}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td colspan='2' style='border:none;'>{$cmessage}</td></tr>";
	$body .= "</tbody></table>";
	$body .= "</body></html>";

    $send = mail($to, $subject, $body, $headers);


		}
?>

Finally, run a file to see.


Other Recommended for you

  • .What is Cloud Firestore? Android Studio
  • .Firebase Realtime Database vs Firestore.
  • .What is an Intent in Android? Types of Intent
  • .What is Firebase?