

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

# 使用 的 Amazon SES 範例 AWS CLI
<a name="cli_ses_code_examples"></a>

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 Amazon SES 執行動作和實作常見案例。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

每個範例均包含完整原始碼的連結，您可在連結中找到如何設定和執行內容中程式碼的相關指示。

**Topics**
+ [動作](#actions)

## 動作
<a name="actions"></a>

### `delete-identity`
<a name="ses_DeleteIdentity_cli_topic"></a>

以下程式碼範例顯示如何使用 `delete-identity`。

**AWS CLI**  
**刪除身分**  
下列範例使用 `delete-identity` 命令從透過 Amazon SES 驗證的身分清單中刪除身分：  

```
aws ses delete-identity --identity user@example.com
```
如需驗證的身分詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「在 Amazon SES 中驗證電子郵件地址和網域」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteIdentity](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/delete-identity.html)。

### `get-identity-dkim-attributes`
<a name="ses_GetIdentityDkimAttributes_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-identity-dkim-attributes`。

**AWS CLI**  
**取得身分清單的 Amazon SES Easy DKIM 屬性**  
以下範例使用 `get-identity-dkim-attributes` 命令來擷取身分清單的 Amazon SES Easy DKIM 屬性：  

```
aws ses get-identity-dkim-attributes --identities "example.com" "user@example.com"
```
輸出：  

```
{
   "DkimAttributes": {
       "example.com": {
           "DkimTokens": [
               "EXAMPLEjcs5xoyqytjsotsijas7236gr",
               "EXAMPLEjr76cvoc6mysspnioorxsn6ep",
               "EXAMPLEkbmkqkhlm2lyz77ppkulerm4k"
           ],
           "DkimEnabled": true,
           "DkimVerificationStatus": "Success"
       },
       "user@example.com": {
           "DkimEnabled": false,
           "DkimVerificationStatus": "NotStarted"
       }
   }
}
```
如果您使用從未提交進行驗證的身分來呼叫此命令，則該身分不會出現在輸出中。  
如需 Easy DKIM 的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「Amazon SES 中的 Easy DKIM」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetIdentityDkimAttributes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-identity-dkim-attributes.html)。

### `get-identity-notification-attributes`
<a name="ses_GetIdentityNotificationAttributes_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-identity-notification-attributes`。

**AWS CLI**  
**取得身分清單的 Amazon SES 通知屬性**  
以下範例使用 `get-identity-notification-attributes` 命令來擷取身分清單的 Amazon SES 屬性：  

```
aws ses get-identity-notification-attributes --identities "user1@example.com" "user2@example.com"
```
輸出：  

```
{
   "NotificationAttributes": {
       "user1@example.com": {
           "ForwardingEnabled": false,
           "ComplaintTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic",
           "BounceTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic",
           "DeliveryTopic": "arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic"
       },
       "user2@example.com": {
           "ForwardingEnabled": true
       }
   }
}
```
此命令會傳回電子郵件意見轉寄的狀態，在適用的狀況下，也會傳回退信、投訴和交付通知傳往的 Amazon SNS 主題其 Amazon Resource Name (ARN)。  
如果您使用從未提交進行驗證的身分來呼叫此命令，則該身分不會出現在輸出中。  
如需通知的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「搭配使用通知和 Amazon SES」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetIdentityNotificationAttributes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-identity-notification-attributes.html)。

### `get-identity-verification-attributes`
<a name="ses_GetIdentityVerificationAttributes_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-identity-verification-attributes`。

**AWS CLI**  
**取得身分清單的 Amazon SES 驗證狀態**  
下列範例使用 `get-identity-verification-attributes` 命令來擷取身分清單的 Amazon SES 驗證狀態：  

```
aws ses get-identity-verification-attributes --identities "user1@example.com" "user2@example.com"
```
輸出：  

```
{
   "VerificationAttributes": {
       "user1@example.com": {
           "VerificationStatus": "Success"
       },
       "user2@example.com": {
           "VerificationStatus": "Pending"
       }
   }
}
```
如果您使用從未提交進行驗證的身分來呼叫此命令，則該身分不會出現在輸出中。  
如需驗證的身分詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「在 Amazon SES 中驗證電子郵件地址和網域」。  
+  如需 API 詳細資訊，請參閱《*AWS CLI API 參考*》中的 [GetIdentityVerificationAttributes](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-identity-verification-attributes.html)。

