Skip to content

rolandolopez36/contact_form

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Contact Form with PHPMailer

This project is a basic contact form built with PHP, utilizing PHPMailer to send emails. The project is configured to use Mailtrap as a testing environment to capture and analyze sent emails without affecting real users.

Features

  • Contact form with fields for Name, Email, Subject, and Message.
  • Server-side validation of form inputs.
  • Email sending using PHPMailer.
  • Safe email testing with Mailtrap.

Requirements

  • PHP 7.x or higher
  • Composer (for dependency management)
  • A local server like XAMPP, WAMP, or similar
  • An account with Mailtrap

Installation

1. Clone the repository

git clone https://github.com/rolandolopez36/contact_form.git
cd contact_form

2. Install dependencies

This project uses Composer to manage dependencies. Make sure you have Composer installed and then run:

composer install

3. Configure PHPMailer with Mailtrap

  1. Create an account on Mailtrap and set up an inbox.
  2. Obtain the SMTP credentials from Mailtrap (username, password, host, port).
  3. Configure the send_email.php file to use these credentials:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    // Capture form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    // Configure SMTP server for Mailtrap
    $mail->isSMTP();
    $mail->Host       = 'sandbox.smtp.mailtrap.io';
    $mail->SMTPAuth   = true;
    $mail->Port       = 2525;
    $mail->Username   = 'your_username';
    $mail->Password   = 'your_password';

    // Set character encoding
    $mail->CharSet    = 'UTF-8';

    // Set sender and recipient
    $mail->setFrom('[email protected]', 'Your Application');
    $mail->addAddress('[email protected]', 'Test User');

    // Build email content
    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body    = "<p><strong>Name:</strong> $name</p>
                      <p><strong>Email:</strong> $email</p>
                      <p><strong>Message:</strong> $message</p>";
    $mail->AltBody = "Name: $name\nEmail: $email\nMessage: $message";

    // Send email
    $mail->send();
    echo 'The message has been sent successfully';
} catch (Exception $e) {
    echo "The message could not be sent. PHPMailer Error: {$mail->ErrorInfo}";
}

4. Set up the development environment

  1. Ensure your local server (e.g., XAMPP, WAMP) is configured and running.
  2. Place the project in the htdocs directory (or the root directory of your local server).
  3. Access the project in your browser at http://localhost/contact_form/.

Usage

  1. Open the contact form in your browser at http://localhost/contact_form/.
  2. Fill in the Name, Email, Subject, and Message fields.
  3. Submit the form.
  4. Check your Mailtrap inbox to see the captured email.

Testing

  • Test by sending different messages through the form and verifying that all emails are captured correctly in Mailtrap.
  • Ensure that special characters are handled correctly and that emails are sent in the desired format.

Contributing

If you would like to contribute to this project:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/new-feature).
  3. Make your changes and commit them (git commit -am 'Add new feature').
  4. Push to the branch (git push origin feature/new-feature).
  5. Open a Pull Request.

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published