致命错误:require():无法打开所需的'../PHPMailerAutoload.php'(include_path ='。:/ usr / local / php56 / pear')

时间:2017-09-14 04:13:55

标签: php composer-php phpmailer require

第一次使用PHPMailer,似乎无法传递这个需要的错误..我安装了composer确实需要PHPMailer / PHPMailer并尝试更改路径..但​​仍然无法传递此错误消息:

  

致命错误:require():需要打开失败   '../PHPMailerAutoload.php'(include_path ='。:/ usr / local / php56 / pear')in   /home/hiinfo53/public_html/grandbluehawaii.com/cgifile/contact_phpmailer.php   在第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('contact@grandbluehawaii.com', 'First Last');
    //Send the message to yourself, or whoever should receive contact for submissions
    $mail->addAddress('gail@hiinfo.com', '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 个答案:

答案 0 :(得分:1)

你可以通过阅读the readme来节省很多痛苦,它告诉你如何加载PHPMailer - PHPMailer 6.0已经改变了。 PHPMailerAutoload.php不再存在。有关于在the upgrading guide中升级旧版本的信息,但由于这是一个新项目,您应该永远不会达到这一点,因此我怀疑您是从一开始就使用其他地方的过时脚本开始的。不好的主意。

使用composer安装PHPMailer:

composer require phpmailer/phpmailer

在您的脚本中,将类导入您的命名空间,加载composer的自动加载器,创建一个PHPMailer实例,它将自动自动加载正确的类:

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

这就是它的全部内容。如果您使用的是SMTP,则还需要use PHPMailer\PHPMailer\SMTP。当您使用作曲家时,您几乎不需要查看vendor/中的内容。

如果您不希望或无法在服务器上运行composer,请在本地运行它,并将供应商文件夹与其余代码一起上传。

如果您不想使用作曲家,可以手动加载src/中的课程。

所有这些都包括在自述文件中。

答案 1 :(得分:0)

如果你没有作曲家(像我一样),这是另一个解决方案:

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

还在所有新对象中添加主PHPMailer类的完全限定名称。

$mail = new PHPMailer\PHPMailer\PHPMailer;