在 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 AwsSesSesClient;
use AwsExceptionAwsException;
// 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);

相关内容

  • 没有找到相关文章

最新更新