0
votes

First time using PHPMailer, and can't seem to get passed this require error .. I installed composer did the require PHPMailer/PHPMailer and tried changing the paths .. but still can't pass this error message:

Fatal error: require(): Failed opening required '../PHPMailerAutoload.php' (include_path='.:/usr/local/php56/pear') in /home/hiinfo53/public_html/grandbluehawaii.com/cgifile/contact_phpmailer.php on line 15

<?php
/**
 * This example shows how to handle a simple contact form.
 */

echo getcwd();
$msg = '';
//Don't run this unless we're handling a form submission
if (array_key_exists('email', $_POST)) {
    date_default_timezone_set('Etc/UTC');


   require '../PHPMailerAutoload.php';

    //Create a new PHPMailer instance

    $mail = new PHPMailer;
    //Tell PHPMailer to use SMTP - requires a local mail server
    //Faster and safer than using mail()
    $mail->isSMTP();
    $mail->Host = 'usm1260.sgded.com';
    $mail->Port = 465;
    $mail->SMTPSecure = 'ssl';
    //Use a fixed address in your own domain as the from address
    //**DO NOT** use the submitter's address here as it will be forgery
    //and will cause your messages to fail SPF checks
    $mail->setFrom('[email protected]', 'First Last');
    //Send the message to yourself, or whoever should receive contact for submissions
    $mail->addAddress('[email protected]', 'Grand Blue Hawaii');
    //Put the submitter's address in a reply-to header
    //This will fail if the address provided is invalid,
    //in which case we should ignore the whole request
    if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
        $mail->Subject = 'A user has contacted you from grandbluehawaii.com';
        //Keep it simple - don't use HTML
        $mail->isHTML(false);
        //Build a simple message body
        $mail->Body = <<<EOT
"Aloha,\n\n"
"A user visiting grandbluehawaii.com has sent you a message".\n\n"
"Please make sure any Reply-To's this email is directed to user's email address".\n\n"
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;
        //Send the message, check for errors
        if (!$mail->send()) {
            //The reason for failing to send will be in $mail->ErrorInfo
            //but you shouldn't display errors to users - process the error, log it on your server.
            $msg = 'Sorry, something went wrong. Please try again later.';
        } else {
            $msg = 'Message sent! Mahalo for contacting Grand Blue Hawaii.';
        }
    } else {
        $msg = 'Invalid email address, message ignored.';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact form</title>
</head>
<body>
<h1>Contact us</h1>
<?php if (!empty($msg)) {
    echo "<h2>$msg</h2>";
} ?>
<form method="POST">
    <label for="name">Name: <input type="text" name="name" id="name"></label><br>
    <label for="email">Email address: <input type="email" name="email" id="email"></label><br>
    <label for="message">Message: <textarea name="message" id="message" rows="8" cols="20"></textarea></label><br>
    <input type="submit" value="Send">
</form>
</body>
</html>
2
require '../PHPMailerAutoload.php'; this path needs review. You need to do this based on the location of the files in the project.Scuzzy
@Scuzzy I tried altering path to reach appropriate directory (e.g. ../vendor/phpmailer/phpmailer/) but still get error .. I also can't locate PHPMailerAutoload.phpgmatsushima
@Scuzzy I'm following this example, pretty exactly github.com/PHPMailer/PHPMailer/blob/master/examples/…gmatsushima
@Scuzzy this exact file correct? PHPMailerAutoload.phpgmatsushima
can you actually find that file on your computer or project files? because that is the name the script is looking for. Except to say your script wants PHPMailerAutoload.php in the parent folder relative to itself.Scuzzy

2 Answers

3
votes

You could have saved yourself a lot of pain by simply reading the readme, which tells you how to load PHPMailer - which has changed with PHPMailer 6.0. PHPMailerAutoload.php does not exist any more. There is info about upgrading form older versions in the upgrading guide, but since this is a new project, you should never have got to this point, so I suspect you got started by using an obsolete script from elsewhere. Bad idea.

Install PHPMailer using composer:

composer require phpmailer/phpmailer

In your script, import the class into your namespace, load composer's autoloader, create a PHPMailer instance, and it will autoload the right classes automatically:

use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer();

That's all there is to it. If you're using SMTP, you'll need to use PHPMailer\PHPMailer\SMTP as well. When you're using composer, you should almost never have to even look at what is in vendor/.

If you don't want to or cannot run composer on your server, run it locally, and upload the vendor folder along with the rest of your code.

If you don't want to use composer, you can load the classes in src/ manually.

All of this is covered in the readme.

0
votes

In case you don't have a composer (like me), here is another solution:

require '../PHPMailer-master/src/PHPMailer.php';
require '../PHPMailer-master/src/SMTP.php';
require '../PHPMailer-master/src/Exception.php';

Also add in all the new objects the fully-qualified name of the main PHPMailer class.

$mail = new PHPMailer\PHPMailer\PHPMailer;