PHP code example of dealnews / aws-ses

1. Go to this page and download the library: Download dealnews/aws-ses library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dealnews / aws-ses example snippets


use Aws\SesV2\SesV2Client;
use DealNews\AwsSes\Mailer;
use DealNews\AwsSes\Message;

$client = new SesV2Client([
    'region'  => 'us-east-1',
    'version' => '2019-09-27',
]);

$mailer = new Mailer($client);

$result = $mailer->send(
    (new Message())
        ->from('[email protected]', 'DealNews')
        ->to('[email protected]')
        ->subject('Your order has shipped')
        ->html('<p>Thanks for your order!</p>')
        ->text('Thanks for your order!')
);

echo $result->getMessageId();

$message = (new Message())
    ->from('[email protected]', 'DealNews')
    ->to('[email protected]')
    ->to('[email protected]', 'Alt Contact')
    ->cc('[email protected]')
    ->bcc('[email protected]')
    ->replyTo('[email protected]')
    ->subject('Your order has shipped')
    ->text('Thanks for your order!');

$mailer->send($message);

$message = (new Message())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Your invoice')
    ->text('See the attached invoice.')
    ->attach($pdf_bytes, 'invoice.pdf', 'application/pdf')
    ->attachFromPath('/tmp/receipt.png'); // filename/content-type auto-detected

$mailer->send($message);

$message = (new Message())
    ->from('[email protected]')
    ->to('[email protected]')
    ->subject('Your order has shipped')
    ->text('Thanks for your order!')
    ->header('X-DealNews-Category', 'transactional')
    ->configurationSetName('transactional-emails');

$mailer->send($message);

use DealNews\AwsSes\Exception\AwsSesExceptionInterface;
use DealNews\AwsSes\Exception\MissingRequiredFieldException;
use DealNews\AwsSes\Exception\SendException;

try {
    $mailer->send($message);
} catch (MissingRequiredFieldException $e) {
    // The message itself was incomplete (no from/recipient/subject/body).
    // AWS was never contacted.
} catch (SendException $e) {
    // AWS rejected or failed to process the request.
    // The underlying AWS SDK exception is available via getPrevious().
    $aws_exception = $e->getPrevious();
} catch (AwsSesExceptionInterface $e) {
    // Catches anything else this library throws (e.g. an invalid
    // email address passed to from()/to()/cc()/bcc()/replyTo()).
}

public function __construct(SesV2Client $ses_client, ?EmailContentBuilder $content_builder = null)
public function send(Message $message): SendResult

public function build(Message $message): array

public function getMessageId(): string
public function getHttpStatusCode(): int
public function getRequestId(): string

use Aws\MockHandler;
use Aws\Result;
use Aws\SesV2\SesV2Client;

$mock_handler = new MockHandler();
$mock_handler->append(new Result(['MessageId' => 'test-message-id']));

$client = new SesV2Client([
    'region'      => 'us-east-1',
    'version'     => '2019-09-27',
    'handler'     => $mock_handler,
    'credentials' => ['key' => 'test', 'secret' => 'test'],
]);

$mailer = new Mailer($client);

$mock_handler->append(function ($command) {
    // $command['Content']['Simple']['Attachments'], etc.
    return new Result(['MessageId' => 'test-message-id']);
});