1

The official AWS PHP SDK Pinpoint documentation is so dense that even sending a simple email seems like a daunting task :)

https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-2016-12-01.html#sendmessages


    $result = $client->sendMessages([
        'ApplicationId' => '<string>', // REQUIRED
        'MessageRequest' => [ // REQUIRED
            'Addresses' => [
                '<__string>' => [
                    'BodyOverride' => '<string>',
                    'ChannelType' => 'GCM|APNS|APNS_SANDBOX|APNS_VOIP|APNS_VOIP_SANDBOX|ADM|SMS|VOICE|EMAIL|BAIDU|CUSTOM',
                    'Context' => ['<string>', ...],
                    'RawContent' => '<string>',
                    'Substitutions' => [
                        '<__string>' => ['<string>', ...],
                        // ...
                    ],
                    'TitleOverride' => '<string>',
                ],
                // ...
            ],
            'Context' => ['<string>', ...],
            'Endpoints' => [
                '<__string>' => [
                    'BodyOverride' => '<string>',
                    'Context' => ['<string>', ...],
                    'RawContent' => '<string>',
                    'Substitutions' => [
                        '<__string>' => ['<string>', ...],
                        // ...
                    ],
                    'TitleOverride' => '<string>',
                ],
                // ...
            ],
            'MessageConfiguration' => [ // REQUIRED
                'EmailMessage' => [
                    'Body' => '<string>',
                    'FeedbackForwardingAddress' => '<string>',
                    'FromAddress' => '<string>',
                    'RawEmail' => [
                        'Data' => <string || resource || Psr\Http\Message\StreamInterface>,
                    ],
                    'ReplyToAddresses' => ['<string>', ...],
                    'SimpleEmail' => [
                        'HtmlPart' => [
                            'Charset' => '<string>',
                            'Data' => '<string>',
                        ],
                        'Subject' => [
                            'Charset' => '<string>',
                            'Data' => '<string>',
                        ],
                        'TextPart' => [
                            'Charset' => '<string>',
                            'Data' => '<string>',
                        ],
                    ],
                    'Substitutions' => [
                        '<__string>' => ['<string>', ...],
                        // ...
                    ],
                ],
            ],
            'TemplateConfiguration' => [
                'EmailTemplate' => [
                    'Name' => '<string>',
                    'Version' => '<string>',
                ],
                'PushTemplate' => [
                    'Name' => '<string>',
                    'Version' => '<string>',
                ],
                'SMSTemplate' => [
                    'Name' => '<string>',
                    'Version' => '<string>',
                ],
                'VoiceTemplate' => [
                    'Name' => '<string>',
                    'Version' => '<string>',
                ],
            ],
            'TraceId' => '<string>',
        ],
    ]);

Does someone have a working code snippet just for sending a simple email using the PHP SDK v3?

2 Answers2

1

The following php script demonstrates how you can use AWS PHP SDK v3 to send emails using Amazon Pinpoint service using the sendMessage API.

Feel free to fork it from gists and customize to fit your need.

<?php

require 'vendor/autoload.php';

use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;

/**
 * @author syumaK(Amos Syuma)
 * Date: March 24, 2020
 * Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
 *
 */

/**
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
    'profile' => 'syumaK',
    'region'  => 'us-east-1',
    'version'  => 'latest',
));

$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);

# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";

# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";

try {

    $result = $pinpointClient->sendMessages([
        'ApplicationId' => '4fd13xxxxxxxxx', // REQUIRED
        'MessageRequest' => [ // REQUIRED
            'Addresses' => [
                $TOADDRESS => [
                    'ChannelType' => 'EMAIL',
                ],
            ],

            'MessageConfiguration' => [ // REQUIRED
                'EmailMessage' => [
                    'FromAddress' => $SENDER,
                ],
            ],

            'TemplateConfiguration' => [ // REQUIRED
                'EmailTemplate' => [
                    'Name' => 'One',
                    'Version' => '1',
                ],
            ],

        ],
    ]);

    print $result;

    } catch (AwsException $e){

        // output error message if fails
        error_log($e->getMessage());
    }
?>

Tested the above code snippet using the following environment spec:

Ubuntu: 18.04
Apache2: 2.4.18
aws-sdk-php": "^3.108"

Hope this helps!

aksyuma
  • 2,957
  • 1
  • 15
  • 29
  • Thank you for the answer. I coded starting from there and got a working solution. At the beginning I got "Resource not found" error. However, in the end I took out the "TemplateConfiguration" block and added "SimpleEmail" block under "EmailMessage". That way it worked! – antoniotajuelo Mar 24 '20 at 19:39
1

Based on syumaK answer, I finally got it working with this snippet:

<?php

require 'vendor/autoload.php';

use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;

/**
 * @author syumaK(Amos Syuma)
 * Date: March 24, 2020
 * Description: A php script that uses AWS PHP SDK for Pinpoint service to send an email message.
 *
 */

/**
 * This code expects that you have AWS credentials set up per:
 * https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
 */

// Instantiate a client with the credentials from the credential profiles (.aws/credentials) file.
$settings = (array(
    'profile' => 'syumaK',
    'region'  => 'us-east-1',
    'version'  => 'latest',
));

$pinpointClient = new Aws\Pinpoint\PinpointClient($settings);

# The "From" address. This address has to be verified in Amazon Pinpoint in the region you're using to send email.
$SENDER = "redacted";

# The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses also have to be verified.
$TOADDRESS = "redacted";

try {

    $result = $pinpointClient->sendMessages([
      'ApplicationId' => 'REPLACE WITH APP ID (NOT NAME)',
      'MessageRequest' => [ // REQUIRED
        'Addresses' => [
          $TOADDRESS => [
            'ChannelType' => 'EMAIL',
          ],
        ],
        'MessageConfiguration' => [ // REQUIRED
          'EmailMessage' => [
            'FromAddress' => $SENDER,
            'SimpleEmail' => [
              'HtmlPart' => [
                'Charset' => 'utf-8',
                'Data' => 'my sample html',
              ],
              'Subject' => [
                'Charset' => 'utf-8',
                'Data' => 'my sample subject',
              ],
              'TextPart' => [
                'Charset' => 'utf-8',
                'Data' => 'my sample text',
              ]
            ]
          ],
        ],
      ]
    ]);

    print $result;

    } catch (AwsException $e){

        // output error message if fails
        error_log($e->getMessage());
    }
?>
  • Has anybody tried the AWS PINPOINT VOICE CHANNEL? I have tried to find some conde sample and NOT even AWS has any examples available as of today. Their sandbox works fine and my account was just approved for production use, but no luck finding a code example for AWS PHP SDK 3.x If anybody has a working example it would be greatly appreciated. – Hugo Barbosa Nov 20 '21 at 23:18