Lambda(PHP)とSNSでE-mailを送る方法

Lambda(PHP)とSNSでE-mailを送る方法

前提

項目 バージョン
OS Ubuntu 20.04.4 LTS
PHP PHP 7.4.3-4ubuntu2.17 (cli) (built: Jan 10 2023 15:37:44) ( NTS )

Composerインストール済みであること
トピック、サブスクリプション(プロトコル:E-mail)作成済みであること

$ sudo apt update
$ sudo apt install -y php-curl
$ sudo apt install -y php7.4-xml
$ sudo apt install -y composer

aws sdkインストール

composerでaws sdkをインストールします。

$ composer require bref/bref
$ vendor/bin/bref init
 What kind of lambda do you want to create? (you will be able to add more functions later by editing `serverless.yml`) [Web application]:
  [0] Web application
  [1] Event-driven function
 > 1
$ composer require aws/aws-sdk-php

index.php

index.phpを修正します。

<?php require __DIR__ . '/vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException; return function ($event) { $client = new SnsClient([ 'version' => '2010-03-31',
        'region' => 'ap-northeast-1',
    ]);
    try {
        $result = $client->publish([
            'Message' => 'hohogeです', // 必須 
            'TopicArn' => '<topicのArn>'
        ]);
    } catch (AwsException $e) {
        $error = $e->getMessage();
        var_dump($error);
        exit(500);
    }
    var_dump($result);
    return "ok";
};

デプロイします。

$ sls deploy

Lambda実行すると、メールが送信されていることが確認できます。

Lambda(PHP)とSNSでE-mailを送る方法

参考サイト

Installation — AWS SDK for PHP documentation
Sending SMS messages in Amazon SNS with the Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Get and set SMS messaging preferences, check a phone number, and get a list of phone numbers for Amazon SNS using the Am...
AWS SDK for PHP 3.x

コメント

タイトルとURLをコピーしました