【问题标题】:How to send a simple email in AWS Pinpoint using the PHP SDK?如何使用 PHP 开发工具包在 AWS Pinpoint 中发送简单的电子邮件?
【发布时间】:2022-02-27 18:26:24
【问题描述】:

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

是否有人有一个工作代码 sn-p 仅用于使用 PHP SDK v3 发送简单的电子邮件?

【问题讨论】:

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


    【解决方案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());
        }
    ?>
    

    使用以下环境规范测试了上面的代码sn-p:

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

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回答。我从那里开始编码并得到了一个可行的解决方案。一开始我收到“找不到资源”错误。但是,最后我取出了“TemplateConfiguration”块并在“EmailMessage”下添加了“SimpleEmail”块。这样就成功了!
    【解决方案2】:

    根据 syumaK 的回答,我终于可以使用这个 sn-p:

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

    【讨论】:

    • 有人试过 AWS PINPOINT VOICE CHANNEL 吗?我试图找到一些 conde 样本,但截至今天,甚至 AWS 也没有任何可用的示例。他们的沙箱工作正常,我的帐户刚刚被批准用于生产,但没有运气找到 AWS PHP SDK 3.x 的代码示例如果有人有工作示例,将不胜感激。
    猜你喜欢
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多