

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Amazon SES API と AWS SDK for PHP バージョン 3 を使用したカスタムメールテンプレートの作成
<a name="ses-template"></a>

Amazon Simple Email Service (Amazon SES) では、テンプレートを使用して受取人ごとにパーソナライズされた E メールを送信できます。テンプレートには、件名、E メール本文のテキストパートと HTML パートが含まれています。件名および本文セクションには、受取人ごとにパーソナライズされたユニークな値を含めることもできます。

詳細については、「Amazon Simple Email Service デベロッパーガイド」の「[Amazon SES を使用してパーソナライズされた E メールを送信する](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) を使用して E メールテンプレートを作成する。
+ [ListTemplates](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listtemplates) を使用してすべての E メールテンプレートをリストする。
+ [GetTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#gettemplate) を使用して E メールテンプレートを取得する。
+ [UpdateTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#updateTemplate) を使用して E メールテンプレートを更新する。
+ [DeleteTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deletetemplate) を使用して E メールテンプレートを削除する。
+ [SendTemplatedEmail](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#sendtemplatedemail) を使用してテンプレート E メールを送信する。

AWS SDK for PHP 用のすべてのサンプルコードは [GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code) で入手できます。

## 認証情報
<a name="examplecredentials"></a>

サンプルコードを実行する前に、AWS の認証情報を設定します ([AWS SDK for PHP バージョン 3 AWS を使用した での認証](credentials.md) を参照)。AWS SDK for PHP からのインポート ([AWS SDK for PHP バージョン 3 のインストール](getting-started_installation.md) を参照)。

Amazon SES の使用の詳細については、「[Amazon SES デベロッパーガイド](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/)」を参照してください。

## E メールテンプレートを作成する
<a name="create-an-email-template"></a>

パーソナライズされた E メールメッセージを送信するためのテンプレートを作成するには、[CreateTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateTemplate.html) オペレーションを使用します。このテンプレートは、テンプレートが追加された AWS リージョンでのメッセージの送信が承認されたどのアカウントでも使用できます。

**注記**  
Amazon SES は HTML を検証しないため、E メールを送信する前に必ず *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";
}
```

## E メールテンプレートを取得する
<a name="get-an-email-template"></a>

件名、HTML 本文、プレーンテキストを含む既存の E メールテンプレートのコンテンツを表示するには、[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";
}
```

## すべての E メールテンプレートをリストする
<a name="list-all-email-templates"></a>

現在の AWS リージョンにあるお客様の AWS アカウント に関連付けられているすべての E メールテンプレートのリストを取得するには、[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";
}
```

## E メールテンプレートを更新する
<a name="update-an-email-template"></a>

件名、HTML 本文、プレーンテキストを含む特定の E メールテンプレートのコンテンツを変更するには、[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";
}
```

## E メールテンプレートを削除する
<a name="delete-an-email-template"></a>

特定の E メールテンプレートを削除するには、[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";
}
```

## テンプレートを使用して E メールを送信する
<a name="send-an-email-with-a-template"></a>

テンプレートを使用して E メールを受取人に送信するには、[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";
}
```