### `get-send-quota`
<a name="ses_GetSendQuota_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-send-quota`。

**AWS CLI**  
**取得您的 Amazon SES 傳送限制**  
下列範例使用 `get-send-quota` 命令來傳回 Amazon SES 傳送限制：  

```
aws ses get-send-quota
```
輸出：  

```
{
   "Max24HourSend": 200.0,
   "SentLast24Hours": 1.0,
   "MaxSendRate": 1.0
}
```
Max24HourSend 是您的傳送配額，也就是您在 24 小時期間內可傳送的電子郵件數量上限。傳送配額所反映的是一段時間內的配額。每當您嘗試傳送電子郵件時，Amazon SES 會檢查您在過去的 24 小時內傳送的電子郵件數量。只要您已傳送的電子郵件總數量低於您的配額，您的傳送請求將被接受並將傳送您的電子郵件。  
SentLast24Hours 是您在過去 24 小時內傳送的電子郵件數量。  
MaxSendRate 是您每秒可傳送的電子郵件數量上限。  
請注意，傳送限制依據收件人而定，而非訊息。例如，一封電子郵件中有 10 個收件人，就會佔用 10 個您的傳送配額。  
如需詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「管理您的 Amazon SES 傳送限制」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetSendQuota](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-send-quota.html)。

### `get-send-statistics`
<a name="ses_GetSendStatistics_cli_topic"></a>

以下程式碼範例顯示如何使用 `get-send-statistics`。

**AWS CLI**  
**取得您的 Amazon SES 傳送統計資料**  
以下範例使用 `get-send-statistics` 命令來傳回 Amazon SES 傳送統計資料  

```
aws ses get-send-statistics
```
輸出：  

```
{
   "SendDataPoints": [
       {
           "Complaints": 0,
           "Timestamp": "2013-06-12T19:32:00Z",
           "DeliveryAttempts": 2,
           "Bounces": 0,
           "Rejects": 0
       },
       {
           "Complaints": 0,
           "Timestamp": "2013-06-12T00:47:00Z",
           "DeliveryAttempts": 1,
           "Bounces": 0,
           "Rejects": 0
       }
   ]
}
```
結果是一份資料點清單，代表過去兩週的傳送活動。清單中的每個資料點都包含間隔 15 分鐘的統計資料。  
在此範例中，只有兩個資料點，因為只有使用者在過去兩週內傳送的電子郵件落在兩個 15 分鐘間隔內。  
如需詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「監控 Amazon SES 用量統計資料」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetSendStatistics](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/get-send-statistics.html)。

### `list-identities`
<a name="ses_ListIdentities_cli_topic"></a>

以下程式碼範例顯示如何使用 `list-identities`。

**AWS CLI**  
**列出特定 AWS 帳戶的所有身分 （電子郵件地址和網域）**  
下列範例使用 `list-identities` 命令來列出已提交給 Amazon SES 驗證的所有身分：  

```
aws ses list-identities
```
輸出：  

```
{
    "Identities": [
      "user@example.com",
      "example.com"
    ]
}
```
傳回的清單包含所有身分，無論驗證狀態為何 (已驗證、等待驗證、失敗等)。  
在此範例中，因為我們未指定 identity-type 參數，所以會傳回電子郵件地址 * 和 * 網域。  
如需驗證詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「在 Amazon SES 中驗證電子郵件地址和網域」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListIdentities](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/list-identities.html)。

### `send-email`
<a name="ses_SendEmail_cli_topic"></a>

以下程式碼範例顯示如何使用 `send-email`。

**AWS CLI**  
**使用 Amazon SES 傳送格式化電子郵件**  
下列範例使用 `send-email` 命令來傳送格式化電子郵件：  

```
aws ses send-email --from sender@example.com --destination file://destination.json --message file://message.json
```
輸出：  

```
{
   "MessageId": "EXAMPLEf3a5efcd1-51adec81-d2a4-4e3f-9fe2-5d85c1b23783-000000"
}
```
目的地和訊息是在目前目錄中以 .json 檔案形式儲存的 JSON 資料結構。這些檔案如下：  
`destination.json`:  

```
{
  "ToAddresses":  ["recipient1@example.com", "recipient2@example.com"],
  "CcAddresses":  ["recipient3@example.com"],
  "BccAddresses": []
}
```
`message.json`:  

