【发布时间】:2019-06-25 08:36:01
【问题描述】:
我使用 composer 安装了 AWS PHP SDK,但不确定如何在我现有的类中使用 AWS SES。
我使用了 SES PHP Docs 网站中的示例代码。我能够创建一个 test.php 文件来测试该代码并且它可以工作。但是,当尝试在我的班级中使用 SES 时,我的页面中出现错误 500。
我尝试在 Class 文件中包含自动加载文件。
<?php
namespace Main\messaging;
use vendor\Aws\Ses\SesClient;
use vendor\Aws\Exception\AwsException;
class Email
{
public function sendEmail(){
// 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-west-2'
]);
// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
$sender_email = 'welcome@myemail.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 = ['test@email.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';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ;
$html_body = '<h1>AWS Amazon Simple Email Service Test Email</h1>'.
'<p>This email was sent with <a href="https://aws.amazon.com/ses/">'.
'Amazon SES</a> using the <a href="https://aws.amazon.com/sdk-for-php/">'.
'AWS SDK for PHP</a>.</p>';
$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";
}
}
}
我期望看到确认消息回显并收到电子邮件。然而,这不是我得到的。我认为我没有正确地将 AWS 库包含在我的课程中。
【问题讨论】:
-
你得到了什么做?
-
我在课堂上使用它时什么也得不到。当我调用该函数时,我在浏览器中收到错误 500。但是,如果我不在课程中使用它,而只是通过终端调用 php 文件,我会成功发送电子邮件。
-
如果“无”表示空白页,您应该检查错误日志或调整错误报告设置。 500 + 空白页意味着您在某处得到了有用的错误消息。
-
(如果您使用 Composer,我敢打赌,错误是您使用的是
use vendor\...。vendor部分通常应该省略。 -
我正在使用作曲家。我试试看
标签: php amazon-web-services amazon-simple-email-service