Overview

If you need to send emails from your PHP application on a Windows Shared Hosting server, you’ll want to use a secure and reliable email library. Two of the most popular options are PHPMailer and SwiftMailer.

This guide focuses on PHPMailer, demonstrating how to configure and utilise it to send authenticated emails directly through SMTP from your PHP scripts.

Why Use PHPMailer?

The built-in mail() function in PHP lacks modern email standards such as authentication and encryption. PHPMailer solves this by providing:

  • SMTP authentication support
  • TLS/SSL encryption for secure delivery
  • HTML message formatting
  • File attachments and inline images
  • CC, BCC, and Reply-To handling
  • Detailed error handling and debugging options

It’s simple, secure, and ideal for use in shared hosting environments.


Step 1: Download PHPMailer

You’ll first need to download the latest PHPMailer release from the official GitHub repository:

 https://github.com/PHPMailer/PHPMailer

Instructions:

  1. Download the ZIP package.
  2. Upload it to your hosting account via File Manager or FTP.
  3. Extract the contents into your project directory.

(Recommended) Rename the extracted folder to PHPMailer for easy reference.

Step 2: Add PHPMailer to Your PHP Script

Once uploaded, include the PHPMailer class files and configure the SMTP details as shown below.
Update the SMTP host, port, username, and password according to your email provider’s details.

<?php
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

// Load PHPMailer class files
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

// Create a new PHPMailer instance
$mail = new PHPMailer(true);

try {
    // SMTP configuration
    $mail->SMTPDebug = SMTP::DEBUG_SERVER;             // Enable detailed debug output
    $mail->isSMTP();                                   // Use SMTP for sending
    $mail->Host       = 'smtp.domain.com';             // Specify main SMTP server
    $mail->SMTPAuth   = true;                          // Enable SMTP authentication
    $mail->Username   = '[email protected]';            // SMTP username
    $mail->Password   = 'Password';                    // SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;   // Enable implicit TLS encryption
    $mail->Port       = 465;                           // Port 465 for SSL, or 587 for STARTTLS

    // Recipients
    $mail->setFrom('[email protected]', 'Your Name');
    $mail->addAddress('[email protected]', 'Recipient Name');
    $mail->addReplyTo('[email protected]', 'Your Name');
    $mail->addCC('[email protected]');
    $mail->addBCC('[email protected]');

    // (Optional) Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');

    // Email content
    $mail->isHTML(true);                               // Send as HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = '<b>This is the HTML message body in bold!</b>';
    $mail->AltBody = 'This is the plain text version of the message.';

    // Send the email
    $mail->send();
    echo '✅ Message has been sent successfully.';
} catch (Exception $e) {
    echo "❌ Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
?>

You can also obtain a sample PHP code from the following article: Send mail via PHP script using PHPMailer.

Summary
Using PHPMailer (or Swift Mailer) on your Windows Shared Hosting account gives you complete control over how emails are sent, authenticated, and formatted.

It’s secure, reliable, and easy to integrate, making it the preferred choice for contact forms, password resets, and notification systems.

Was this answer helpful? 0 Users Found This Useful (0 Votes)