Berikut adalah cara membuat contact form dengan PHP, SMTP, dan CSS untuk tampilan yang elegan dan user-friendly.
1. File HTML + CSS
Buat file bernama contact-form.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: #ffffff;
padding: 20px 30px;
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 400px;
}
.container h2 {
text-align: center;
color: #333333;
margin-bottom: 20px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
color: #555555;
}
.form-group input,
.form-group textarea {
width: 100%;
padding: 10px;
border: 1px solid #cccccc;
border-radius: 4px;
font-size: 14px;
}
.form-group textarea {
resize: none;
height: 100px;
}
.btn {
display: block;
width: 100%;
background: #007bff;
color: #ffffff;
padding: 10px;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
text-align: center;
}
.btn:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>Contact Us</h2>
<form action="send-email.php" method="POST">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>
<div class="form-group">
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
</div>
<button type="submit" class="btn">Send Message</button>
</form>
</div>
</body>
</html>
PHP2. File PHP untuk SMTP
Buat file bernama send-email.php
:
<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Ambil data dari formulir
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Konfigurasi PHPMailer
$mail = new PHPMailer(true);
try {
// Konfigurasi server SMTP
$mail->isSMTP();
$mail->Host = 'smtp.example.com'; // Ganti dengan host SMTP Anda
$mail->SMTPAuth = true;
$mail->Username = 'your_email@example.com'; // Email Anda
$mail->Password = 'your_email_password'; // Password email Anda
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Enkripsi
$mail->Port = 587; // Port SMTP
// Pengaturan email
$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name'); // Email penerima
$mail->isHTML(true);
$mail->Subject = 'Contact Form Submission';
$mail->Body = "
<h3>New message from $name</h3>
<p>Email: $email</p>
<p>Message:</p>
<p>$message</p>
";
// Kirim email
$mail->send();
echo "<script>
alert('Message has been sent successfully!');
window.location.href = 'contact-form.html';
</script>";
} catch (Exception $e) {
echo "<script>
alert('Message could not be sent. Mailer Error: {$mail->ErrorInfo}');
window.history.back();
</script>";
}
?>
PHP3. Konfigurasi SMTP
- Ganti nilai
smtp.example.com
,your_email@example.com
, danyour_email_password
dengan detail SMTP Anda. - Jika menggunakan Gmail:
- Aktifkan akses aplikasi yang kurang aman di Google Account Settings.
- Atau, gunakan password khusus aplikasi jika fitur tersebut diaktifkan.
4. Hasil Akhir
See the Pen Contact Form PHP Smtp by WebbizID (@De-Orchids) on CodePen.
Formulir akan memiliki tampilan modern dan responsif. Saat diisi dan dikirimkan, email akan dikirim melalui SMTP. Anda dapat menyesuaikan warna dan gaya CSS sesuai kebutuhan.
Jika ada kendala, silakan tanyakan! 😊
Leave a Reply