Form Contact Send Mail using PHP and Design CSS Bootstrap

Form Contact Send Mail using PHP and Design CSS Bootstrap

1. PHP Mailer Contact Form (PHPMailer)

PHP Mailer Script Explanation

<?php $result = ""; $error = ""; if (isset($_POST['submit'])) { require 'phpmailer/PHPMailerAutoload.php'; $mail = new PHPMailer; // SMTP settings $mail->isSMTP(); // Send as HTML $mail->Host = "smtp.gmail.com"; // SMTP server $mail->SMTPAuth = true; // Turn on SMTP authentication $mail->Username = "your email"; // Your email address $mail->Password = 'your password'; // Your email password $mail->Port = 587; // SMTP port $mail->SMTPSecure = 'tls'; $mail->setFrom($_POST['email'], $_POST['name']); $mail->addAddress('your email'); // Recipient email address $mail->addReplyTo($_POST['email'], $_POST['name']); // Reply-to address $mail->isHTML(true); $mail->Subject = 'Form Submission: ' . $_POST['subject']; $mail->Body = '<h3>Name: ' . $_POST['name'] . '<br> Email: ' . $_POST['email'] . '<br> Message: ' . $_POST['message'] . '</h3>'; if (!$mail->send()) { $error = "Something went wrong. Please try again."; } else { $result = "Thanks, " . $_POST['name'] . " for contacting us."; } } ?>
  • PHPMailer Setup: You're using PHPMailer to send an email via SMTP (Gmail in this case).

  • Input Processing: The form values are collected  $_POST and passed to PHPMailer for sending.

  • Success/Error Handling: The email sending result is displayed via the $result or $error message.

HTML Contact Form

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Contact Form</title> <link href="https://fonts.googleapis.com/css?family=Roboto|Courgette|Pacifico:400,700" rel="stylesheet"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2 m-auto"> <div class="contact-form"> <h1>Contact Form SendMail</h1> <h2 class="text-center text-white"><?= $result; ?></h2> <h2 class="text-center text-danger"><?= $error; ?></h2> <form action="" method="post"> <div class="row"> <div class="col-sm-6"> <div class="form-group"> <label for="inputName">Name</label> <input type="text" class="form-control" id="Name" name="name" placeholder="Enter Full" required> </div> </div> <div class="col-sm-6"> <div class="form-group"> <label for="inputEmail">Email</label> <input type="email" class="form-control" id="Email" name="email" placeholder="Enter Email" required> </div> </div> </div> <div class="form-group"> <label for="inputSubject">Subject</label> <input type="text" class="form-control" id="Subject" name="subject" placeholder="Enter Subject" required> </div> <div class="form-group"> <label for="inputMessage">Message</label> <textarea class="form-control" id="Message" name="message" rows="5" placeholder="Enter Message..." required></textarea> </div> <div class="text-center"> <button type="submit" name="submit" class="btn btn-primary"><i class="fa fa-paper-plane"></i> Send</button> </div> </form> </div> </div> </div> </div> </body> </html>
  • Form Fields: Users enter their name, email, subject, and message.

  • Result/Error Message: The result of the form submission (success or error) is displayed at the top of the form.

2. Using PHP mail() Function (Alternative to PHPMailer)

The mail() function in PHP is another way to send emails. Below is a simple example of how you can send an email using mail().

Basic mail() Syntax:

<?php $to_email = 'recipient@example.com'; // Recipient's email address $subject = 'Test Email from PHP'; $message = 'This is a test email sent using PHP mail function.'; $headers = 'From: sender@example.com'; // Sender's email address mail($to_email, $subject, $message, $headers); ?>

This basic script will send a plain text email. You can also add more headers, like CC, BCC, and MIME types for HTML emails.

Example with CC, BCC:

<?php $to_email = 'recipient@example.com'; $subject = 'Test Email from PHP'; $message = 'This is a test email sent using PHP mail function.'; $headers = 'From: sender@example.com' . "\r\n" . 'CC: cc@example.com' . "\r\n" . 'BCC: bcc@example.com'; mail($to_email, $subject, $message, $headers); ?>

3. Sanitizing User Inputs

It's essential to sanitize user inputs to prevent email injection attacks. Here’s an example of sanitizing and validating email input:

<?php function sanitize_email($email) { $email = filter_var($email, FILTER_SANITIZE_EMAIL); return filter_var($email, FILTER_VALIDATE_EMAIL) ? $email : false; } $email = $_POST['email']; if ($email = sanitize_email($email)) { // Proceed to send email mail($to_email, $subject, $message, $headers); } else { echo "Invalid email address!"; } ?>

4. Using Secure Mail with PHPMailer

For better security, especially for sending sensitive information, PHPMailer can be used with encryption (SSL/TLS) to securely send emails. This approach prevents the email contents from being intercepted during transmission.

Conclusion

  • PHP mail() Function: It is Simple to use for sending emails but lacks advanced features like HTML support, SMTP, and error handling.

  • PHPMailer: A more robust solution for sending emails, especially with features like SMTP, HTML emails, and error handling.

  • Sanitizing Inputs: Always sanitize user inputs (especially email) to prevent security issues.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close