在 AWS AMI 实例上使用 PHPMailer 发送邮件时出现问题



我是UNIX的新手,来自Windows背景。 我在 Amazon EC2 上创建了一个实例,并安装了 apache、PHP 和 MySQl。 我已经成功上传了PHP网站的文件。 一切正常,除了我在从联系表格发送邮件时遇到问题。

我在这里浏览了 AWS 教程:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-smtp-php.html

我成功安装了作曲家并在 Putty 中运行它,我看到供应商目录已创建并下载了 phpmailer 文件。

网站结构如下所示:

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

我尝试使用教程中包含的示例电子邮件脚本,如下所示:

// 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 PHPMailerPHPMailerPHPMailer;
// 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');

但是我收到以下错误:

Fatal error: Uncaught Error: Class 'PHPMailerPHPMailerPHPMailer' 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

第 10 行是

$mail = new PHPMailer;

所以我对问题是什么感到困惑。 所需的 PHPMailer 文件似乎已由作曲家正确创建,并且"vendor\autoload.php"的路径应该是正确的。

服务器设置中是否有可能遗漏了某些内容?

任何建议都感激地收到。

大卫

AWS 不再有自动加载功能,PHPMailer 应按如下方式初始化:

<?php
require("/home/site/libs/PHPMailer-master/src/PHPMailer.php");   require("/home/site/libs/PHPMailer-master/src/SMTP.php");
$mail = new PHPMailerPHPMailerPHPMailer();
$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";
} ?>

谢谢劳伦斯为我指出正确的方向...... 正如您所指出的,问题在于正在使用的phpmailer版本。

当我将此项目的 composer.json 更改为

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

并运行了作曲家更新我的脚本现在(大部分(成功运行。

看起来AWS文档中给出的示例不正确,因为它说使用phpmailer 5.2,但它提供的脚本仅适用于版本6及更高版本。

谢谢!

大卫

最新更新