How to use PHPMailer without composer?
Asked Answered
D

3

24

I'd like to use the latest PHPMailer library with require_once() instead of messing around with Composer. I'd like a pure xcopy deployment with minimal fuss.

Here's what I'm attempting to do:

require_once("src/PHPMailer.php");
$mail = new PHPMailer;
$mail->isSMTP(); 
$mail->SMTPDebug = 2;
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$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->AltBody = 'HTML messaging not supported';

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

I get the error message: Fatal error: Class PHPMailer not found in [....]\EmailTester.php on line 21

Line 21 is this: $mail = new PHPMailer;

This line is just a guess on my part: require_once("src/PHPMailer.php"); - clearly I need to include some file or files, but I can't tell which.

I'm working from the gmail example on github which is also not included in the zip download. But I can navigate to it in github. In that example file it begins like this:

use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/autoload.php';
$mail = new PHPMailer;

I see no autoload.php file in the zip download, and after googling all over I see this implies using Composer. But there must be some way to simply do an include and get the files I need.

A few things puzzle me about this PHPMailer library and perhaps github in general:

  1. When I download PHP Mailer from GitHub, why are so many listed files and folders not included in the downloaded zip file?

enter image description here

  1. Why do they reference autoload.php which doesn't exist in the zip download?
  2. Clearly I don't understand some things about github, but why not provide a working code sample instead of referencing dependencies that don't exist in the download, forcing people to find it elsewhere and hope they can figure out how to come back and plug it in correctly?
  3. In this YouTube video titled Send Emails with PHP & Gmail, he downloads the same zip I downloaded and from the same place, yet his zip contains different files, including PHPMailerAutoload.php. Why am I getting completely different files than he gets? That video was published March 4, 2017 -- so, less than 1 year ago -- has it really changed so much since then?

In summary: How can I get PHPMailer working without external dependencies and installations such as Composer, and instead use require_once() to get what I need?

Discotheque answered 6/1, 2018 at 15:4 Comment(7)
github.com/PHPMailer/PHPMailer has explicit instructions on how to use it without composer. "Alternatively, if you're not using composer, copy the contents of the PHPMailer folder into one of the include_path directories specified in your PHP configuration and load each class file manually: [...]"Grasping
Plus, also says right there on that page, "If you don't speak git or just want a tarball, click the 'zip' button on the right of the project page in GitHub, though note that docs and examples are not included in the tarball."Grasping
CBroe is entirely correct. The other thing you’re missing is namespace support - you still need those use statements, and that’s what is causing the class not found error.Ducan
The general reason for focusing on using composer is that it’s such a massive win. Even in your trivially simple example, it’s already more complicated than using composer. Many people complained that the tar ball contained too many files for production, so docs and examples were excluded using git config options.Ducan
You’ve also suggested a common misapprehension - composer does not add any dependencies. For deployment all that’s added is the vendor folder and the autoload.php file, whether you have 1 dependency or 1000.Ducan
Composer should burn in hell!Caseous
You have some great comments and answers here. But since no one answered your question "What/why/where is autoload.php" for you and anyone else curious, this is a composer-based file that is generated by composer and automatically includes all the dependencies you've requested in your composer file.Tzar
D
40

Here's the full working example (though you see a few variables that must be defined and set):

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!";
}
Discotheque answered 6/1, 2018 at 15:21 Comment(4)
From what I've been hearing, and from a small bit of experience using, commenting out the $mail->isSMTP(); line is sometimes necessary to get things to work.Virchow
Good comment. I followed the AWS's instructions carefully, but the script didn't work. Commenting this line out did the trick. Thank you.Hutchinson
I use the same way but it kills my page :( i use newest version! What could be the issue?Americano
I managed to fix it. (Actually PHPmailer was all right) The issue was with my server ports that was not open.Americano
C
5

PHP Mailer original source : PHP Mailer In gitHub

// for use PHP Mailer without composer : 

// ex. Create a folder in root/PHPMAILER
// Put on this folder this 3 files find in "src" 
// folder of the distribution : 
// PHPMailer.php , SMTP.php , Exception.php
  
  // include PHP Mailer
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  include dirname(__DIR__) .'/PHPMAILER/PHPMailer.php';
  include dirname(__DIR__) .'/PHPMAILER/SMTP.php';
  include dirname(__DIR__) .'/PHPMAILER/Exception.php';

  // i made a function 

   /*
    *
    * Function send_mail_by_PHPMailer($to, $from, $subject, $message);
    * send a mail by PHPMailer method
    * @Param $to -> mail to send
    * @Param $from -> sender of mail
    * @Param $subject -> suject of mail
    * @Param $message -> html content with datas
    * @Return true if success / Json encoded error message if error 
    * !! need -> classes/Exception.php - classes/PHPMailer.php - classes/SMTP.php
    *
    */
    function send_mail_by_PHPMailer($to, $from, $subject, $message){

          // SEND MAIL by PHP MAILER
          $mail = new PHPMailer();
          $mail->CharSet = 'UTF-8';
          $mail->isSMTP(); // Use SMTP protocol
          $mail->Host = 'your_host.com'; // Specify  SMTP server
          $mail->SMTPAuth = true; // Auth. SMTP
          $mail->Username = 'my_mail@your_host.com'; // Mail who send by PHPMailer
          $mail->Password = 'your_passord_of_your_box'; // your pass mail box
          $mail->SMTPSecure = 'ssl'; // Accept SSL
          $mail->Port = 465; // port of your out server
          $mail->setFrom($from); // Mail to send at
          $mail->addAddress($to); // Add sender
          $mail->addReplyTo($from); // Adress to reply
          $mail->isHTML(true); // use HTML message
          $mail->Subject = $subject;
          $mail->Body = $message;

          // SEND
          if( !$mail->send() ){

              // render error if it is
              $tab = array('error' => 'Mailer Error: '.$mail->ErrorInfo );
              echo json_encode($tab);
              exit;
          }
          else{
              // return true if message is send
              return true;
          }

    }
    /*
    *
    * END send_mail_by_PHPMailer($to, $from, $subject, $message)
    * send a mail by PHPMailer method
    *
    */
  
    // use function :
    send_mail_by_PHPMailer($to, $from, $subject, $message);
Cockup answered 30/4, 2021 at 0:6 Comment(0)
C
4

This worked for me, I also downloaded phpmailer from this address

https://sourceforge.net/projects/phpmailer/

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';
Concerto answered 17/11, 2018 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.