Cara mengirim email respons otomatis dalam PHP

Cara mengirim email respons otomatis dalam PHP

Cara mengirim email respons otomatis dalam PHP

Cara mengirim email respons otomatis dalam PHP, Dalam contoh ini kami menggunakan SMTP Gmail untuk mengirim email auto resoponse ke pengguna yang mendaftarkan formulir. Jangan lupa untuk mengunjungi artikel tentang cara setting kirim email dengan php dulu ya jika sudah paham baru lanjoot

Untuk mengirim email terlebih dahulu buat tabel di database.

Buat Tabel Users

Kueri SQL untuk membuat tabel adalah:

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;

 

Untuk mengirim email tanggapan ke pengguna, Anda harus memiliki bidang email di formulir Anda.

Di sini kami menggunakan 3 file untuk mengirim email tanggapan otomatis dalam PHP.

index.php : Untuk menerima data pengguna.

database.php : Untuk terhubung dengan database.

mail-process.php : Untuk memproses data pengguna dan mengirim email tanggapan otomatis ke PHP.

notification.php : Untuk memproses data pengguna dan mengirim email tanggapan otomatis ke pengguna dan admin.

Form Kirim Email

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>

Koneksi Database

database.php

<?php

$url='localhost;

$username='root';

$password='';

$conn=mysqli_connect($url,$username,$password,"crud");

if(!$conn){

die('Could not Connect My Sql:' .mysql_error());

}

?>

Source Kirim Email

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 ="nauralpk@gmail.com";

$mail->Password ="passwordnya";

$mail->SetFrom(' nauralpk@gmail.com','LKP Naura);

$mail->AddReplyTo("nauralpk@gmail.com","LKP Naura");

$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()."

";

}

?>

 

Jika Anda ingin mengirim email peringatan ke admin bahwa ada orang yang mendaftar dengan kami maka gunakan file proses ini daripada file mail-process.php.

Source Balasan Email

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 ="nauralpk@gmail.com";

$mail->Password ="passwordnya";

$mail->SetFrom(' nauralpk@gmail.com','LKP Naura);

$mail->AddReplyTo("nauralpk@gmail.com","LKP Naura");

$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 = "nauralpk@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 ="nauralpk@gmail.com";

$mail->Password ="passwordnya";

$mail->SetFrom(' nauralpk@gmail.com','LKP Naura);

$mail->AddReplyTo("nauralpk@gmail.com","LKP Naura");

$mail->Subject = $subject;

$mail->Body = $message;

$mail->AltBody = $message;

if($mail->Send())

{

echo "You have a new user";

}

}

catch(phpmailerException $ex)

{

$msg = "

".$ex->errorMessage()."

";

}

?>

Jika masih ada yang kurang jelas silahkan cantumkan pertanyaan kamu di kolom komentar ya..

 

Tinggalkan Komentar

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Need Help? Chat with us