How to send auto response email in PHP


In this example we using gmail SMTP for send auto resoponse email to the user who register a form.

To send mail first create a table in the database.

SQL query for create a table is :

CREATE TABLE `users` (
 
`userid` int(8) NOT NULL,
  
`first_name` varchar(55) NOT NULL,
  
`last_name` varchar(55) NOT NULL,
  
`city_name` varchar(55) NOT NULL,
  
`email` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

To send a response mail to the user, you must have email field in your form.

Here we using 3 file for send auto response mail in PHP.

index.php:To recieve user data.

database.php:For connect with database.

mail-process.php:For process the user data and send auto response mail to PHP.

notification.php:For process the user data and send auto response mail to both user and admin.

index.php

<!DOCTYPE html>
<html>
<body>
<form method="post" action="mail-process.php">
First name:<br>
<input type="text" name="first_name">
<br>
Last name:<br>
<input type="text" name="last_name">
<br>
City name:<br>
<input type="text" name="city_name">
<br>
Email Id:<br>
<input type="email" name="email">
<br><br>
<input type="submit" name="save" value="submit">
</form>
</body>
</html>

database.php

<?php
$url='127.0.0.1:3306';
$username='root';
$password='';
$conn=mysqli_connect($url,$username,$password,"crud");
if(!$conn){
 die('Could not Connect My Sql:' .mysql_error());
}
?>

mail-process.php

<?php
include_once 'database.php';
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$city_name = $_POST['city_name'];
$email = $_POST['email'];

/* sql query for inserting data into database */

mysqli_query($conn,"insert into employee (first_name,last_name,city_name,email) values ('$first_name','$last_name','$city_name','$email')") or die(mysqli_error());
require_once 'mailer/class.phpmailer.php';
/* creates object */
$mail = new PHPMailer(true);
$mailid = $email;
$subject = "Thank u";
$text_message = "Hello";
$message = "Thank You for Contact with us.";

try
{
$mail->IsSMTP();
$mail->isHTML(true);
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = '465';
$mail->AddAddress($mailid);
$mail->Username ="divyasundarsahu@gmail.com";
$mail->Password ="password@123";
$mail->SetFrom('divyasundarsahu@gmail.com','Divyasundar Sahu');
$mail->AddReplyTo("divyasundarsahu@gmail.com","Divyasundar Sahu");
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if($mail->Send())
{
echo "Thank you for register u got a notification through the mail you provide";
}
}
catch(phpmailerException $ex)
{
$msg = "
".$ex->errorMessage()."
"; } ?>

If you want to send an alert mail to admin that some one register with us then use this process file instead of mail-process.php file.

notification.php

<?php
include_once 'database.php';
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$city_name = $_POST['city_name'];
$email = $_POST['email'];

/* sql query for inserting data into database */

mysqli_query($conn,"insert into employee (first_name,last_name,city_name,email) values ('$first_name','$last_name','$city_name','$email')") or die(mysqli_error());
require_once 'mailer/class.phpmailer.php';
/* creates object */
$mail = new PHPMailer(true);
$mailid = $email;
$subject = "Thank u";
$text_message = "Hello";
$message = "Thank You for Contact with us.";

try
{
$mail->IsSMTP();
$mail->isHTML(true);
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "smtp.gmail.com";
$mail->Port = '465';
$mail->AddAddress($mailid);
$mail->Username ="divyasundarsahu@gmail.com";
$mail->Password ="password@123";
$mail->SetFrom('divyasundarsahu@gmail.com','Divyasundar Sahu');
$mail->AddReplyTo("divyasundarsahu@gmail.com","Divyasundar Sahu");
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AltBody = $message;
if($mail->Send())
{
echo "Thank you for register u got a notification through the mail you provide";
}
}
catch(phpmailerException $ex)
{
$msg = "
".$ex->errorMessage()."
"; } require_once 'mailer/class.phpmailer.php'; /* creates object */ $mail = new PHPMailer(true); $mailid = "divyasundarsahu@gmail.com"; $subject = "Thank u"; $text_message = "Hello"; $message = "New User Registered. Mail id :".$mailid.""; try { $mail->IsSMTP(); $mail->isHTML(true); $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $mail->SMTPSecure = "ssl"; $mail->Host = "smtp.gmail.com"; $mail->Port = '465'; $mail->AddAddress($mailid); $mail->Username ="divyasundarsahu@gmail.com"; $mail->Password ="password@123"; $mail->SetFrom('divyasundarsahu@gmail.com','Divyasundar Sahu'); $mail->AddReplyTo("divyasundarsahu@gmail.com","Divyasundar Sahu"); $mail->Subject = $subject; $mail->Body = $message; $mail->AltBody = $message; if($mail->Send()) { echo "You have a new user"; } } catch(phpmailerException $ex) { $msg = "
".$ex->errorMessage()."
"; } ?>