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

我正在使用 AWS 示例来调用适用于 PHP 的 AWS 开发工具包。将代码转换为函数时,出现以下错误:未捕获错误:调用成员函数 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';


波斯汪
浏览 124回答 2
2回答

翻阅古今

尝试在 send_email 函数中声明 $SesClient

一只名叫tom的猫

它在没有函数的情况下工作,因为您在函数之外声明了 $SesClient 。在函数之后声明它 - 或通过它传递给函数function function send_email($recipient, $message, $subject, $SesClient) {/* snip */}echo send_email('test@gmail.com', 'test', 'test subject', $SesClient);
打开App,查看更多内容
随时随地看视频慕课网APP