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.
- 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.
- PHP 7.x or higher
- Composer (for dependency management)
- A local server like XAMPP, WAMP, or similar
- An account with Mailtrap
git clone https://github.com/rolandolopez36/contact_form.git
cd contact_form
This project uses Composer to manage dependencies. Make sure you have Composer installed and then run:
composer install
- Create an account on Mailtrap and set up an inbox.
- Obtain the SMTP credentials from Mailtrap (username, password, host, port).
- 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('no-reply@yourdomain.com', 'Your Application');
$mail->addAddress('test@yourdomain.com', '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}";
}
- Ensure your local server (e.g., XAMPP, WAMP) is configured and running.
- Place the project in the
htdocs
directory (or the root directory of your local server). - Access the project in your browser at
http://localhost/contact_form/
.
- Open the contact form in your browser at
http://localhost/contact_form/
. - Fill in the Name, Email, Subject, and Message fields.
- Submit the form.
- Check your Mailtrap inbox to see the captured email.
- 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.
If you would like to contribute to this project:
- Fork the repository.
- Create a new branch (
git checkout -b feature/new-feature
). - Make your changes and commit them (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature/new-feature
). - Open a Pull Request.
This project is licensed under the MIT License.