```
{
   "Subject": {
       "Data": "Test email sent using the AWS CLI",
       "Charset": "UTF-8"
   },
   "Body": {
       "Text": {
           "Data": "This is the message body in text format.",
           "Charset": "UTF-8"
       },
       "Html": {
           "Data": "This message body contains HTML formatting. It can, for example, contain links like this one: <a class=\"ulink\" href=\"http://docs.aws.amazon.com/ses/latest/DeveloperGuide\" target=\"_blank\">Amazon SES Developer Guide</a>.",
           "Charset": "UTF-8"
       }
   }
}
```
將寄件者和收件者的電子郵件地址取代為您要使用的地址。請注意，必須透過 Amazon SES 驗證寄件者的電子郵件地址。在您取得 Amazon SES 生產存取權之前，除非收件者是 Amazon SES 信箱模擬器，否則您還必須驗證每個收件者的電子郵件地址。如需驗證詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「在 Amazon SES 中驗證電子郵件地址和網域」。  
輸出中的訊息 ID 表示對 send-email 的呼叫成功。  
如果您沒有收到電子郵件，請檢查您的垃圾郵件匣。  
如需傳送格式化電子郵件的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中「使用 Amazon SES API 傳送格式化電子郵件」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [SendEmail](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/send-email.html)。

### `send-raw-email`
<a name="ses_SendRawEmail_cli_topic"></a>

以下程式碼範例顯示如何使用 `send-raw-email`。

**AWS CLI**  
**使用 Amazon SES 傳送電子郵件原始碼**  
下列範例使用 `send-raw-email` 命令來傳送包含 TXT 附件的電子郵件：  

```
aws ses send-raw-email --raw-message file://message.json
```
輸出：  

```
{
   "MessageId": "EXAMPLEf3f73d99b-c63fb06f-d263-41f8-a0fb-d0dc67d56c07-000000"
}
```
原始訊息是在目前目錄中以名為 `message.json` 的檔案儲存的 JSON 資料結構。其中包含下列各項：  

```
{
   "Data": "From: sender@example.com\nTo: recipient@example.com\nSubject: Test email sent using the AWS CLI (contains an attachment)\nMIME-Version: 1.0\nContent-type: Multipart/Mixed; boundary=\"NextPart\"\n\n--NextPart\nContent-Type: text/plain\n\nThis is the message body.\n\n--NextPart\nContent-Type: text/plain;\nContent-Disposition: attachment; filename=\"attachment.txt\"\n\nThis is the text in the attachment.\n\n--NextPart--"
}
```
如您所見，「資料」是一個長字串，其中包含 MIME 格式的整個原始電子郵件內容，包括名為 attachment.txt 的附件。  
將 sender@example.com 和 recipient@example.com 取代為您要使用的地址。請注意，必須透過 Amazon SES 驗證寄件者的電子郵件地址。在您取得 Amazon SES 生產存取權之前，除非收件者是 Amazon SES 信箱模擬器，否則您還必須驗證收件者的電子郵件地址。如需驗證詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「在 Amazon SES 中驗證電子郵件地址和網域」。  
輸出中的訊息 ID 表示對 send-raw-email 的呼叫成功。  
如果您沒有收到電子郵件，請檢查您的垃圾郵件匣。  
如需傳送電子郵件原始碼的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「使用 Amazon SES API 傳送電子郵件原始碼」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [SendRawEmail](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/send-raw-email.html)。

### `set-identity-dkim-enabled`
<a name="ses_SetIdentityDkimEnabled_cli_topic"></a>

以下程式碼範例顯示如何使用 `set-identity-dkim-enabled`。

**AWS CLI**  
**啟用或停用 Amazon SES 已驗證身分的 Easy DKIM**  
下列範例會使用 `set-identity-dkim-enabled` 命令停用已驗證電子郵件地址的 DKIM：  

```
aws ses set-identity-dkim-enabled --identity user@example.com --no-dkim-enabled
```
如需 Easy DKIM 的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「Amazon SES 中的 Easy DKIM」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [SetIdentityDkimEnabled](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/set-identity-dkim-enabled.html)。

### `set-identity-feedback-forwarding-enabled`
<a name="ses_SetIdentityFeedbackForwardingEnabled_cli_topic"></a>

以下程式碼範例顯示如何使用 `set-identity-feedback-forwarding-enabled`。

**AWS CLI**  
**啟用或停用 Amazon SES 已驗證身分的退信和投訴電子郵件意見轉寄**  
以下範例使用 `set-identity-feedback-forwarding-enabled` 命令來啟用已驗證電子郵件地址，以透過電子郵件接收退信和投訴通知：  

