独自の HTTP API を通じてメールを送信する
このガイドでは、認証コードやパスワードリセットリンクなどの Casdoor のメールを、あなたが定義した HTTP エンドポイントを通じて送信する方法について説明します。組み込みタイプがないメールサービスや、メールの構築と送信を行う独自サービスで使用してください。
学習目標
- メールサービスの HTTP リクエストを Casdoor で定義します。
- Casdoor がリクエストをどのように構築するかを理解します。
必要なもの
- 成功時に HTTP 200 を返す HTTP API を持つメールサービスまたは Webhook
- Casdoor 管理コンソールへの管理者アクセス権
Casdoor にプロバイダーを追加する
-
Casdoor管理コンソールで、Identity > Providers に移動し、プロバイダーを追加します。
-
Category を
Emailに、Type をCustom HTTP Emailに設定します。 -
フィールドに入力します:
Field Description From address Email address of the sender From name Name of the sender. If empty, Casdoor uses the display name of the organization Endpoint URL of your API. The default is https://example.com/send-custom-http-emailMethod GET,POST(default),PUT, orDELETEContent type For methods other than GET:application/x-www-form-urlencoded(default) orapplication/jsonHTTP header Extra request headers, such as an Authorizationheader with your API key. Enter each header as a row of Keys and ValuesHTTP body mapping For methods other than GET: the field names of the request. See Field names -
Email title と Email content を記述します。Templatesを参照してください。
-
プロバイダーを保存し、アプリケーションに追加します。Add providers to an applicationを参照してください。
このページには Host, Port, SSL mode, Enable proxy, Test SMTP Connection も表示されますが、Custom HTTP Email プロバイダーではこれらを使用しません。
Casdoorによるリクエストの構築方法
フィールド名
リクエストにはこれらのフィールドが含まれます。HTTP body mapping で、各フィールドをAPIが期待する名前に変更できます。マッピングの各行に値が必要です。
| Row | Default field name | Value |
|---|---|---|
| From name | fromName | From name |
| From address | fromAddress | From address |
| To address | toAddress | The recipient |
| Subject | subject | Email title |
| Email content | content | Email content, with the placeholders replaced |
複数の宛先の場合、Casdoorは toAddress の代わりに、各宛先ごとに toAddresses フィールドを送信します。
メソッドとコンテンツタイプ
GET: Casdoorはすべてのフィールドを Endpoint のクエリパラメータとして送信します。POST,PUT, またはDELETEwithapplication/x-www-form-urlencoded: Casdoorはすべてのフィールドをボディ内のフォームフィールドとして送信します。POST,PUT, またはDELETEwithapplication/json: Casdoorは、送信者、件名、内容のフィールドを含むJSONオブジェクトを送信します。
現在のバージョンでは、JSONオブジェクトに宛先フィールドがありません。APIがリクエスト内で宛先を必要とする場合は、application/x-www-form-urlencoded または GET を使用してください。
Casdoorは HTTP header のヘッダーをリクエストに追加します。HTTP 200以外のすべてのレスポンスを失敗として扱います。
メールの言語
Casdoorが認証コードまたはマジックリンクを送信する際、要求元のブラウザの言語を示す Accept-Language ヘッダーを、en や zh などの2文字コードとして追加します。HTTP header にすでに Accept-Language の行がある場合、Casdoorはその値を保持します。APIはこのヘッダーを使用して、ユーザーの言語でメールを送信できます。これは、ユーザーが存在する前にCasdoorが送信するサインアップコードにも適用されます。
例
フィールド from, to, subject, html を含む POST フォームを受け入れるAPI:
| Field | Value |
|---|---|
| Endpoint | https://mail.example.com/api/send |
| Method | POST |
| Content type | application/x-www-form-urlencoded |
| HTTP header | Authorization: Bearer <your-api-key> |
| HTTP body mapping | From address: from, To address: to, Subject: subject, Email content: html |
<your-api-key> は、あなたのメールサービスのAPIキーです。
結果の確認
- プロバイダーを保存します。
- Test Email にアドレスを入力し、Send Testing Email をクリックします。APIがリクエストを受信します。