Search code examples
phpemailsmtpphpmailer

I'm trying to connect with smtp email when I got this error, how can I fixed this?


I'm trying to connect with smtp email when I got this error, how can I fixed this? It says " Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\payroll\phpmailer\index.php on line 9

Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\payroll\phpmailer\index.php on line 9"

here is my code

<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 1;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'smtp-relay.sendinblue.com';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'XXXXXXXXXX@gmail.com';                     //SMTP username
    $mail->Password   = 'XXXXXXXXXXXXX';                               //SMTP password
    $mail->SMTPSecure = 'tls';            //Enable implicit TLS encryption
    $mail->Port       = 587;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

    //Recipients
    $mail->setFrom('XXXXXXXXXX@gmail.com', 'Renz');
    //Add a recipient
    $mail->addAddress('ellen@example.com');               //Name is optional
   
    //Attachments
   

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
};

Solution

  • If autoload.php exists, it will be a path problem. The current working directory might not be where you think it is, so perhaps try changing it to:

    require __DIR__ . '/vendor/autoload.php';
    

    which will force it to be relative to this file.

    @Markus AO's comment made me think of something else. Your path looks a bit odd. When you use PHPMailer from a project, you would normally expect PHPMailer to be inside the project, but it looks like you've done the reverse, putting your scripts inside PHPMailer's folder. I'd expect your project to look something like this:

    C:\xampp\htdocs\payroll
        index.php
        \vendor
            autoload.php
            \composer
            \PHPMailer
                \src
                    PHPMailer.php
                    ...
    

    but it looks like you've arranged it inside out.