Search code examples
phpcomposer-phpphpmailer

Warning: require_once(Composer/PHPMailer/vendor/phpmailer/phpmailer/src/autoload.php): failed to open stream: No such file or directory


I get an error of Warning: require_once(Composer/PHPMailer/vendor/phpmailer/phpmailer/src/autoload.php): failed to open stream: No such file or directory in /home/hyperspace/public_html/contact.php on line 5 and Fatal error: require_once(): Failed opening required 'Composer/PHPMailer/vendor/phpmailer/phpmailer/src/autoload.php' (include_path='.:/opt/alt/php72/usr/share/pear') in /home/hyperspace/public_html/contact.php on line 5, I do not have root access to my cpanel. You can check the error at http://hyperspacedesigns.co.za/contact.php. My code that I use is:

 <?php
 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;

 require_once "Composer/PHPMailer/vendor/phpmailer/phpmailer/src/autoload.php"; //This is line 5//

    $mail = new PHPmailer();

    $mail->Host = "";
    $mail->isSMTP();
    $mail->SMTPAuth = true;
    $mail->Username = "";
    $mail->Password = "";
    $mail->SMTPSecure = "ssl";
    $mail->Port = 465;

    $mail->addAddress('[email protected]');
    $mail->setFrom($_POST['email']);
    $mail->name = $_POST['name'];
    $mail->Subject = $_POST['subject'];
    $mail->number = $_POST['number'];
    $mail->isHTML(true);
    $mail->Body = $_POST['message'];

    if ($mail->send())
        $msg = "Your email has been sent, Thank You!";
    else
        //$msg = "Please try agian!";
        echo $mail->ErrorInfo;
?>

My html is on my contact.php file where my form is, this is my forms code:

<!-- /contact-form -->
   <section class="w3l-contact-main">
       <div class="contact-infhny py-5">
           <div class="container">
               <div class="contact-grids row py-lg-5">
                   <div class="contact-left col-lg-6">
                        <img src="assets/images/contact-sec.jpg" alt="" class="img-fluid">
                </div>
                <div class="contact-right col-lg-6 pl-lg-4">
                        <h3>Contact</h3>
                    <h4>Everything Starts With A Hello!</h4>
                    <p>We’re here to answer any questions you may have and create an effective solution for your instructional needs.</p>

                    <?php if ($msg != "") echo "$msg<br>"; ?>

                    <form action="contact.php" method="post" class="signin-form mt-lg-5 mt-4">
                        <div class="input-grids">
                            <input type="text" name="name" placeholder="Full name" class="contact-input" />
                            <input type="email" name="email" placeholder="Your email" class="contact-input" />
                            <input type="text" name="subject" placeholder="Subject" class="contact-input" />
                            <input type="number" name="number" placeholder="Phone number" class="contact-input" />
                        </div>
                        <div  class="form-input">
                            <textarea name="message" placeholder="Type your message here" required=""></textarea>
                        </div>
                        <div  class="form-input mb-5">
                            <label class="container"><p>Send me a copy <a href="#">privacy policy.</a></p>
                                <input type="checkbox">
                                <span class="checkmark"></span>
                            </label>
                            </div>
                        <button class="btn submit">Submit</button>
                    </form>
                </div>

            </div>
        </div>
    </div>
    <div class="map-hny">
        <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d387193.305935303!2d-74.25986548248684!3d40.69714941932609!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x89c24fa5d33f083b%3A0xc80b8f06e177fe62!2sNew+York%2C+NY%2C+USA!5e0!3m2!1sen!2sin!4v1563262564932!5m2!1sen!2sin" style="border:0" allowfullscreen=""></iframe>
   </div>
</section>
<!-- //contact-form -->

I know it will not send an email because I have not changed my submit button to work with the php but I get the error when I load the page.

This is how my files look in cpanel: enter image description here

I used composer to install PHPMailer. In my Composer file I installed PHPMailer and all of its components. I have not changed anything in my php.ini as I do not know if it has anything to do with my error. I do not know if it has anything to do with me not having root access. Can someone please help me?


Solution

  • You're not using composer correctly; this line is wrong:

    require_once "Composer/PHPMailer/vendor/phpmailer/phpmailer/src/autoload.php";
    

    You need to define a composer.json file for your project, so lets say you make your project in /home/hyperspace/public_html (though note that having library files within your web root is generally bad). cd into that directory and run composer require phpmailer/phpmailer. That will create a composer.json file, a composer.lock file, and a folder called vendor containing a PHPMailer folder and some scripts, one of which will be called autoload.php. Then, in your contact.php script you would load the autoloader like this:

    require 'vendor/autoload.php';
    

    If you can't run commands directly on the server, do all the same things locally, and then upload the whole folder, including the vendor folder.