07 Enabling the Contact Form
id: contact
Section number: 07
This project uses Composer for dependency management and PHPMailer for handling email sending in the contact form. Ensure the
vendor/
directory contains the PHPMailer library.Store sensitive data (e.g., email credentials) in the
.env
file located in theenv/
folder. Use the following format:
Note: The .env
file should never be placed inside the public_html
or any publicly accessible directory. Always move it outside of the public directory to ensure security, and update the $dotenv
path in the send_email.php
script accordingly 👇:
Understanding
SMTP_SECURE
The
SMTP_SECURE
variable specifies the encryption protocol for securing the connection to the SMTP server. Common values include:tls
: Recommended for most servers, including Gmail, as it provides strong encryption while using the default SMTP port (587).ssl
: Uses a different port (usually 465) and provides an additional layer of encryption during the connection.
Using the correct encryption type is crucial for establishing a secure connection with the SMTP server. Always confirm the required protocol with your email provider.
Generate SMTP Password: For Gmail users, generate an App Password to use as
SMTP_PASS
instead of your primary email password. For other email providers, follow their guidelines for generating secure credentialsEdit Configurations: Update the send_email.php script to load the .env file and retrieve the credentials securely as well as edit the body of the email ($email_body) if you wish:
Important: Replace your-email-here
in the $mail->addAddress()
function with the email address where you want to receive the messages. This step is critical for ensuring the contact form works as expected.
Test the Form (Running the Project Locally)
If your project is not yet deployed online, you can run it locally using one of the following methods:
Using XAMPP:
Place the project folder inside the
htdocs
directory of XAMPP.Start Apache and MySQL from the XAMPP control panel.
Access your project in the browser using the URL:
http://localhost/<your-project-folder>/public
Using PHP's Built-in Server:
Navigate to the
public
folder of your project in the terminal.Start the server with the command:
Open your browser and navigate to:
http://localhost:8000
Last updated