Search code examples
phpphpmailer

Unable to locate the PHPmailer library : getting a failed to open stream message


I'm trying to send emails using the phpmailer library that I have downloaded manually. I have tried to follow the tutorial provided on https://alexwebdevelop.com/phpmailer-tutorial/ and I did not choose the option to use composer, since my knowledge in Composer are very limited .. Please bear with me :)

I got an src zip file that I have installed on my host's server in one of my folder as shown on the picture below:

enter image description here

When running the PHP script, I'm getting the following error message :

enter image description here

My code source 'send_mail_3.php' is installed in the folder called 'php' (web2\php)

I'm asking for support today, because I don't know where to start my investigation.

I hope this question will be understandable and useful for other users.

Thanks for your help.

Here is the contents of my send_mail_3.php:

<?php

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

/* Exception class. */
require '\PHPMailer\src\Exception.php';

/* The main PHPMailer class. */
require '\PHPMailer\src\PHPMailer.php';

/* SMTP class, needed if you want to use SMTP. */
require '\PHPMailer\src\SMTP.php';

/* Create a new PHPMailer object. Passing TRUE to the constructor enables exceptions. */
$mail = new PHPMailer(TRUE);

/* Open the try/catch block. */
try {
   /* Set the mail sender. */
   $mail->setFrom('myemailaddress@google.com', 'Darth Vader');

   /* Add a recipient. */
   $mail->addAddress('myemailaddress@google.com', 'Emperor');

   /* Set the subject. */
   $mail->Subject = 'Force';

   /* Set the mail message body. */
   $mail->Body = 'There is a great disturbance in the Force.';

   /* Finally send the mail. */
   $mail->send();
}
catch (Exception $e)
{
   /* PHPMailer exception. */
   echo $e->errorMessage();
}
catch (\Exception $e)
{
   /* PHP exception (note the backslash to select the global namespace Exception class). */
   echo $e->getMessage();
}

Solution

  • You’re confusing paths and namespaces. Your use statements are correct, but your require paths are not. Assuming you have put PHPMailer in a folder called PHPMailer next to your script, it should be:

    /* Exception class. */
    require './PHPMailer/src/Exception.php';
    
    /* The main PHPMailer class. */
    require './PHPMailer/src/PHPMailer.php';
    
    /* SMTP class, needed if you want to use SMTP. */
    require './PHPMailer/src/SMTP.php';
    

    Beyond this, learn how to use composer.