Lambda(PHP)とSESで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\Ses\SesClient;
use Aws\Exception\AwsException;
return function ($event) {
$client = new SesClient([
'version' => '2010-12-01',
'region' => 'ap-northeast-1',
]);
try {
$result = $client->sendEmail([
'Destination' => [ // 必須
'ToAddresses' => ['example@example.com'], // 送信先メールアドレス
],
'Message' => [ // 必須
'Body' => [ // 必須
'Text' => [
'Data' => '本文です', // 必須
]
],
'Subject' => [ // 必須
'Data' => 'タイトルです', // 必須
]
],
'Source' => 'example@example.com' // 必須 送信元メールアドレス
]);
} catch (AwsException $e) {
$error = $e->getMessage();
var_dump($error);
exit(500);
}
var_dump($result);
return "ok";
};
sendEmailの必須項目のみ設定しています。
デプロイします。
$ sls deploy
Lambda実行すると、メールが送信されていることが確認できます。
参考サイト
ERROR: The request could not be satisfied
ERROR: The request could not be satisfied

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^



コメント