How to Install and Use PHPMailer without Composer

PHPMailer is an email creation and transport class written in php. This class makes it easy to create and send text and html emails with attachments.

The easiest way to install this class is using composer. But most websites are hosted in shared space and installing composer is not an option. I am going to show you how you can install and use PHPMailer without composer.

First download PHPMailer from Github.

Extract the archive and upload the content to a folder on your web server .

Create or modify your mail sending code with the following:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';

$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2; 
$mail->Host = "your_smtp_host"; 
$mail->Port = "your_smtp_port"; // typically 587 
$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = "your_mail_username";
$mail->Password = "your_mail_password";
$mail->setFrom("your_email", "your_name");
$mail->addAddress("send_to_email_address", "send_to_Name");
$mail->Subject = 'Any_subject_of_your_choice';
$mail->msgHTML("test body"); // remove if you do not want to send HTML email
$mail->AltBody = 'HTML not supported';
$mail->addAttachment('docs/brochure.pdf'); //Attachment, can be skipped

$mail->send();

A few things to note here.

$mail->SMTPDebug is for displaying errors and can be skipped.
$mail->SMTPAuth can be commented out if authentication is not required.

Other optional stuff are commented along with the code above.

IMPORTANT: An important thing to note here is the path of the include files. If you have the mail code on a file say mail.php, this file should be placed just on the same level as the src folder of PHPMailer package. For example look at my directory structure for sending mail.

As you can see my mail.php and the src folder of the PHP Mailer Package are both on the same level. If you change this structure, then you will need to change the include paths in the mail.php file as well.