1

Below is my php code. I am using gcp app engine first time and it is throwing error PHP Fatal error: require(): Failed opening required 'vendor/phpmailer/phpmailer/src/Exception.php'. I already have this file in the directory mentioned, then also it shows this error. Is it regarding the new phpmailer format or something else? Please help.


<!DOCTYPE html>
<html lang="en">
  <head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>

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

  require 'vendor/phpmailer/phpmailer/src/Exception.php';
  require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
  require 'vendor/phpmailer/phpmailer/src/SMTP.php';

  // Include autoload.php file
  require 'vendor/autoload.php';
  // Create object of PHPMailer class
  $mail = new PHPMailer(true);

  $output = '';

  if (isset($_POST['submit'])) {
    $name = $_POST['contactName'];
    $email = $_POST['contactEmail'];
    $subject = $_POST['contactSubject'];
    $message = $_POST['contactMessage'];

    try {
      $mail->isSMTP();
      $mail->Host = 'smtp.gmail.com';
      $mail->SMTPAuth = true;
      // Gmail ID which you want to use as SMTP server
      $mail->Username = 'rajeshsingh80906@gmail.com';
      // Gmail Password
      $mail->Password = 'secret';
      $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
      $mail->Port = 587;

      // Email ID from which you want to send the email
      $mail->setFrom('rajeshsingh80906@gmail.com');
      // Recipient Email ID where you want to receive emails
      $mail->addAddress('ranarajesh495@gmail.com');
      // $mail->addAttachment(''); 
      $mail->isHTML(true);
      $mail->Subject = 'Form Submission';
      $mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";

      $mail->send();
      $output = '<div class="alert alert-success">
                  <h5>Thankyou! for contacting us, We\'ll get back to you soon!</h5>
                </div>';
    } catch (Exception $e) {
      $output = '<div class="alert alert-danger">
                  <h5>' . $e->getMessage() . '</h5>
                </div>';
    }
  }

?>

<title>insert page</title>
<script type="text/javascript">
        function back_to_main() {
          setTimeout(function () {
   //Redirect with JavaScript
   window.location = './index.html'
}, 5000);
        }
        </script>
  <body onload='back_to_main();'>
  thank you...
</body>
  </html>
ra459
  • 11
  • 1
  • 4
  • 1
    Are you not using composer? Why do you want to include the phpmailer files separately? Also, 'require' should be put before 'use'. – csba Jun 12 '20 at 08:22

3 Answers3

2

While searching for already existing cases on StackOverflow, I came over this one: PHP namespaces and "use"

Also, if I'm correct, you need to import the files before using the "use" statement, like this:

<?php
  require 'vendor/phpmailer/phpmailer/src/Exception.php';
  require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
  require 'vendor/phpmailer/phpmailer/src/SMTP.php';
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  use PHPMailer\PHPMailer\SMTP;
Werzaire
  • 78
  • 3
0

I don't think you need this if you installed PHPMailer via composer so I have removed this part from your code.

require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';

Try the below code. I have reformatted your code.

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

// Include Composer autoload.php file
require 'vendor/autoload.php';

// Create object of PHPMailer class
$mail = new PHPMailer(true);

$output = '';

if (isset($_POST['submit'])) {
    $name = $_POST['contactName'];
    $email = $_POST['contactEmail'];
    $subject = $_POST['contactSubject'];
    $message = $_POST['contactMessage'];

    try {
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        // Gmail ID which you want to use as SMTP server
        $mail->Username = 'rajeshsingh80906@gmail.com';
        // Gmail Password
        $mail->Password = 'secret';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;

        // Email ID from which you want to send the email
        $mail->setFrom('rajeshsingh80906@gmail.com');
        // Recipient Email ID where you want to receive emails
        $mail->addAddress('ranarajesh495@gmail.com');
        // $mail->addAttachment(''); 
        $mail->isHTML(true);
        $mail->Subject = 'Form Submission';
        $mail->Body = "<h3>Name : $name <br>Email : $email <br>Message : $message</h3>";

        $mail->send();
        $output = '<div class="alert alert-success"><h5>Thankyou! for contacting us, We\'ll get back to you soon!</h5></div>';
    }
    catch (Exception $e) {
        $output = '<div class="alert alert-danger"><h5>' . $e->getMessage() . '</h5></div>';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <title>insert page</title>
    <script type="text/javascript">
        function back_to_main() {
            setTimeout(function () {
                //Redirect with JavaScript
                window.location = './index.html'
            }, 5000);
        }
    </script>
</head>
<body onload='back_to_main();'>
  thank you...
</body>
</html>

Please note I have not tested the above code.

For more information please read https://github.com/PHPMailer/PHPMailer

Miroslav
  • 336
  • 1
  • 7
  • I tired the above thing, its not working as well. It throws the following error now- "The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol." – ra459 Jun 12 '20 at 08:58
  • @ra459 this is not something wrong with the code that I have given you! This is an Exception that PHPMailer is throwing. You just need to configure your PHPMailer settings properly. I have fixed the issue you had originally. – Miroslav Jun 12 '20 at 09:09
  • I have resolved the above irrelevant issue, but after applying changes as you said,now i am getting this error. require(): Failed opening required 'vendor/autoload.php' (include_path='.;/base/data/home/apps/j~dt-website-278313/20200612t145411.427336870951406362/;/base/alloc/tmpfs/dynamic_runtimes/php55g/6485fc5dd706e6f8/sdk') – ra459 Jun 12 '20 at 10:55
  • Is there any reason why you are unable to `require` the `autoload.php` file which is generated automatically by Composer? I'm guessing you haven't installed PHPMailer properly via Composer or the path to the `autoload.php` file is incorrect. – Miroslav Jun 13 '20 at 22:24
  • I have resolved the issue. Actually gcp doesn't identify autolaod.php so i had to use gcp mailer. – ra459 Jun 21 '20 at 06:48
0

If you are using linux and download this without composer, give the permission to the PHPMailer folder.

sudo chmod -R 777 youphpmailerfolder

In my case I put the folder as PHPMailer inside a folder called config, inside my root project. So, what I do was enter in my folder config from the project and run:

sudo chmod -R 777 PHPMailer

I hope this helps!