Как отправить простое электронное письмо в AWS Pinpoint с помощью PHP SDK? - PullRequest
1 голос
/ 23 марта 2020

Официальная документация AWS PHP SDK Pinpoint настолько плотна, что даже отправка простого электронного письма кажется сложной задачей:)

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>',
        ],
    ]);

Есть ли у кого-нибудь рабочий фрагмент кода только для отправки простого электронного письма с помощью PHP SDK v3?

Ответы [ 2 ]

1 голос
/ 24 марта 2020

Следующий скрипт php демонстрирует, как вы можете использовать AWS PHP SDK v3 для отправки электронных писем с помощью сервиса Amazon Pinpoint с помощью sendMessage API.

Feel Вы можете свободно раскошелиться на gists и настроить его под свои нужды.

<?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());
    }
?>

Протестировал приведенный выше фрагмент кода, используя следующую среду spe c:

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

Надеюсь, это поможет!

0 голосов
/ 24 марта 2020

Основываясь на ответе syumaK, я наконец-то получил работу с этим фрагментом:

<?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());
    }
?>
...