Last active
May 8, 2022 08:37
-
-
Save tutweb/4282ec1c16354b8cb8712eaf6877a07a to your computer and use it in GitHub Desktop.
Contoh form kontak dengan PHP yang terkirim ke email
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 | |
$statusMsg = ''; | |
$msgClass = ''; | |
if(isset($_POST['submit'])){ | |
// Get the submitted form data | |
$email = $_POST['email']; | |
$name = $_POST['name']; | |
$subject = $_POST['subject']; | |
$message = $_POST['message']; | |
// Cek apakah ada data yang belum diisi | |
if(!empty($email) && !empty($name) && !empty($subject) && !empty($message)){ | |
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){ | |
$statusMsg = 'Please enter your valid email.'; | |
$msgClassk = 'errordiv'; | |
}else{ | |
// Pengaturan penerima email dan subjek email | |
$toEmail = 'email@namadomain.com'; // Ganti dengan alamat email yang Anda inginkan | |
$emailSubject = 'Pesan website dari '.$name; | |
$htmlContent = '<h2> via Form Kontak Website</h2> | |
<h4>Name</h4><p>'.$name.'</p> | |
<h4>Email</h4><p>'.$email.'</p> | |
<h4>Subject</h4><p>'.$subject.'</p> | |
<h4>Message</h4><p>'.$message.'</p>'; | |
// Mengatur Content-Type header untuk mengirim email dalam bentuk HTML | |
$headers = "MIME-Version: 1.0" . "\r\n"; | |
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; | |
// Header tambahan | |
$headers .= 'From: '.$name.'<'.$email.'>'. "\r\n"; | |
// Send email | |
if(mail($toEmail,$emailSubject,$htmlContent,$headers)){ | |
$statusMsg = 'Pesan Anda sudah terkirim dengan sukses !'; | |
$msgClass = 'succdiv'; | |
}else{ | |
$statusMsg = 'Maaf pesan Anda gagal terkirim, silahkan ulangi lagi.'; | |
$msgClass = 'errordiv'; | |
} | |
} | |
}else{ | |
$statusMsg = 'Harap mengisi semua field data'; | |
$msgClass = 'errordiv'; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Kontak Form dengna PHP | Jurnalweb.com</title> | |
<style> | |
.mainContent { | |
width: 35%; | |
margin: 2.5em auto; | |
background: #fff; | |
padding: 2.5em; | |
} | |
.contactFrm h4 { | |
font-size: 1em; | |
color: #252525; | |
margin-bottom: 0.5em; | |
font-weight: 300; | |
letter-spacing: 5px; | |
} | |
.contactFrm input[type="text"], .contactFrm input[type="email"] { | |
width: 70%; | |
outline: none; | |
font-size: 0.9em; | |
padding: .7em 1em; | |
border: 1px solid #000; | |
-webkit-appearance: none; | |
display: block; | |
margin-bottom: 1.2em; | |
} | |
.contactFrm textarea { | |
resize: none; | |
width: 93.5%; | |
font-size: 0.9em; | |
outline: none; | |
padding: .6em 1em; | |
border: 1px solid #000; | |
min-height: 10em; | |
-webkit-appearance: none; | |
} | |
.contactFrm input[type="submit"] { | |
outline: none; | |
color: #FFFFFF; | |
padding: 0.5em 0; | |
font-size: 1em; | |
margin: 1em 0 0 0; | |
-webkit-appearance: none; | |
background: #006faa; | |
transition: 0.5s all; | |
border: 2px solid #006faa; | |
-webkit-transition: 0.5s all; | |
transition: 0.5s all; | |
-moz-transition: 0.5s all; | |
width: 47%; | |
cursor: pointer; | |
} | |
.contactFrm input[type="submit"]:hover { | |
background: none; | |
color: #006faa; | |
} | |
p.statusMsg{font-size:18px;} | |
p.succdiv{color: #008000;} | |
p.errordiv{color:#E80000;} | |
</style> | |
</head> | |
<body> | |
<div class="mainContent"> | |
<h2>Kontak Form PHP / <small><a href="http://www.jurnalweb.com">Jurnalweb.com</a></small></h2> | |
<div class="contactFrm"> | |
<?php if(!empty($statusMsg)){ ?> | |
<p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p> | |
<?php } ?> | |
<form action="" method="post"> | |
<h4>Name</h4> | |
<input type="text" name="name" placeholder="Nama Anda" required=""> | |
<h4>Email </h4> | |
<input type="email" name="email" placeholder="email@example.com" required=""> | |
<h4>Subject</h4> | |
<input type="text" name="subject" placeholder="Tulis subject" required=""> | |
<h4>Message</h4> | |
<textarea name="message" placeholder="Tulis pesan Anda disini" required=""> </textarea> | |
<input type="submit" name="submit" value="Submit"> | |
<div class="clear"> </div> | |
</form> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
untuk settingan pada website hosting ?