【问题标题】:problem sending mail with PHPMailer on AWS AMI instance在 AWS AMI 实例上使用 PHPMailer 发送邮件时出现问题
【发布时间】:2018-09-04 16:30:42
【问题描述】:

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

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

我成功安装composer并在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 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');

但我收到以下错误:

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

第 10 行是

$mail = new PHPMailer;

所以我不知道问题是什么。 所需的 PHPMailer 文件似乎已由 composer 正确创建,并且 'vendor\autoload.php' 的路径应该是正确的。

我错过了服务器设置中的某些内容吗?

任何建议都非常感谢。

大卫

【问题讨论】:

  • 问题已发布,请阅读此内容。 stackoverflow.com/questions/28906487/…
  • 奇怪,它建议 ~5.2 但该版本不使用自动加载器:/ 安装最新版本的 phpmailer 应该没问题(6.0)

标签: php amazon-web-services amazon-ec2 phpmailer


【解决方案1】:

AWS没有自动加载了,PHPMailer应该初始化如下:

 <?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";
         } ?>

【讨论】:

  • vendor/autoload.php 由 composer 创建,与 AWS 没有直接关系
【解决方案2】:

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

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

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

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

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

谢谢!

大卫

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 2018-05-16
    • 2011-03-28
    • 2010-11-20
    • 1970-01-01
    相关资源
    最近更新 更多