Created
August 7, 2020 07:59
-
-
Save tfkproject/e5df23f30e742c7e88ce25751ba220eb to your computer and use it in GitHub Desktop.
Aplikasi mengirim email, salah satunya bisa dimanfaatkan untuk metode reset password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'PHPMailer/PHPMailerAutoload.php'; // ini lib yg digunakan | |
$mail = new PHPMailer; | |
// array for JSON response | |
$response = array(); | |
$email = $_POST['email']; // parameter cuma email saja | |
// include db connect class | |
require 'connect.php'; | |
$sql = "select * from `user` WHERE `email` = '$email' "; | |
if($result = $db->query($sql)){ | |
if($count = $result->num_rows){ | |
$response["field"] = array(); | |
while($row = $result->fetch_object()){ | |
$data = array(); | |
$data["id_user"] = $row->id_user; | |
// Konfigurasi SMTP | |
$mail->isSMTP(); | |
$mail->Host = 'ssl://smtp.gmail.com'; | |
$mail->SMTPAuth = true; | |
$mail->Username = '[email protected]'; | |
$mail->Password = 'passwordnyo'; | |
$mail->Port = 465; | |
$mail->setFrom('[email protected]', 'Layanan Aplikasi Blablabla'); | |
// Menambahkan penerima | |
$mail->addAddress($email); | |
// Menambahkan beberapa penerima | |
//$mail->addAddress('[email protected]'); | |
//$mail->addAddress('[email protected]'); | |
// Menambahkan cc atau bcc | |
//$mail->addCC('[email protected]'); | |
//$mail->addBCC('[email protected]'); | |
// Subjek email | |
$mail->Subject = 'Lapkar Password Recovery'; | |
// Mengatur format email ke HTML | |
$mail->isHTML(true); | |
// Buat kode recovery | |
$digits = 4; | |
//$rec_code = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT); | |
$rec_code = str_pad(rand(0,9999), $digits, "0", STR_PAD_LEFT); | |
// Masukkan kode ke database | |
$db->query("update `user` set `recovery_code` = '$rec_code' where `id_user` = '$row->id_user'"); | |
// Konten/isi email | |
$mailContent = ' | |
<html> | |
<h4>Selamat datang '.$row->nama_user.', terimakasih telah menggunakan layanan Lapkar</h4> | |
<hr> | |
<h4>KODE RECOVERY ANDA :</h4> | |
<hr> | |
<center><h2>'.$rec_code.'</h2></center> | |
<hr> | |
<h4>Berikut panduan cara recovery password anda</h4> | |
<p> | |
1. Buka link <a href="http://lapkarpontianak.online/lupa-password.php?email='.$email.'" target="_blank">berikut ini</a>.<br> | |
2. Masukkan kode recovery anda. <b>'.$rec_code.'</b> adalah kode recovery anda.*<br> | |
3. Buat password baru anda.<br> | |
Selesai. | |
</p> | |
<small>*Kode recovery bersifat sekali pakai. Setelah kode tersebut digunakan, anda tidak akan dapat menggunakan kode tersebut untuk kedua kalinya. | |
<br>Jika anda lupa password, anda dibutuhkan untuk melakukan request ulang. </small> | |
<hr> | |
<p class="copy_rights">© <script>document.write(new Date().getFullYear());</script> Lapkar Online Pontianak - Aman Raya</p> | |
</html> | |
'; | |
$mail->Body = $mailContent; | |
// Menambahakn lampiran | |
// Kirim email | |
if(!$mail->send()){ | |
$data["message"] = "Maaf, terjadi kesalahan sistem. Request anda tidak dapat kami proses."; | |
}else{ | |
$data["message"] = "Terimakasih, request anda telah terkirim. Periksa email anda dan ikuti petunjuk yang tertera."; | |
} | |
array_push($response["field"], $data); | |
} | |
$response["success"] = 1; | |
// echoing JSON response | |
echo json_encode($response); | |
} | |
else{ | |
// no datas found | |
$response["success"] = 0; | |
$response["message"] = "Maaf, email tidak terdaftar!"; | |
// echo no users JSON | |
echo json_encode($response); | |
} | |
$result->free(); | |
} | |
else { | |
// no datas found | |
$response["success"] = 0; | |
$response["message"] = "Maaf, terdapat error pada database"; | |
// echo no users JSON | |
echo json_encode($response); | |
} | |
// CATATAN PENTING: | |
// 1. Buat email dg format sbb: | |
// Jenis: For manage business // bisa juga just my self, tapi sangat disarankan yg for manage business | |
// Nama: Aplikasi blablabla | |
// Email: [email protected] | |
// Dibuat: 12 Oktober 2019 | |
// TTL: 7 Januari 1990 | |
// Jenis Kelamin: Rather not say | |
// Pass google: isipasswrdnyaapa | |
// Phone: - | |
// Email Recovery: - | |
// 2. Masuk ke https://myaccount.google.com/security cari "Akses ke aplikasi yg kurang aman > Aktifkan" | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment