我在尝试使用 PHP 邮件程序发送电子邮件时收到错误。我遵循了一个利用本地主机进行测试的教程,但是,我正在实时服务器上进行测试。我相信我的目录设置方式可能会导致问题。我使用 Composer 将 PHP Mailer 安装到目录中,并将整个目录上传到服务器上。以下是我遇到的错误:
[16-Jan-2018 08:36:33] PHP Warning: require_once(__DIR__/composer/autoload_real.php)
[<a href='function.require-once'>function.require-once</a>]:
failed to open stream: No such file or directory in /public_html/netWorks/emailtests/vendor/autoload.php on line 5
[16-Jan-2018 08:36:33] PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]:
Failed opening required '__DIR__/composer/autoload_real.php' (include_path='.:/usr/lib64/php:/usr/share/pear') in
/public_html/netWorks/emailtests/vendor/autoload.php on line 5
这是索引.php -
<?php
require 'vendor/autoload.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'kevin@kevin.com'; // SMTP username
$mail->Password = 'PW0RD'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('kmoriarty@kevin.com', 'Mailer');
$mail->addAddress('kevin@kevin.com', 'Kevin'); // Add a recipient
$mail->addReplyTo('kevin@kevin.com', 'Email Master Flex');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//Attachments
//$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'You are receiving this because...';
$mail->Body = 'Stack Overflow is <b>AWESOME!</b>';
$mail->AltBody = 'You are receiving this because Stack Overflow is AWESOME!';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
我尝试将自动加载的目录更改为/vendor/autoload.php但这也失败了。
我的父文件夹有供应商文件夹,composer.json,composer.lock和我的index.php。供应商文件夹内是作曲家文件夹,phpmailer文件夹和autoload.php。有没有可能这是我遇到问题的地方?
我已经升级到PHP版本5.6 - 我现在收到此错误:
[16-Jan-2018 09:23:03 America/Denver] PHP Fatal error: Class 'PHPMailer' not found in /public_html/netWorks/emailtests/index.php on line 4
感谢 Synchro 关于升级 PHP 的建议,我能够解决。我还需要添加命名空间:
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;