Rosure
  • 👋Documentation for Rosure Portfolio
  • Getting Started
    • 🎯Features
    • ➡️Sections Overview
  • Setup and Configuration
    • 📍Getting Started
    • 🎨Customization
      • Adding a New Section
      • 03 What I do Section (Adding a New Service)
      • 04 Skills Section (Adding a New Skill)
      • 05 Experience Section (Adding work experience)
      • 06 Projects Section (Adding a New Porject)
      • 07 Enabling the Contact Form
      • Set Default Theme and Configure Toggle Button
    • 🖌️Global Styles
  • Additional
    • 📄Licenses
    • 💌Credits
Powered by GitBook
On this page
  1. Setup and Configuration
  2. Customization

07 Enabling the Contact Form

  • id: contact

  • Section number: 07

  1. 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.

  2. Store sensitive data (e.g., email credentials) in the .env file located in the env/ folder. Use the following format:

SMTP_HOST=smtp.gmail.com
SMTP_USER=your-email@example.com
SMTP_PASS=your-email-password
SMTP_SECURE=tls
FROM_EMAIL=your-email@example.com
FROM_NAME="Your Name or Portfolio Name"

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 👇:

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
  1. 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.

  2. 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 credentials

  3. Edit 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:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '../vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();

$config = [
    'smtp_host' => $_ENV['SMTP_HOST'],
    'smtp_user' => $_ENV['SMTP_USER'],
    'smtp_pass' => $_ENV['SMTP_PASS'],
    'smtp_secure' => $_ENV['SMTP_SECURE'],
    'from_email' => $_ENV['FROM_EMAIL'],
    'from_name' => $_ENV['FROM_NAME']
];

// Capture form data
$name = htmlspecialchars($_POST['name'] ?? 'Name not provided');
$email = htmlspecialchars($_POST['email'] ?? 'Email not provided');
$message = htmlspecialchars($_POST['message'] ?? 'Message not provided');

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host       = $config['smtp_host'];
    $mail->SMTPAuth   = true;
    $mail->Username   = $config['smtp_user'];
    $mail->Password   = $config['smtp_pass'];
    $mail->SMTPSecure = $config['smtp_secure'];
    $mail->Port       = 587;
    
    $mail->setFrom($config['from_email'], $config['from_name']);
    
    // Add the recipient email address here
    $mail->addAddress('your-email-here'); 

    $mail->isHTML(true);
    $mail->Subject = 'New Message from Rosure Portfolio';
    $mail->Body    = "
        <h2>New Message</h2>
        <p><strong>Name:</strong> {$name}</p>
        <p><strong>Email:</strong> {$email}</p>
        <p><strong>Message:</strong></p>
        <p>{$message}</p>
    ";

    $mail->send();
    echo "Message sent succesfully.";
} catch (Exception $e) {
    http_response_code(500);
    echo "Error sending message: {$mail->ErrorInfo}";
}
?>

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.

  1. 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:

  1. 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

  2. Using PHP's Built-in Server:

    • Navigate to the public folder of your project in the terminal.

    • Start the server with the command:

      php -S localhost:8000
    • Open your browser and navigate to: http://localhost:8000

Previous06 Projects Section (Adding a New Porject)NextSet Default Theme and Configure Toggle Button

Last updated 4 months ago

🎨