为什么在包含我的PHP脚本时不执行

时间:2020-08-07 05:47:13

标签: php amazon-web-services apache amazon-s3 amazon-ec2

我会尽量弄清楚: 我正在使用Amazon EC2,Amazon S3,Amazon RDS和现在的Amazon SES开发Web应用程序。 我正在尝试在注册后实施电子邮件验证。不幸的是,由于某些原因,php邮件功能在Amazon EC2实例上不起作用,我尝试重新安装sendmail和postfix并调查php.ini文件无济于事。这导致我遵循以下规则:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-php.html

可以正常工作,但前提是您使用putty从通过ssh访问的shell中运行它。

Using username "ec2-user".
Authenticating with public key "imported-openssh-key"
Last login: Fri Aug  7 05:32:51 2020 from 116.90.72.92

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
[ec2-user@ip-172-31-24-178 ~]$ sudo su
[root@ip-172-31-24-178 ec2-user]# php amazon-ses-sample.php
Email sent! Message ID: 010f0173c771a9d3-b9d5e167-7b19-4b42-a550-dce52a8b83d5-000000
[root@ip-172-31-24-178 ec2-user]#


我所有的php脚本都是从我正在使用的EC2实例上运行的apache(httpd)服务器运行的。脚本运行良好,但我尝试包含Amazon ses示例(请参见下面的代码),但它会出错并且无法执行:


//include 'amazon-ses-sample.php'; <-------**HERE: including this doesn't work.**
?>

此脚本来自AWS,仅在您使用以下命令时有效:$ php amazon-ses-sample.php


// If necessary, modify the path in the require statement below to refer to the
// location of your Composer autoload.php file.
 require '/var/www/aws/aws-autoloader.php';

use Aws\Ses\SesClient;
use Aws\Exception\AwsException;

// Create an SesClient. Change the value of the region parameter if you're
// using an AWS Region other than US West (Oregon). Change the value of the
// profile parameter if you want to use a profile in your credentials file
// other than the default.
$SesClient = new SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region'  => 'us-east-2'
]);

// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'ngudy049@gmail.com';

// Replace these sample addresses with the addresses of your recipients. If
// your account is still in the sandbox, these addresses must be verified.
$recipient_emails = ['ngudy049@gmail.com','ngudy049@gmail.com'];

// Specify a configuration set. If you do not want to use a configuration
// set, comment the following variable, and the
// 'ConfigurationSetName' => $configuration_set argument below.
//$configuration_set = 'ConfigSet';

$nameString = "username3";

$subject = 'Amazon web app sign up test';
$plaintext_body = "This email was sent with Amazon SES using the AWS SDK for PHP.".
"Username: ".$nameString." Password: ".$passwordString."Please click this link to activate your account:".
"http://www.yourwebsite.com/verify.php?email=".$emailString."&hash=".$hashString; // Our message above including the link

$html_body =  "<p>Thanks for signing up! Your account has been created, you can login".
" with the following credentials after you have activated your account by pressing the url below.</p>".
"<br>".
"Username: ".$usernameString. "<br>".
"Password: ".$passwordString. "<br>".
"<br>".
"http://3.23.88.212/verify.php?email=".$emailString."&hash=".$hashString; // Our message above including the link;


$char_set = 'UTF-8';

try {
    $result = $SesClient->sendEmail([
        'Destination' => [
            'ToAddresses' => $recipient_emails,
        ],
        'ReplyToAddresses' => [$sender_email],
        'Source' => $sender_email,
        'Message' => [
          'Body' => [
              'Html' => [
                  'Charset' => $char_set,
                  'Data' => $html_body,
              ],
              'Text' => [
                  'Charset' => $char_set,
                  'Data' => $plaintext_body,
              ],
          ],
          'Subject' => [
              'Charset' => $char_set,
              'Data' => $subject,
          ],
        ],
        // If you aren't using a configuration set, comment or delete the
        // following line
        //'ConfigurationSetName' => $configuration_set,
    ]);
    $messageId = $result['MessageId'];
    echo("Email sent! Message ID: $messageId"."\n");
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n");
    echo "\n";
}
?>

我尝试将amazon-ses-sample.php的内容添加到我的php脚本中,但这不起作用。仅当您使用时才有效:

[root@ip-172-31-24-178 ec2-user]# php /var/www/html/amazon-ses-sample.php

1 个答案:

答案 0 :(得分:0)

根据您的代码判断,我看到以下观察结果。

当您运行/var/www/html/amazon-ses-sample.php时,它可以工作,但是当您在CLI上以root用户身份调用时,否则对于apache用户来说,它将不工作。

当您使用include运行时,这只会引发警告以突出显示无法包含该文件(因此请确保检查您的本地PHP日志),而require将显式致命错误,如果它无法到达文件。

可能会导致此问题的原因很多,但以下是可能的问题:

  • 检查相对于脚本的文件路径,如果您仅调用include file.php,则会相对于当前目录添加该路径。
  • 确保文件的本地文件权限允许apache用户/组读取并执行文件。此属性随操作系统/安装的不同而不同,可以设置为自定义值。