How to Install and Use PHPMailer without Composer

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.

How to 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();
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;

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

Other code Link work For me Gmail

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; // 0 = off (for production use) - 1 = client messages - 2 = client and server messages
$mail->Host = "smtp.gmail.com"; // use $mail->Host = gethostbyname('smtp.gmail.com'); // if your network does not support SMTP over IPv6
$mail->Port = 587; // TLS only
$mail->SMTPSecure = 'tls'; // ssl is depracated
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;
$mail->setFrom($emailFrom, $emailFromName);
$mail->addAddress($emailTo, $emailToName);
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->msgHTML("test body"); //$mail->msgHTML(file_get_contents('contents.html'), __DIR__); //Read an HTML message body from an external file, convert referenced images to embedded,
$mail->AltBody = 'HTML messaging not supported';
// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file

if(!$mail->send()){
    echo "Mailer Error: " . $mail->ErrorInfo;
}else{
    echo "Message sent!";
}

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.

Leave a Comment