Twilio SendGridを使用したメール送信を、Oracle APEXより行ってみました。
Twilio SendGridの無料アカウントを、以下のサイトより作成しました。クレジット・カードの入力は求められていません。
サイド・メニューにあるEmail APIのIntegration Guideを開き、Web APIのChooseをクリックします。
Web APIを使用するために必要な手順が表示されます。
cURLが一番PL/SQLでのアクセスの参考になるため、cURLを選択します。
Chooseをクリックします。
作業手順が表示されます。
My First API Key Nameに名前を入力し、Create Keyをクリックします。
SendGridを呼び出す際に使用するAPIキーが生成されます。
APIキーが生成されます。生成されたAPIキーをクリップボードにコピーして保存しておきます。再度表示することはできないので、忘れずに保存します。
Oracle APEXでWeb資格証明を作成する際に使用します。
Twilio SendGridのAPIキーが作成できました。
SettingsのSender Authenticationの設定も行います。DNSの設定ができない場合は、Single Sender Verificationの設定を行なうことになるはずです。
Oracle APEXでの作業に移ります。
ワークスペース・ユーティリティよりWeb資格証明を開きます。
作成済みのWeb資格証明が一覧されます。作成をクリックします。
名前はTwilio SendGridとしました。静的識別子はTWILIO_API_KEYとしています。静的識別子は、APEX_WEB_SERVICE.MAKE_REST_REQUESTの引数p_credential_static_idとして指定します。
認証タイプとしてHTTPヘッダーを選択します。資格証明名はHTTPヘッダー名であるAuthorizationを記述します。
資格証明シークレットはAuthorizationヘッダーに与える値になります。Bearerで始めて空白で区切り、Twilio SendGridのAPIキーを続けます。
Bearer SendGridのAPIキー
URLに対して有効は、SendGridのエンドポイントとなるURLを指定します。
https://api.sendgrid.com/v3/mail/send
以上で作成をクリックします。
SQLコマンドを開き、以下のPL/SQLコードを実行します。l_recipients、l_senderを変更します。l_senderについては、Twilio SendGrid側でSingle Senderとして検証したメール・アドレスを指定します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare | |
l_request json_object_t; | |
l_email json_object_t; | |
l_email_array json_array_t; | |
l_email_to json_object_t; | |
l_personalizations_array json_array_t; | |
l_content json_object_t; | |
l_content_array json_array_t; | |
-- | |
type t_recipients is table of varchar2(80); | |
l_recipients t_recipients; | |
l_sender varchar2(80); | |
l_subject varchar2(32767); | |
type t_types is table of varchar2(80); | |
l_types t_types; | |
type t_contents is table of varchar2(32767); | |
l_values t_contents; | |
-- | |
l_response clob; | |
begin | |
l_recipients := t_recipients('受信者のメール・アドレス'); | |
l_sender := '送信者のメール・アドレス'; | |
l_subject := 'Sending with SendGrid is Fun'; | |
l_types := t_types('text/plain'); | |
l_values := t_contents('and easy to do anywhere, even with PL/SQL'); | |
/* | |
* 電子メールのリクエストはl_requestに保持する。 | |
* 受信者はl_personalizations_arrayに保持する。 | |
*/ | |
l_request := json_object_t(); | |
l_personalizations_array := json_array_t(); | |
/* | |
* メール・アドレス | |
*/ | |
l_email_array := json_array_t(); | |
for i in 1..l_recipients.count | |
loop | |
l_email := json_object_t(); | |
l_email.put('email', l_recipients(i)); | |
l_email_array.append(l_email); | |
end loop; | |
/* | |
* Toの宛先として保存する。 | |
*/ | |
l_email_to := json_object_t(); | |
l_email_to.put('to', l_email_array); | |
l_personalizations_array.append(l_email_to); | |
/* | |
* リクエストに宛先を設定する。 | |
*/ | |
l_request.put('personalizations', l_personalizations_array); | |
/* | |
* Fromを設定する。 | |
*/ | |
l_email := json_object_t(); | |
l_email.put('email', l_sender); | |
l_request.put('from', l_email); | |
/* | |
* Subjectを設定する。 | |
*/ | |
l_request.put('subject', l_subject); | |
/* | |
* Contentを設定する。 | |
*/ | |
l_content_array := json_array_t(); | |
for i in 1..l_values.count | |
loop | |
l_content := json_object_t(); | |
l_content.put('type', l_types(i)); | |
l_content.put('value', l_values(i)); | |
l_content_array.append(l_content); | |
end loop; | |
l_request.put('content',l_content_array); | |
-- 確認 | |
-- dbms_output.put_line(l_request.to_clob()); | |
apex_web_service.clear_request_headers; | |
apex_web_service.set_request_headers('Content-Type','application/json',p_reset = false); | |
l_response := apex_web_service.make_rest_request( | |
p_url => 'https://api.sendgrid.com/v3/mail/send' | |
,p_http_method => 'POST' | |
,p_body => l_request.to_clob() | |
,p_credential_static_id => 'TWILIO_API_KEY' | |
); | |
dbms_output.put_line(l_response); | |
end; |
正常に終了すると、応答となるデータは返ってこないようです。
件名がSending with SendGrid is Fun、本文がand easy to do anywhere, even with PL/SQLというメールの受信を確認できたら、Twilio SendGridの動作確認は終了です。
完