本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon SES API 和Amazon SDK for PHP版本 3
要启用其他Amazon账户,Amazon Identity and Access Management用户,或者Amazon要代表您通过 Amazon Simple Email Service (Amazon SES) 发送电子邮件,您需要创建发送授权策略。这是一个附加到您拥有的身份的 JSON 文档。
该策略明确列出您允许哪些人、在何种条件下使用该身份。除了您以及在策略中明确授予权限的实体之外,所有其他发件人不允许发送电子邮件。一个身份可以不附加策略、附加一个策略或附加多个策略。此外,您还可以使用一个包含多个语句的策略来实现多个策略的效果。
有关更多信息,请参阅使用 Amazon SES 的发送授权。
以下示例演示如何:
-
使用 PutIdentityPolicy 创建授权发件人。
-
使用 GetIdentityPolicies 检索授权发件人的策略。
-
使用 ListIdentityPolicies 列出授权发件人。
-
使用 DeleteIdentityPolicy 撤销授权发件人的权限。
Amazon SDK for PHPGitHub 上提供了
凭证
运行示例代码之前,请配置您的Amazon凭证,如中所述设置 凭证. 然后导入Amazon SDK for PHP,如中所述基本用法.
有关使用 Amazon SES 的更多信息,请参阅Amazon SES 开发人员指南.
创建授权发件人
授权另一个Amazon要代表您发送电子邮件,请使用身份识别策略添加或更新授权,允许从您经过验证的电子邮件地址或域发送电子邮件。要创建身份策略,请使用 PutIdentityPolicy 操作。
导入
require 'vendor/autoload.php'; use Aws\Ses\SesClient; use Aws\Exception\AwsException;
示例代码
$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\Ses\SesClient; use Aws\Exception\AwsException;
示例代码
$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区域,请使用ListIdentityPoliciesoperation.
导入
require 'vendor/autoload.php'; use Aws\Ses\SesClient; use Aws\Exception\AwsException;
示例代码
$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通过删除关联的身份策略,通过电子邮件身份或域身份发送电子邮件。DeleteIdentityPolicyoperation.
导入
require 'vendor/autoload.php'; use Aws\Ses\SesClient; use Aws\Exception\AwsException;
示例代码
$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"; }