```
aws ses set-identity-feedback-forwarding-enabled --identity user@example.com --forwarding-enabled
```
您必須透過 Amazon SNS 或電子郵件意見轉寄來接收退信與投訴通知，因此只有您為退信與投訴通知皆選擇 Amazon SNS 主題的情況下，才可停用電子郵件意見轉寄。  
如需通知的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「搭配使用通知和 Amazon SES」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [SetIdentityFeedbackForwardingEnabled](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/set-identity-feedback-forwarding-enabled.html)。

### `set-identity-notification-topic`
<a name="ses_SetIdentityNotificationTopic_cli_topic"></a>

以下程式碼範例顯示如何使用 `set-identity-notification-topic`。

**AWS CLI**  
**設定 Amazon SES 會將已驗證身分的退信、投訴和/或交付通知發佈至的 Amazon SNS 主題**  
下列範例會使用 `set-identity-notification-topic` 命令來指定已驗證電子郵件地址將收到退信通知的 Amazon SNS 主題：  

```
aws ses set-identity-notification-topic --identity user@example.com --notification-type Bounce --sns-topic arn:aws:sns:us-east-1:EXAMPLE65304:MyTopic
```
如需通知的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「搭配使用通知和 Amazon SES」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [SetIdentityNotificationTopic](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/set-identity-notification-topic.html)。

### `verify-domain-dkim`
<a name="ses_VerifyDomainDkim_cli_topic"></a>

以下程式碼範例顯示如何使用 `verify-domain-dkim`。

**AWS CLI**  
**為使用 Amazon SES 簽署的 DKIM 產生已驗證網域的 DKIM 權杖**  
下列範例會使用 `verify-domain-dkim` 命令為已使用 Amazon SES 驗證的網域產生 DKIM 權杖：  

```
aws ses verify-domain-dkim --domain example.com
```
輸出：  

```
{
   "DkimTokens": [
       "EXAMPLEq76owjnks3lnluwg65scbemvw",
       "EXAMPLEi3dnsj67hstzaj673klariwx2",
       "EXAMPLEwfbtcukvimehexktmdtaz6naj"
   ]
}
```
若要設定 DKIM，您必須使用傳回的 DKIM 權杖，以使用指向由 Amazon SES 託管之 DKIM 公有金鑰的 CNAME 記錄來更新網域的 DNS 設定。如需詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「Amazon SES 中的 Easy DKIM」。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [VerifyDomainDkim](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/verify-domain-dkim.html)。

### `verify-domain-identity`
<a name="ses_VerifyDomainIdentity_cli_topic"></a>

以下程式碼範例顯示如何使用 `verify-domain-identity`。

**AWS CLI**  
**透過 Amazon SES 驗證網域**  
下列範例會使用 `verify-domain-identity` 命令來驗證網域：  

```
aws ses verify-domain-identity --domain example.com
```
輸出：  

```
{
   "VerificationToken": "eoEmxw+YaYhb3h3iVJHuXMJXqeu1q1/wwmvjuEXAMPLE"
}
```
若要完成網域驗證，您必須將帶有傳回驗證 Token 的 TXT 記錄新增至網域的 DNS 設定。如需詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「在 Amazon SES 中驗證網域」。  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [VerifyDomainIdentity](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/verify-domain-identity.html)。

### `verify-email-identity`
<a name="ses_VerifyEmailIdentity_cli_topic"></a>

以下程式碼範例顯示如何使用 `verify-email-identity`。

**AWS CLI**  
**透過 Amazon SES 驗證電子郵件地址**  
下列範例會使用 `verify-email-identity` 命令來驗證網域：  

```
aws ses verify-email-identity --email-address user@example.com
```
您必須先驗證您要用於傳送電子郵件的寄件地址或網域來證明您擁有該地址或網域，才可使用 Amazon SES 傳送電子郵件。如果您尚未擁有生產存取權，除了由 Amazon SES 信箱模擬器提供的電子郵件地址外，您仍需驗證任何您傳送電子郵件的收件電子郵件地址。  
呼叫 verify-email-identity 之後，電子郵件地址會收到一封驗證電子郵件。使用者必須按一下電子郵件中的連結，以完成驗證程序。  
如需詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》**中的「在 Amazon SES 中驗證電子郵件地址」。  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [VerifyEmailIdentity](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ses/verify-email-identity.html)。