1

I'm fairly new to UNIX, coming from a Windows background. I've created an instance on Amazon EC2 and installed apache, PHP and MySQl. i've successfully uploaded the files for a PHP website. Everything works well, except i have a problem sending mails from a contact form.

i went through the AWS tutorial here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-php.html

i successfully installed composer and ran it in Putty and i see the vendor directory is created and the phpmailer files are downloaded.

site structure looks like this:

html
test_mail.php
--vendor
----bin
----composer
----phpmailer
----autoload.php

I've tried using the sample email script included in the tutorial which looks something like this :

// If necessary, modify the path in the require statement below to refer to the 
// location of your Composer autoload.php file.
require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;

// Instantiate a new PHPMailer 
$mail = new PHPMailer;

// Tell PHPMailer to use SMTP
$mail->isSMTP();

$mail->SMTPDebug = 2;

// Replace sender@example.com with your "From" address. 
// This address must be verified with Amazon SES.
$mail->setFrom('sender@example.com', 'Sender Name');

but i get the following error:

Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailer' not found in /var/www/testSite/html/test_mail.php:10 Stack trace: #0 {main} thrown in /var/www/testSite/html/test_mail.php on line 10

line 10 is

$mail = new PHPMailer;

so i'm stumped as to what the problem is. The required PHPMailer files seem to have been created correctly by composer, and the path to 'vendor\autoload.php' should be correct.

is it possible there's something in the server setup I've missed?

any suggestions gratefully received.

David

Dog
  • 618
  • 1
  • 9
  • 26
  • The problem was posted please read this. https://stackoverflow.com/questions/28906487/fatal-error-class-phpmailer-not-found – YOUNES.EK Sep 04 '18 at 16:38
  • 1
    Odd, its suggesting `~5.2` but that version does not use the autoloader :/ install the latest version of phpmailer and it should be fine (6.0) – Lawrence Cherone Sep 04 '18 at 16:39

2 Answers2

0

AWS Does not have autoload anymore, and PHPMailer should be initialized as follows:

 <?php

      require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");   require("/home/site/libs/PHPMailer-master/src/SMTP.php");

        $mail = new PHPMailer\PHPMailer\PHPMailer();
        $mail->IsSMTP(); // enable SMTP

        $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
        $mail->SMTPAuth = true; // authentication enabled
        $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 465; // or 587
        $mail->IsHTML(true);
        $mail->Username = "xxxxxx";
        $mail->Password = "xxxx";
        $mail->SetFrom("xxxxxx@xxxxx.com");
        $mail->Subject = "Test";
        $mail->Body = "hello";
        $mail->AddAddress("xxxxxx@xxxxx.com");

         if(!$mail->Send()) {
            echo "Mailer Error: " . $mail->ErrorInfo;
         } else {
            echo "Message has been sent";
         } ?>
Dragon Kyn
  • 68
  • 7
0

Thank you Lawrence for pointing me in the right direction... The problem was, as you pointed out, with the version of phpmailer being used.

when i changed the composer.json for this project to

{
    "require": {
         "phpmailer/phpmailer":"~6.0"   
    }
}

and ran the composer update my scripts now run (mostly) successfully.

It looks like the example given in the AWS docs is incorrect as it says to use phpmailer 5.2, but the script it gives only works in version 6 onwards.

thank you!

David

Dog
  • 618
  • 1
  • 9
  • 26