在 null 上调用成员函数 sendEmail()

Call to a member function sendEmail() on null

我正在使用 AWS 示例为 PHP 调用 AWS SDK。将代码转换为函数时出现以下错误:未捕获错误:调用成员函数 sendEmail()

第 41 行似乎是问题所在:$result = $SesClient->sendEmail([

我试过删除结果变量并注释掉它的用法

当我 运行 代码和它不是函数时它工作正常,我不确定我在这里做错了什么,我确信这可能是一个简单的错误。

提前感谢您的帮助

<?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 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-1'
]);
function send_email($recipient, $message, $subject){

    // Replace sender@example.com with your "From" address.
    // This address must be verified with Amazon SES.
    $sender_email = 'sender@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 = ['recipient1@example.com','recipient2@example.com'];
    $recipient_emails = [$recipient];

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

    $subject = $subject;
    $plaintext_body = $message.PHP_EOL.'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
    $html_body =  '<h1>'.$message.'</h1>';
    $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";
    }
    return 'success';
}
echo send_email('test@gmail.com', 'test', 'test subject');

尝试在 send_email 函数中声明 $SesClient

它在没有函数的情况下工作,因为您在函数外声明了 $SesClient。在函数之后声明它 - 或者通过

将它传递给函数
function function send_email($recipient, $message, $subject, $SesClient) {
/* snip */
}

echo send_email('test@gmail.com', 'test', 'test subject', $SesClient);