使用 Amazon SES API 和 Amazon SDK for PHP 版本 3 来向发件人授权 - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon SES API 和 Amazon SDK for PHP 版本 3 来向发件人授权

要允许其他 Amazon Web Services 账户、Amazon Identity and Access Management 用户或 Amazon 服务可代表您通过 Amazon Simple Email Service (Amazon SES) 发送电子邮件,您需要创建发送授权策略。这是一个附加到您拥有的身份的 JSON 文档。

该策略明确列出您允许哪些人、在何种条件下使用该身份。除了您以及在策略中明确授予权限的实体之外,所有其他发件人不允许发送电子邮件。一个身份可以不附加策略、附加一个策略或附加多个策略。此外,您还可以使用一个包含多个语句的策略来实现多个策略的效果。

有关更多信息,请参阅使用 Amazon SES 的发送授权

以下示例演示如何:

的所有示例代码都可以在此Amazon SDK for PHP处找到 GitHub

凭证

运行示例代码之前,请配置您的 Amazon 凭证,如 凭证 中所述。然后导入 Amazon SDK for PHP,如 基本用法 中所述。

有关使用 Amazon SES 的更多信息,请参阅 Amazon SES 开发人员指南

创建授权发件人

要授权其他 Amazon Web Services 账户 代表您发送电子邮件,请使用身份授权策略添加或更新授权,以允许从您经过验证的电子邮件地址或域发送电子邮件。要创建身份策略,请使用PutIdentityPolicy操作。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Ses\SesClient;

示例代码

$SesClient = new SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-1' ]); $identity = "arn:aws:ses:us-east-1:123456789012:identity/example.com"; $other_aws_account = "0123456789"; $policy = <<<EOT { "Id":"ExampleAuthorizationPolicy", "Version":"2012-10-17", "Statement":[ { "Sid":"AuthorizeAccount", "Effect":"Allow", "Resource":"$identity", "Principal":{ "AWS":[ "$other_aws_account" ] }, "Action":[ "SES:SendEmail", "SES:SendRawEmail" ] } ] } EOT; $name = "policyName"; try { $result = $SesClient->putIdentityPolicy([ 'Identity' => $identity, 'Policy' => $policy, 'PolicyName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

检索授权发件人的策略

返回与特定电子邮件身份或域身份关联的发送授权策略。要获得给定电子邮件地址或域名的发送授权,请使用GetIdentityPolicy操作。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Ses\SesClient;

示例代码

$SesClient = new SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-1' ]); $identity = "arn:aws:ses:us-east-1:123456789012:identity/example.com"; $policies = ["policyName"]; try { $result = $SesClient->getIdentityPolicies([ 'Identity' => $identity, 'PolicyNames' => $policies, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

列出授权发件人

要列出与当前Amazon区域中特定电子邮件身份或域名身份关联的发送授权策略,请使用ListIdentityPolicies操作。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Ses\SesClient;

示例代码

$SesClient = new SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-1' ]); $identity = "arn:aws:ses:us-east-1:123456789012:identity/example.com"; try { $result = $SesClient->listIdentityPolicies([ 'Identity' => $identity, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

撤销授权发件人的权限

通过删除与Amazon Web Services 账户该DeleteIdentityPolicy操作关联的身份策略,取消对他人使用电子邮件身份或域名身份发送电子邮件的发送授权。

导入

require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Ses\SesClient;

示例代码

$SesClient = new SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-1' ]); $identity = "arn:aws:ses:us-east-1:123456789012:identity/example.com"; $name = "policyName"; try { $result = $SesClient->deleteIdentityPolicy([ 'Identity' => $identity, 'PolicyName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }