

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用第 3 適用於 PHP 的 AWS SDK 版的 Amazon SES 範例
<a name="ses-examples"></a>

Amazon Simple Email Service (Amazon SES) 是一種電子郵件平台，可讓您使用自己的電子郵件地址和網域，以簡單、節省成本的方式來傳送和接收電子郵件。如需 Amazon SES 的詳細資訊，請參閱《[Amazon SES 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

AWS 提供兩種版本的 Amazon SES 服務，相對地，適用於 PHP 的 SDK 提供兩種版本的用戶端：[SesClient](https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.Ses.SesClient.html) 和 [SesV2Client](https://docs.aws.amazon.com/aws-sdk-php/v3/api/class-Aws.SesV2.SesV2Client.html)。雖然呼叫方法或結果的方式可能不同，但用戶端的功能在許多情況下會重疊。這兩個 APIs 也提供獨佔功能，因此您可以使用這兩個用戶端來存取所有功能。

本節中的範例都使用原始 `SesClient`。

第 3 適用於 PHP 的 AWS SDK 版的所有範例程式碼都可在 [ GitHub 上取得](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)。

**Topics**
+ [驗證電子郵件地址](ses-verify.md)
+ [使用電子郵件範本](ses-template.md)
+ [管理電子郵件篩選條件](ses-filters.md)
+ [使用電子郵件規則](ses-rules.md)
+ [監控您的傳送活動](ses-send-email.md)
+ [授權寄件者](ses-sender-policy.md)

# 使用 Amazon SES API 和第 3 適用於 PHP 的 AWS SDK 版驗證電子郵件身分
<a name="ses-verify"></a>

當您第一次開始使用 Amazon Simple Email Service (Amazon SES) 帳戶時，所有寄件者和收件人都必須 AWS 在您傳送電子郵件的相同區域中進行驗證。如需如何傳送電子郵件的詳細資訊，請參閱[使用 Amazon SES 傳送電子郵件](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/sending-email.html)。

下列範例示範如何：
+ 使用 [VerifyEmailIdentity](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#verifyemailidentity) 驗證電子郵件地址。
+ 使用 [VerifyDomainIdentity](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#verifydomainidentity) 驗證電子郵件網域。
+ 使用 [ListIdentities](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listidentities) 列出所有電子郵件地址。
+ 使用 [ListIdentities](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listidentities) 列出所有電子郵件網域。
+ 使用 [DeleteIdentity](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deleteidentity) 移除電子郵件地址。
+ 使用 [DeleteIdentity](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deleteidentity) 移除電子郵件網域。

您可以在 GitHub 上 適用於 PHP 的 AWS SDK 取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

如需使用 Amazon SES 的詳細資訊，請參閱《[Amazon SES 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

## 驗證電子郵件地址
<a name="verifying-email-addresses"></a>

Amazon SES 只能從已驗證的電子郵件地址或網域傳送電子郵件。透過驗證電子郵件地址，您可以證明您是該地址的擁有者，並希望允許 Amazon SES 從該地址傳送電子郵件。

當您執行下列程式碼範例時，Amazon SES 會傳送電子郵件到您指定的地址。當您 (或電子郵件的收件人) 按一下電子郵件中的連結後，該地址即經過驗證。

若要將電子郵件地址新增至 Amazon SES 帳戶，請使用 [VerifyEmailIdentity](https://docs.aws.amazon.com/ses/latest/APIReference/API_VerifyEmailIdentity.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$email = 'email_address';

try {
    $result = $SesClient->verifyEmailIdentity([
        'EmailAddress' => $email,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 驗證電子郵件網域
<a name="verify-an-email-domain"></a>

Amazon SES 只能從已驗證的電子郵件地址或網域傳送電子郵件。透過驗證網域，可以證明您是該網域的擁有者。驗證網域時，您允許 Amazon SES 從該網域上的任何地址傳送電子郵件。

當您執行下列程式碼範例時，Amazon SES 會為您提供驗證字符。您必須將此字符新增至您網域的 DNS 組態。如需詳細資訊，請參閱《[Amazon Simple Email Service 開發人員指南》中的使用 Amazon SES 驗證網域](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-domain-procedure.html)。

若要將傳送網域新增至 Amazon SES 帳戶，請使用 [VerifyDomainIdentity](https://docs.aws.amazon.com/ses/latest/APIReference/API_VerifyDomainIdentity.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$domain = 'domain.name';

try {
    $result = $SesClient->verifyDomainIdentity([
        'Domain' => $domain,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 列出電子郵件地址
<a name="list-email-addresses"></a>

若要擷取目前區域中提交的電子郵件地址清單 AWS ，無論驗證狀態為何，請使用 [ListIdentities](https://docs.aws.amazon.com/ses/latest/APIReference/API_ListIdentities.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

try {
    $result = $SesClient->listIdentities([
        'IdentityType' => 'EmailAddress',
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 列出電子郵件網域
<a name="list-email-domains"></a>

若要擷取目前區域中提交的電子郵件網域清單 AWS ，無論驗證狀態為何，請使用 [ListIdentities](https://docs.aws.amazon.com/ses/latest/APIReference/API_ListIdentities.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

try {
    $result = $SesClient->listIdentities([
        'IdentityType' => 'Domain',
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 刪除電子郵件地址
<a name="delete-an-email-address"></a>

若要從身分清單中刪除已驗證的電子郵件地址，請使用 [DeleteIdentity](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteIdentity.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$email = 'email_address';

try {
    $result = $SesClient->deleteIdentity([
        'Identity' => $email,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 刪除電子郵件網域
<a name="delete-an-email-domain"></a>

若要從已驗證的身分清單中刪除已驗證的電子郵件網域，請使用 [DeleteIdentity](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteIdentity.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$domain = 'domain.name';

try {
    $result = $SesClient->deleteIdentity([
        'Identity' => $domain,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

# 使用 Amazon SES API 和 第 3 適用於 PHP 的 AWS SDK 版建立自訂電子郵件範本
<a name="ses-template"></a>

Amazon Simple Email Service (Amazon SES) 可讓您使用 範本傳送為每個收件人個人化的電子郵件。範本包含主旨行以及電子郵件內文的文字和 HTML 部分。主旨和內文區段可能還包含專為每位收件人個人化的獨特值。

如需詳細資訊，請參閱《[Amazon Simple Email Service 開發人員指南》中的使用 Amazon SES 傳送個人化](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html)電子郵件。

下列範例示範如何：
+ 使用 [CreateTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#createtemplate) 建立電子郵件範本。
+ 使用 [ListTemplates](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listtemplates) 列出所有電子郵件範本。
+ 使用 [GetTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#gettemplate) 擷取電子郵件範本。
+ 使用 [UpdateTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#updateTemplate) 更新電子郵件範本。
+ 使用 [DeleteTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deletetemplate) 移除電子郵件範本。
+ 使用 [SendTemplatedEmail](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#sendtemplatedemail) 傳送範本電子郵件。

您可以在 GitHub 上 適用於 PHP 的 AWS SDK 取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

如需使用 Amazon SES 的詳細資訊，請參閱《[Amazon SES 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

## 建立電子郵件範本
<a name="create-an-email-template"></a>

若要建立範本以便傳送個人化的電子郵件訊息，請使用 [CreateTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateTemplate.html) 操作。範本可供任何有權在新增範本的 AWS 區域中傳送訊息的帳戶使用。

**注意**  
Amazon SES 不會驗證您的 HTML，因此請在傳送電子郵件之前確定 *HtmlPart* 有效。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$name = 'Template_Name';
$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>';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was send with Amazon SES using the AWS SDK for PHP.';

try {
    $result = $SesClient->createTemplate([
        'Template' => [
            'HtmlPart' => $html_body,
            'SubjectPart' => $subject,
            'TemplateName' => $name,
            'TextPart' => $plaintext_body,
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 取得電子郵件範本
<a name="get-an-email-template"></a>

若要檢視現有電子郵件範本的內容，包括主旨行、HTML 內文和純文字，請使用 [GetTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_GetTemplate.html) 操作。僅需提供 TemplateName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$name = 'Template_Name';

try {
    $result = $SesClient->getTemplate([
        'TemplateName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 列出所有電子郵件範本
<a name="list-all-email-templates"></a>

若要擷取 AWS 帳戶 目前 AWS 區域中與 相關聯的所有電子郵件範本清單，請使用 [ListTemplates](https://docs.aws.amazon.com/ses/latest/APIReference/API_ListTemplates.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

try {
    $result = $SesClient->listTemplates([
        'MaxItems' => 10,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 更新電子郵件範本
<a name="update-an-email-template"></a>

若要變更特定電子郵件範本的內容，包括主旨行、HTML 內文和純文字，請使用 [UpdateTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_UpdadteTemplate.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$name = 'Template_Name';
$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>';
$subject = 'Amazon SES test (AWS SDK for PHP)';
$plaintext_body = 'This email was send with Amazon SES using the AWS SDK for PHP.';

try {
    $result = $SesClient->updateTemplate([
        'Template' => [
            'HtmlPart' => $html_body,
            'SubjectPart' => $subject,
            'TemplateName' => $name,
            'TextPart' => $plaintext_body,
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 刪除電子郵件範本
<a name="delete-an-email-template"></a>

若要移除特定的電子郵件範本，請使用 [DeleteTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteTemplate.html) 操作。您只需要提供 TemplateName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$name = 'Template_Name';

try {
    $result = $SesClient->deleteTemplate([
        'TemplateName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 使用範本傳送電子郵件
<a name="send-an-email-with-a-template"></a>

若要使用範本將電子郵件傳送給收件人，請使用 [SendTemplatedEmail](https://docs.aws.amazon.com/ses/latest/APIReference/API_SendTemplatedEmail.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$template_name = 'Template_Name';
$sender_email = 'email_address';
$recipient_emails = ['email_address'];

try {
    $result = $SesClient->sendTemplatedEmail([
        'Destination' => [
            'ToAddresses' => $recipient_emails,
        ],
        'ReplyToAddresses' => [$sender_email],
        'Source' => $sender_email,

        'Template' => $template_name,
        'TemplateData' => '{ }'
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

# 使用 Amazon SES API 和第 3 適用於 PHP 的 AWS SDK 版管理電子郵件篩選條件
<a name="ses-filters"></a>

除了傳送電子郵件之外，您也可以透過 Amazon Simple Email Service (Amazon SES接收電子郵件。IP 地址篩選條件讓您可自由指定是否接受或拒絕來自某個 IP 地址或某範圍 IP 地址的郵件。如需詳細資訊，請參閱[管理 Amazon SES 電子郵件接收的 IP 地址篩選條件](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-managing-ip-filters.html)。

下列範例示範如何：
+ 使用 [CreateReceiptFilter](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#createreceiptfilter) 建立電子郵件篩選條件。
+ 使用 [ListReceiptFilters](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listreceiptfilters) 列出所有電子郵件篩選條件。
+ 使用 [DeleteReceiptFilter](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deletereceiptfilter) 刪除電子郵件篩選條件。

您可以在 GitHub 上 適用於 PHP 的 AWS SDK 取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

如需使用 Amazon SES 的詳細資訊，請參閱《[Amazon SES 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

## 建立電子郵件篩選條件
<a name="create-an-email-filter"></a>

若要允許或封鎖來自特定 IP 地址的電子郵件，請使用 [CreateReceiptFilter](https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateReceiptFilter.html) 操作。提供 IP 地址或地址範圍，以及此篩選條件的唯一識別名稱。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$filter_name = 'FilterName';
$ip_address_range = '10.0.0.1/24';

try {
    $result = $SesClient->createReceiptFilter([
        'Filter' => [
            'IpFilter' => [
                'Cidr' => $ip_address_range,
                'Policy' => 'Block|Allow',
            ],
            'Name' => $filter_name,
        ],
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 列出所有電子郵件篩選條件
<a name="list-all-email-filters"></a>

若要列出目前 AWS 區域中與 相關聯的 IP AWS 帳戶 地址篩選條件，請使用 [ListReceiptFilters](https://docs.aws.amazon.com/ses/latest/APIReference/API_ListReceiptFilters.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

try {
    $result = $SesClient->listReceiptFilters();
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 刪除電子郵件篩選條件
<a name="delete-an-email-filter"></a>

若要移除特定 IP 地址現有的篩選條件，請使用 [DeleteReceiptFilter](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteReceiptFilter.html) 操作。提供獨一無二的篩選條件名稱以識別欲刪除的收件篩選條件。

如果您需要變更所篩選的地址範圍，則可刪除收件篩選條件後再建立新的篩選條件。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$filter_name = 'FilterName';

try {
    $result = $SesClient->deleteReceiptFilter([
        'FilterName' => $filter_name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

# 使用 Amazon SES API 和第 3 適用於 PHP 的 AWS SDK 版建立和管理電子郵件規則
<a name="ses-rules"></a>

除了傳送電子郵件之外，您也可以透過 Amazon Simple Email Service (Amazon SES接收電子郵件。接收規則可讓您指定 Amazon SES 對您擁有的電子郵件地址或網域所接收的電子郵件執行的操作。規則可以傳送電子郵件到其他 AWS 服務，包括但不限於 Amazon S3、Amazon SNS 或 AWS Lambda。

如需詳細資訊，請參閱[管理 Amazon SES 電子郵件接收的接收規則集](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-managing-receipt-rule-sets.html)[和管理 Amazon SES 電子郵件接收的接收規則](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-managing-receipt-rules.html)。

下列範例示範如何：
+ 使用 [CreateReceiptRuleSet](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#createreceiptruleset) 建立接收規則集。
+ 使用 [CreateReceiptRule](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#createreceiptrule) 建立接收規則。
+ 使用 [DescribeReceiptRuleSet](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#describereceiptruleset) 描述接收規則集。
+ 使用 [DescribeReceiptRule](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#describereceiptrule) 描述接收規則。
+ 使用 [ListReceiptRuleSets](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listreceiptrulesets) 列出所有接收規則集。
+ 使用 [UpdateReceiptRule](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#updatereceiptrule) 更新接收規則。
+ 使用 [DeleteReceiptRule](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deletereceiptrule) 移除接收規則。
+ 使用 [DeleteReceiptRuleSet](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deletereceiptruleset) 移除接收規則集。

您可以在 GitHub 上 適用於 PHP 的 AWS SDK 取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

如需使用 Amazon SES 的詳細資訊，請參閱《[Amazon SES 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

## 建立接收規則集
<a name="create-a-receipt-rule-set"></a>

接收規則集包含一組接收規則。您的帳戶必須至少有一個關聯的接收規則集，您才能建立接收規則。若要建立接收規則集，請使用 [CreateReceiptRuleSet](https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateReceiptRuleSet.html) 操作並提供獨一無二的 RuleSetName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$name = 'Rule_Set_Name';

try {
    $result = $SesClient->createReceiptRuleSet([
        'RuleSetName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 建立接收規則
<a name="create-a-receipt-rule"></a>

透過為現有的接收規則集新增接收規則，控制您的內送電子郵件。此範例說明如何建立接收規則，將傳入的訊息傳送至 Amazon S3 儲存貯體，但您也可以傳送訊息至 Amazon SNS 和 AWS Lambda。若要建立接收規則，請使用 [CreateReceiptRule](https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateReceiptRule.html) 操作並提供規則的名稱和 RuleSetName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$rule_name = 'Rule_Name';
$rule_set_name = 'Rule_Set_Name';
$s3_bucket = 'Bucket_Name';

try {
    $result = $SesClient->createReceiptRule([
        'Rule' => [
            'Actions' => [
                [
                    'S3Action' => [
                        'BucketName' => $s3_bucket,
                    ],
                ],
            ],
            'Name' => $rule_name,
            'ScanEnabled' => true,
            'TlsPolicy' => 'Optional',
            'Recipients' => ['<string>']
        ],
        'RuleSetName' =>  $rule_set_name,

     ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 描述接收規則集
<a name="describe-a-receipt-rule-set"></a>

每秒傳回一次指定的接收規則集的詳細資訊。若要使用 [DescribeReceiptRuleSet](https://docs.aws.amazon.com/ses/latest/APIReference/API_DescribeReceiptRuleSet.html) 操作，請提供 RuleSetName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$name = 'Rule_Set_Name';

try {
    $result = $SesClient->describeReceiptRuleSet([
        'RuleSetName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 描述接收規則
<a name="describe-a-receipt-rule"></a>

傳回指定的接收規則的詳細資訊。若要使用 [DescribeReceiptRule](https://docs.aws.amazon.com/ses/latest/APIReference/API_DescribeReceiptRule.html) 操作，請提供 RuleName 和 RuleSetName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$rule_name = 'Rule_Name';
$rule_set_name = 'Rule_Set_Name';

try {
    $result = $SesClient->describeReceiptRule([
        'RuleName' => $rule_name,
        'RuleSetName' => $rule_set_name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 列出所有接收規則集
<a name="list-all-receipt-rule-sets"></a>

若要列出 AWS 帳戶 目前 AWS 區域中 下存在的接收規則集，請使用 [ListReceiptRuleSets](https://docs.aws.amazon.com/ses/latest/APIReference/API_ListReceiptRuleSets.html) 操作。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

try {
    $result = $SesClient->listReceiptRuleSets();
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 更新接收規則
<a name="update-a-receipt-rule"></a>

此範例說明如何更新將傳入訊息傳送至 AWS Lambda 函數的接收規則，但您也可以傳送訊息至 Amazon SNS 和 Amazon S3。若要使用 [UpdateReceiptRule](https://docs.aws.amazon.com/ses/latest/APIReference/API_UpdateReceiptRule.html) 操作，請提供新的接收規則名稱和 RuleSetName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$rule_name = 'Rule_Name';
$rule_set_name = 'Rule_Set_Name';
$lambda_arn = 'Amazon Resource Name (ARN) of the AWS Lambda function';
$sns_topic_arn = 'Amazon Resource Name (ARN) of the Amazon SNS topic';

try {
    $result = $SesClient->updateReceiptRule([
        'Rule' => [
            'Actions' => [
                'LambdaAction' => [
                    'FunctionArn' => $lambda_arn,
                    'TopicArn' => $sns_topic_arn,
                ],
            ],
            'Enabled' => true,
            'Name' => $rule_name,
            'ScanEnabled' => false,
            'TlsPolicy' => 'Require',
        ],
        'RuleSetName' => $rule_set_name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 刪除接收規則集
<a name="delete-a-receipt-rule-set"></a>

移除所指定而目前未停用的接收規則集。如此亦將刪除其包含的所有接收規則。若要刪除接收規則集，請使用 [DeleteReceiptRuleSet](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteReceiptRuleSet.html) 操作並提供 RuleSetName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$name = 'Rule_Set_Name';

try {
    $result = $SesClient->deleteReceiptRuleSet([
        'RuleSetName' => $name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 刪除接收規則
<a name="delete-a-receipt-rule"></a>

若要刪除指定的接收規則，請使用 [DeleteReceiptRule](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteReceiptRule.html) 操作並提供 RuleName 和 RuleSetName。

 **匯入** 

```
require 'vendor/autoload.php';

use Aws\Exception\AwsException;
```

 **範例程式碼** 

```
$SesClient = new Aws\Ses\SesClient([
    'profile' => 'default',
    'version' => '2010-12-01',
    'region' => 'us-east-2'
]);

$rule_name = 'Rule_Name';
$rule_set_name = 'Rule_Set_Name';

try {
    $result = $SesClient->deleteReceiptRule([
        'RuleName' => $rule_name,
        'RuleSetName' => $rule_set_name,
    ]);
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

# 使用 Amazon SES API 和 第 3 適用於 PHP 的 AWS SDK 版監控您的傳送活動
<a name="ses-send-email"></a>

Amazon Simple Email Service (Amazon SES) 提供監控傳送活動的方法。我們建議您實作這些方法，以持續追蹤重要指標，例如帳戶的退信、抱怨與拒收率等。過高的退信率和投訴率可能會影響您使用 Amazon SES 傳送電子郵件的能力。

下列範例示範如何：
+ 使用 [GetSendQuota](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#getsendquota) 檢查您的傳送份額。
+ 使用 [GetSendStatistics](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#getsendstatistics) 監控您的傳送活動。

您可以在 GitHub 上 適用於 PHP 的 AWS SDK 取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

如需使用 Amazon SES 的詳細資訊，請參閱《[Amazon SES 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

## 檢查您的傳送配額
<a name="check-your-sending-quota"></a>

您在 24 小時期間內所能傳送的訊息數目有特定限制。若要檢查您尚餘幾則訊息可以傳送，請使用 [GetSendQuota](https://docs.aws.amazon.com/ses/latest/APIReference/API_GetSendQuota.html) 操作。如需詳細資訊，請參閱[管理您的 Amazon SES 傳送限制](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/manage-sending-limits.html)。

 **匯入** 

```
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'

]);

try {
    $result = $SesClient->getSendQuota();
    $send_limit = $result["Max24HourSend"];
    $sent = $result["SentLast24Hours"];
    $available = $send_limit - $sent;
    print("<p>You can send " . $available . " more messages in the next 24 hours.</p>");
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

## 監控您的傳送活動
<a name="monitor-your-sending-activity"></a>

若要擷取您在過去兩週已傳送的訊息各項指標，請使用 [GetSendStatistics](https://docs.aws.amazon.com/ses/latest/APIReference/API_GetSendStatistics.html) 操作。此範例將以 15 分鐘為增量，傳回嘗試交付、退信、投訴與拒收的訊息數目。

 **匯入** 

```
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'
]);

try {
    $result = $SesClient->getSendStatistics();
    var_dump($result);
} catch (AwsException $e) {
    // output error message if fails
    echo $e->getMessage();
    echo "\n";
}
```

# 使用 Amazon SES API 和第 3 適用於 PHP 的 AWS SDK 版授權寄件者
<a name="ses-sender-policy"></a>

若要讓其他 AWS 帳戶、 AWS Identity and Access Management 使用者 AWS 或服務代表您透過 Amazon Simple Email Service (Amazon SES) 傳送電子郵件，您可以建立傳送授權政策。此為附加至您自有身分的 JSON 文件。

政策將明確列出您允許誰代表該身分傳送，以及有哪些傳送條件。除了您本人以及政策中明確授予許可的實體，其餘所有寄件者皆不得傳送電子郵件。一個身分可以沒有政策、有一個政策或有多個政策。您也可以使用含有多個陳述式的單一政策來達成多個政策的效果。

如需詳細資訊，請參閱[透過 Amazon SES 使用傳送授權](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/sending-authorization.html)。

下列範例示範如何：
+ 使用 [PutIdentityPolicy](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#createidentitypolicy) 建立已獲授權的寄件者。
+ 使用 [GetIdentityPolicies](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#getidentitypolicies) 擷取已獲授權寄件者的政策。
+ 使用 [ListIdentityPolicies](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listidentitypolicies) 列出已獲授權的寄件者。
+ 使用 [DeleteIdentityPolicy](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deleteidentitypolicy) 對已獲授權的寄件者撤銷許可。

您可以在 GitHub 適用於 PHP 的 AWS SDK 上取得 的所有範例程式碼。 [ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code)

## 登入資料
<a name="examplecredentials"></a>

在執行範例程式碼之前，請先設定您的 AWS 登入資料，如中所述[AWS 使用第 3 適用於 PHP 的 AWS SDK 版向 驗證](credentials.md)。然後匯入 適用於 PHP 的 AWS SDK，如 中所述[安裝第 3 適用於 PHP 的 AWS SDK 版](getting-started_installation.md)。

如需使用 Amazon SES 的詳細資訊，請參閱《[Amazon SES 開發人員指南](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)》。

## 建立授權寄件者
<a name="create-an-authorized-sender"></a>

若要授權其他人代表您 AWS 帳戶 傳送電子郵件，請使用身分政策來新增或更新授權，以從已驗證的電子郵件地址或網域傳送電子郵件。若要建立身分政策，請使用 [PutIdentityPolicy](https://docs.aws.amazon.com/ses/latest/APIReference/API_PutIdentityPolicy.html) 操作。

 **匯入** 

```
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";
}
```

## 擷取授權寄件者的政策
<a name="retrieve-polices-for-an-authorized-sender"></a>

傳回與特定的電子郵件身分或網域身分相關聯的傳送授權政策。若要取得指定的電子郵件地址或網域的傳送授權，請使用 [GetIdentityPolicy](https://docs.aws.amazon.com/ses/latest/APIReference/API_GetIdentityPolicy.html) 操作。

 **匯入** 

```
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";
}
```

## 列出授權寄件者
<a name="list-authorized-senders"></a>

若要列出與目前 AWS 區域中的特定電子郵件身分或網域身分相關聯的傳送授權政策，請使用 [ListIdentityPolicies](https://docs.aws.amazon.com/ses/latest/APIReference/API_ListIdentityPolicies.html) 操作。

 **匯入** 

```
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";
}
```

## 撤銷授權寄件者的許可
<a name="revoke-permission-for-an-authorized-sender"></a>

使用 [DeleteIdentityPolicy](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteIdentityPolicy.html) 操作刪除相關聯的身分政策，移除另一個 傳送授權 AWS 帳戶 ，以使用電子郵件身分或網域身分傳送電子郵件。

 **匯入** 

```
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";
}
```