- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2020-08-26T06:17:38+00:00","","")
#mynavi(Go言語)
#setlinebreak(on);
#html(){{
<style>
.images img { border: 1px solid #333; margin-right: 40px; }
.images div { vertical-align: top; }
</style>
}}
* 概要 [#n43a772a]
#html(<div class="pl10">)
Azure Functions から SendGrid を使用してメール送信するサンプルを記載する。
尚、関数アプリは [[Azure Functions を Go で書く]] で作成したものをベースとする為、当記事の実装は Go となる。
#html(</div>)
* 目次 [#ae66c768]
#contents
- 関連
-- [[Azureメモ]]
-- [[Go言語]]
- 参考
-- [[SendGrid公式>https://sendgrid.kke.co.jp]]
--- [[Microsoft AzureからのSendGridの利用方法>https://sendgrid.kke.co.jp/blog/?p=2621]]
--- [[Goでメール送信!SendGridを使って簡単に実装する方法>https://sendgrid.kke.co.jp/blog/?p=1241]]
--- [[Goのサンプルコード(公式サイト)>https://sendgrid.kke.co.jp/docs/Integrate/Code_Examples/v3_Mail/go.html]]
--- [[Goのサンプルコード>https://sendgrid.kke.co.jp/docs/Integrate/Code_Examples/v3_Mail/go.html]]
--- [[価格>https://sendgrid.kke.co.jp/plan/]]
-- Azureドキュメント
--- [[SendGrid を使用した Azure での電子メールの送信方法>https://docs.microsoft.com/ja-jp/azure/sendgrid-dotnet-how-to-send-email]]
* SendGird アカウントの登録 [#o35e4036]
#html(<div class="pl10">)
** 登録方法による FREEプランの利用枠の違い [#f0d921a5]
#html(<div class="pl10">)
Azure Marketplace からと 日本公式サイトから登録できるが、登録方法によってFREEプランの利用枠が異なる。
| 登録先 | FEEプランの利用枠 |h
| Marketplace | 25,000通/月 |
| 日本公式サイト | 400通/日 |
※ https://sendgrid.kke.co.jp/blog/?p=2621
日単位と月単位で紛らわしいが、Marketplace から登録する方が2倍近くの量を送信できる。
#html(</div>)
** Marketplace から SendGird アカウント登録 [#k51715d3]
#html(<div class="pl10">)
#TODO
#html(<div class="images"><div class="ib">)
Azure ポータルで検索バーから SendGrid Accounts を検索。
&ref(sendgrid_setup01.png,nolink);
#html(</div><div class="ib">)
[追加] を押下。
&ref(sendgrid_setup02.png,nolink);
#html(</div><div class="ib">)
アカウント情報を入力して登録。(とりあえずFREEプランで)
&ref(sendgrid_setup03.png,nolink);
#html(</div><div class="ib">)
Azure ポータルの SendGrid Accounts から Manage を押下。
&ref(sendgrid_setup04.png,nolink);
#html(</div><div class="ib">)
確認メールを送るか聞かれるのでボタンを押下。
&ref(sendgrid_setup05.png,nolink);
#html(</div><div class="ib">)
こんな感じのメールが届くのでボタンを押下。
&ref(sendgrid_setup06.png,nolink);
#html(</div></div>)
#html(</div>)
#html(</div>)
* 利用準備 [#k1941cd8]
#html(<div class="pl10">)
** SendGrid API キーの作成/確認 [#jf890ebb]
#html(<div class="pl10">)
#html(<div class="images"><div class="ib">)
Send Grid 側の管理画面から API Keys を押下。
&ref(sendgrid_setup07.png,nolink);
#html(</div><div class="ib">)
Create API Key を押下。
&ref(sendgrid_setup08.png,nolink);
#html(</div><div class="ib">)
APIキー名を入力して、[Create & View]を押下。
&ref(sendgrid_setup09.png,nolink);
#html(</div><div class="ib">)
APIキーが画面に表示されるので、メモしておく。
&ref(sendgrid_setup10.png,nolink);
#html(</div></div>)
#html(</div>)
** go get [#u7ab22ca]
** goパッケージインストール [#o10c469b]
#html(<div class="pl10">)
SendGrid公式の Go パッケージをインストールしておく。
#myterm2(){{
go get github.com/sendgrid/sendgrid-go
go get github.com/joho/godotenv
}}
#html(</div>)
#html(</div>)
* 実装 [#eb83275b]
#html(<div class="pl10">)
#html(){{
<style>
.ui-widget-content a {
color: #215dc6;
}
.ui-widget-content .links a {
color: #215dc6;
text-decoration: underline;
}
</style>
<div id="tabs1">
<ul>
<li><a href="#tabs1-1">main.go</a></li>
<li><a href="#tabs1-2">local.settings.json</a></li>
</ul>
}}
// START tabs1-1
#html(<div id="tabs1-1">)
基本的な実装は [[Azure Functions を Go で書く]] と変わらないので、以下メール送信部分のみ抜粋して記載する。
[補足]
複数の宛先に送信する実装を以下の公式サイトを参考に実装しようとしたが、
最新版では構成が微妙に変わっているらしく、ビルドが通らなかった為、[[GitHubのソース>https://github.com/sendgrid/sendgrid-go]] を参考に実装した。
#html(<div class="links">)
- [[Goでメール送信!SendGridを使って簡単に実装する方法>https://sendgrid.kke.co.jp/blog/?p=1241]]
1つの宛先に送信する方のサンプルはそのままでも大丈夫。
- [[Goのサンプルコード>https://sendgrid.kke.co.jp/docs/Integrate/Code_Examples/v3_Mail/go.html]]
#html(</div>)
#mycode2(){{
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
)
:
func sendmail(subject string, messages []string, result string) {
API_KEY := os.Getenv("SENDGRID_API_KEY")
senderName := os.Getenv("MAIL_FROM_NAME")
senderMail := os.Getenv("MAIL_FROM_ADDRESS")
recipients := strings.Split(os.Getenv("MAIL_TO_ADDRESSES"), ",")
messageText := ""
messageHtml := ""
for _, s := range messages {
messageText = fmt.Sprintf("%s%s\n", messageText, s)
messageHtml = fmt.Sprintf("%s%s<br />", messageText, s)
}
if result != "SUCCESS" {
messageHtml = fmt.Sprintf("<div style='color: #f00'>%s</div>", messageHtml)
}
from := mail.NewEmail(senderName, senderMail)
email := mail.NewV3Mail()
email.SetFrom(from)
email.Subject = subject
p := mail.NewPersonalization()
for _, s := range recipients {
fmt.Printf("name: %s\n", s)
to := mail.NewEmail(s, s)
p.AddTos(to)
}
email.AddPersonalizations(p)
textContent := mail.NewContent("text/plain", messageText)
htmlContent := mail.NewContent("text/html", messageHtml)
email.AddContent(textContent, htmlContent)
client := sendgrid.NewSendClient(API_KEY)
response, err := client.Send(email)
if err != nil {
log.Println(err)
} else if response.StatusCode == 202 {
log.Printf("info: Mail sended. Status=%d", response.StatusCode)
} else {
log.Printf("error: status %d", response.StatusCode)
log.Printf("error: header %v", response.Headers)
log.Printf("error: body %s", response.Body)
}
}
:
}}
#html(</div>)
// END tabs1-1
// START tabs1-2
#html(<div id="tabs1-2">)
#mycode2(){{
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
:
"SENDGRID_API_KEY": "上記で取得したSendGridのAPIキー",
"MAIL_FROM_NAME": "送信者名",
"MAIL_FROM_ADDRESS": "送信者のメールアドレス",
"MAIL_TO_ADDRESSES": "送信先アドレス1,送信先アドレス2,送信先アドレス3"
}
}
}}
#html(</div>)
// END tabs1-2
#html(</div>)
// END tabs1
#html(<script>$(function() { $("#tabs1").tabs(); });</script>)
#html(</div>)