CodeIgniter Extension

AmazonSesClient
in package

Amazon SES email client wrapper class.

Provides fluent interface for sending emails via AWS Simple Email Service with support for XML-based templates, CC/BCC recipients, and configuration sets.

use \X\Util\AmazonSesClient;

// Explicit credentials (IAM user access key)
$ses = new AmazonSesClient([
  'region' => $_ENV['AMS_SES_REGION'],
  'credentials' => [
    'key' => $_ENV['AMS_SES_ACCESS_KEY'],
    'secret' => $_ENV['AMS_SES_SECRET_KEY'],
  ],
  'configuration' => $_ENV['AMS_SES_CONFIGURATION'],
]);

// IAM role (EC2 instance profile) — credentials resolved by the
// AWS SDK default provider chain when credentials are omitted
$ses = new AmazonSesClient([
  'region' => $_ENV['AMS_SES_REGION'],
  'configuration' => $_ENV['AMS_SES_CONFIGURATION'],
]);

// Cross-account AssumeRole — SES is in a different AWS account
$ses = new AmazonSesClient([
  'region' => 'us-west-2',
  'roleArn' => $_ENV['AWS_SES_ROLE_ARN'],
]);

$ses
  ->from('from@example.com')
  ->to('to@example.com')
  ->messageFromXml('email/sample', ['name' => 'Alex'])
  ->send();

Email body and subject: application/views/email/sample.xml.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mail>
<subject>Test email.</subject>
<message>
Hi {{ name }}.
</message>
</mail>

Table of Contents

Properties

$bcc  : string
BCC email address.
$cc  : string
CC email address.
$charset  : string
Character code of the email. Default is "UTF-8".
$client  : SesClient|null
Cached SES client instance.
$from  : string
Sender's email address.
$fromName  : string
Sender name.
$message  : string
Body.
$options  : array<string|int, mixed>
SES client options.
$subject  : string
Subject.
$to  : string
Destination email address.

Methods

__construct()  : mixed
Initialize AmazonSesClient.
bcc()  : AmazonSesClient
Set BCC recipient address.
cc()  : AmazonSesClient
Set CC recipient address.
charset()  : AmazonSesClient
Set the email character encoding.
from()  : AmazonSesClient
Set the sender address.
message()  : AmazonSesClient
Set the email body text.
messageFromXml()  : AmazonSesClient
Set subject and body from an XML template.
send()  : Result
Send the email via Amazon SES.
subject()  : AmazonSesClient
Set the email subject line.
to()  : AmazonSesClient
Set the recipient address.
client()  : SesClient
Get or create the SES client for this instance.
reset()  : void
Reset all message fields to defaults.

Properties

$charset

Character code of the email. Default is "UTF-8".

private string $charset = 'UTF-8'

$client

Cached SES client instance.

private SesClient|null $client = null

$options

SES client options.

private array<string|int, mixed> $options = null

Methods

__construct()

Initialize AmazonSesClient.

public __construct([array{region?: string, credentials?: array{key: string, secret: string}, roleArn?: string|null, roleSessionName?: string|null, configuration?: string|null, version?: string, debug?: bool} $options = [] ]) : mixed
Parameters
$options : array{region?: string, credentials?: array{key: string, secret: string}, roleArn?: string|null, roleSessionName?: string|null, configuration?: string|null, version?: string, debug?: bool} = []

Configuration options:

  • region: AWS region for service requests.
  • credentials: AWS credentials array with key and secret. Optional. When omitted, credentials are resolved by the AWS SDK default provider chain (e.g. EC2 instance profile / IAM role).
  • roleArn: IAM role ARN for cross-account SES access via STS AssumeRole. When set, temporary credentials are obtained by assuming this role. When empty, falls back to credentials or the default provider chain.
  • roleSessionName: Session name for STS AssumeRole. Default is "ses-sending".
  • configuration: SES configuration set name. Default is null.
  • version: SES API version. Default is "latest".
  • debug: Output SES options to debug log. Default is false.

bcc()

Set BCC recipient address.

public bcc(string|array<string|int, string> $bcc) : AmazonSesClient
Parameters
$bcc : string|array<string|int, string>

BCC email address or array of addresses.

Return values
AmazonSesClient —

Method chaining.

cc()

Set CC recipient address.

public cc(string|array<string|int, string> $cc) : AmazonSesClient
Parameters
$cc : string|array<string|int, string>

CC email address or array of addresses.

Return values
AmazonSesClient —

Method chaining.

charset()

Set the email character encoding.

public charset(string $charset) : AmazonSesClient
Parameters
$charset : string

Character encoding (e.g., "UTF-8", "ISO-2022-JP").

Return values
AmazonSesClient —

Method chaining.

from()

Set the sender address.

public from(string $from[, string|null $fromName = null ]) : AmazonSesClient
Parameters
$from : string

Sender's email address.

$fromName : string|null = null

Display name for the sender.

Return values
AmazonSesClient —

Method chaining.

messageFromXml()

Set subject and body from an XML template.

public messageFromXml(string $xmlPath[, array<string|int, mixed> $params = [] ]) : AmazonSesClient

The XML file should contain <subject> and <message> elements. Twig variables can be used in the template.

Parameters
$xmlPath : string

XML template path relative to application/views/.

$params : array<string|int, mixed> = []

Template variables for interpolation.

Return values
AmazonSesClient —

Method chaining.

send()

Send the email via Amazon SES.

public send() : Result

After sending, all recipient and message fields are reset.

Return values
Result —

SES API response containing MessageId.

to()

Set the recipient address.

public to(string|array<string|int, string> $to) : AmazonSesClient
Parameters
$to : string|array<string|int, string>

Recipient email address or array of addresses.

Return values
AmazonSesClient —

Method chaining.

client()

Get or create the SES client for this instance.

private client() : SesClient

The client is cached per instance, so clients created with different options (region, credentials, roleArn) do not interfere with each other.

Return values
SesClient —

Cached SES client instance.

reset()

Reset all message fields to defaults.

private reset() : void

        
On this page

Search results