1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Tencent Cloud SDK for PHPでSESメール送信

Posted at

参考
優技録:https://www.yuulinux.tokyo/19204/

APIキーの発行

SDKに必要なキーを発行する

Cloud Access Management > Access Key > API Key > Create Key

AWSみたいに権限設定といった手順はなくCreate Keyを押すとKeyが発行された
発行したアカウントと同等の権限が付与されているのかも
SecretKeyを見るために2段階認証あり。

API Explorer

API Explorerを使うとsdkのサンプルが見れる。便利
試しにSESで登録したメールテンプレート一覧を取得してみる

Simple Email Service > Template Apis > ListEmailTemplates
API Keyを入力してRegionはap-hongkongしか選択不可、limitとoffsetは適当に1,0で
スクリーンショット 2021-02-18 14.16.21.png

Online Call > Send Request

    "Error": {
      "Code": "UnsupportedRegion",
      "Message": "The action not support this region."
    },

Regionはap-hongkongしか選べないのにUnsupportedRegionだなんて!!

少し焦ったけどAPI Inspectorを使ってSESのコンソール見てみたら、SESのRegionはap-singaporeのようでした
なのでAPI Explorerからは動きません

PHPからメール送信

メールテンプレートは事前に作成ずみで、変数にtaroと表示するようにしてます

<?php
require_once 'vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Ses\V20201002\SesClient;
use TencentCloud\Ses\V20201002\Models\SendEmailRequest;
try {
    $cred = new Credential("SecretId", "SecretKey");
    $httpProfile = new HttpProfile();
    $httpProfile->setEndpoint("ses.tencentcloudapi.com");
    $clientProfile = new ClientProfile();
    $clientProfile->setHttpProfile($httpProfile);
    $client = new SesClient($cred, "ap-singapore", $clientProfile);
    $req = new SendEmailRequest();
    $params = array(
        "Destination" => [
            "to@test.com"
        ],
        "Template" => [
            "TemplateID" => 00000,
            "TemplateData" => "{\"name\":\"taro\"}"
        ],
        "FromEmailAddress" => "from@test.com",
        "Subject" => "test",
        "ReplyToAddresses" => "from@test.com"
    );
    $req->fromJsonString(json_encode($params));
    $resp = $client->SendEmail($req);
    print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
    echo $e;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?