

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Membuat template email khusus menggunakan Amazon SES API dan AWS SDK untuk PHP Versi 3
<a name="ses-template"></a>

Amazon Simple Email Service (Amazon SES) memungkinkan Anda mengirim email yang dipersonalisasi untuk setiap penerima dengan menggunakan templat. Template mencakup baris subjek dan teks dan bagian HTML dari badan email. Bagian subjek dan tubuh juga dapat berisi nilai unik yang dipersonalisasi untuk setiap penerima.

Untuk informasi selengkapnya, lihat [Mengirim Email yang Dipersonalisasi Menggunakan Amazon SES](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html) di Panduan Pengembang Layanan Email Sederhana Amazon.

Contoh berikut menunjukkan cara:
+ Buat template email menggunakan [CreateTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#createtemplate).
+ Daftar semua template email menggunakan [ListTemplates](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#listtemplates).
+ Ambil template email menggunakan [GetTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#gettemplate).
+ Perbarui template email menggunakan [UpdateTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#updateTemplate).
+ Hapus template email menggunakan [DeleteTemplate](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#deletetemplate).
+ Kirim email template menggunakan [SendTemplatedEmail](https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-email-2010-12-01.html#sendtemplatedemail).

Semua kode contoh untuk AWS SDK untuk PHP tersedia [di sini GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/php/example_code).

## Kredensial
<a name="examplecredentials"></a>

Sebelum menjalankan kode contoh, konfigurasikan AWS kredenal Anda, seperti yang dijelaskan dalam. [Mengautentikasi dengan AWS menggunakan AWS SDK untuk PHP Versi 3](credentials.md) Kemudian impor AWS SDK untuk PHP, seperti yang dijelaskan dalam[Menginstal AWS SDK untuk PHP Versi 3](getting-started_installation.md).

Untuk informasi selengkapnya tentang penggunaan Amazon SES, lihat [Panduan Pengembang Amazon SES](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/).

## Buat template email
<a name="create-an-email-template"></a>

Untuk membuat template untuk mengirim pesan email yang dipersonalisasi, gunakan [CreateTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateTemplate.html)operasi. Template dapat digunakan oleh akun apa pun yang berwenang untuk mengirim pesan di AWS Wilayah tempat templat ditambahkan.

**catatan**  
Amazon SES tidak memvalidasi HTML Anda, jadi pastikan *HtmlPart*itu valid sebelum mengirim email.

 **Impor** 

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

use Aws\Exception\AwsException;
```

 **Kode Sampel** 

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

## Dapatkan template email
<a name="get-an-email-template"></a>

Untuk melihat konten untuk template email yang ada termasuk baris subjek, isi HTML, dan teks biasa, gunakan [GetTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_GetTemplate.html)operasi. Hanya TemplateName diperlukan.

 **Impor** 

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

use Aws\Exception\AwsException;
```

 **Kode Sampel** 

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

## Daftar semua template email
<a name="list-all-email-templates"></a>

Untuk mengambil daftar semua template email yang terkait dengan Anda Akun AWS di AWS Wilayah saat ini, gunakan [ListTemplates](https://docs.aws.amazon.com/ses/latest/APIReference/API_ListTemplates.html)operasi.

 **Impor** 

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

use Aws\Exception\AwsException;
```

 **Kode Sampel** 

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

## Perbarui template email
<a name="update-an-email-template"></a>

Untuk mengubah konten untuk template email tertentu termasuk baris subjek, isi HTML, dan teks biasa, gunakan [UpdateTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_UpdadteTemplate.html)operasi.

 **Impor** 

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

use Aws\Exception\AwsException;
```

 **Kode Sampel** 

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

## Hapus template email
<a name="delete-an-email-template"></a>

Untuk menghapus template email tertentu, gunakan [DeleteTemplate](https://docs.aws.amazon.com/ses/latest/APIReference/API_DeleteTemplate.html)operasi. Yang Anda butuhkan hanyalah TemplateName.

 **Impor** 

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

use Aws\Exception\AwsException;
```

 **Kode Sampel** 

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

## Kirim email dengan template
<a name="send-an-email-with-a-template"></a>

Untuk menggunakan templat untuk mengirim email ke penerima, gunakan [SendTemplatedEmail](https://docs.aws.amazon.com/ses/latest/APIReference/API_SendTemplatedEmail.html)operasi.

 **Impor** 

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

use Aws\Exception\AwsException;
```

 **Kode Sampel** 

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