如何使用PHP SDK在AWS Pinpoint中发送简单电子邮件?

时间:2020-03-23 15:28:15

标签: php amazon-web-services aws-pinpoint

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 个答案:

答案 0 :(得分:1)

以下php脚本演示了如何使用AWS PHP SDK v3通过sendMessage API通过Amazon Pinpoint服务发送电子邮件。

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

使用以下环境规范测试了以上代码段:

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

希望这会有所帮助!

答案 1 :(得分:0)

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