Email Delivery, powered by Sendgrid を使う
こちらのサービスを使ってメールを送信します。
IaaS を有効化済みのアカウントをお持ちであれば、25000 通まで無料の「Free Package」を活用できます。
Email Delivery, powered by Sendgrid - IBM Cloud : https://cloud.ibm.com/catalog/infrastructure/email-delivery

GitHub からダウンロード
GitHub にあるファイルをダウンロードします。
git clone https://github.com/khayama/fn-sendEmail.git
cd fn-sendEmail
今回の Functions デプロイ用 yaml では以下のように定義しています。
Functions 実行パラメータは、環境変数の値を取得して埋め込む形で定義しています。
sendEmail.yml
packages:
sendEmail_package:
version: 1.0
license: Apache-2.0
actions:
sendEmail:
version: 1.0
description: sendEmail with SL_USER, SL_APIKEY
docker: openwhisk/dockerskeleton
function: sendEmail.sh
inputs:
SL_USER: $SL_USER
SL_APIKEY: $SL_APIKEY
TO: $TO
FROM:
default: no-reply@ibmfunctions.com
SUBJECT:
default: test subject
BODY:
default: test body
triggers:
every5minutes:
feed: /whisk.system/alarms/alarm
inputs:
cron: "*/5 * * * *"
rules:
sendEmail-every5minutes:
description: Action is triggered every 5 minutes
action: sendEmail
trigger: every5minutes
環境変数セット
以下のように環境変数をセットします。
export SL_USER="xxx"
export SL_APIKEY="xxx"
export TO="宛先メールアドレス"
Functions デプロイ
ibmcloud fn deploy --manifest sendEmail.yml
# 削除するには ibmcloud fn undeploy --manifest sendEmail.yml
動作確認
5 分おきの定期トリガーの後に、「sendEmail」のアクションが正常に実行されていることが確認できます。
受信メール
こんな感じです。
参考
- sendEmail - https://sldn.softlayer.com/
- ibm cloud infrastructure - Send email on softlayer - Stack Overflow
- curlでJSONをPOSTするとき値にシェル変数を使う方法について - Qiita
- Shinobilayer: SendGrid初心者のための注意点(SoftLayer版) - Qiita
- Getting started with email delivery
- IBM Cloud Functions alarm package の cron 秒フィールドの注意点 - Qiita
- JSONデータに改行コードを入れる方法 | SRIA BLOG – 宮城県仙台市のWEBシステム開発・スマホアプリ開発