0
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?

blastengineのGo SDKを使って大量のアドレスを登録・送信する

Posted at

blastengine(ブラストエンジン)はシンプルに使える開発者向けメールサービスを提供しています。何かと面倒なメール配信をシンプルに、かつ確実に送信先へメールを届けられます。

現在、Go言語向けに、blastengine SDKを開発しています。今回は、バルクメール(一括配信メール)のテンプレート登録や大量のアドレス宛に送信する方法について解説します。

ユーザ登録する

blastengineにユーザ登録します。管理画面に入るためのユーザID、パスワードが手に入るので、ログインします(ユーザIDは後で使います)。

送信元ドメインのSPFを設定する

送信元として利用するドメイン(自分で持っているもの)の設定をします。これは任意のドメイン管理サービスで設定できますが、TXTレコードに以下のSPFを追加します。

txt @ v=spf1 include:spf.besender.jp ~all

APIキーを取得する

ログイン後、管理画面の右上にある設定メニューに移動します。

getting-started-6.jpg

そして設定の中で、APIキーを取得します。

getting-started-5.png

必要な情報について

SDKを使う上で必要なのは、先ほどのAPIキーとユーザIDになります。

SDKのインストール

SDKはblastengineMania/blastengine-go: Blastengine Golang SDKにて公開しています。インストールは go get で行います。

go get github.com/blastengineMania/blastengine-go

初期化

先ほどのAPIキーとユーザIDを使って初期化します。

package main

import (
	"fmt"
	"github.com/blastengineMania/blastengine-go"
)

func main() {
	apiKey := "yourApiKey"
	userId := "yourUserId"
	client := blastengine.Initialize(apiKey, userId)
}

バルクオブジェクトの作成

メールを一括配信する際にはバルクオブジェクトを使います。

bulk := client.NewBulk()

必要な情報をセットする

続けて、バルクオブジェクトに必要なオブジェクトをセットします。

bulk.SetFrom("from@example.com", "Sender Name")
bulk.SetSubject("Test Subject")
bulk.SetTextPart("This is a text part")
bulk.SetHtmlPart("<p>This is an HTML part</p>")

テンプレートを登録する

バルクメールは、 Begin メソッドを実行して、一度メールをテンプレートとして登録します。

err := bulk.Begin()
if err != nil {
  fmt.Println("Failed to create bulk:", err)
} else {
  fmt.Println("Bulk created successfully")
}

送信先を登録する

登録した後、送信先を登録します。送信先は、メールアドレスと置換するキーワードのペアで登録します。標準のAPIでは50件までになりますが、SDK経由の場合は数の制限はありません。

bulk.AddTo("test@example.com", map[string]string{"name": "Test User 1"})
bulk.AddTo("test2@example.com", map[string]string{"name": "Test User 2"})

そして、更新します。 Import メソッドを使うと、 Job 構造体が返ってきます。

job, err := bulk.Import(nil)
if err != nil {
  fmt.Println("Failed to import bulk:", err)
} else {
  fmt.Println("Bulk imported successfully")
}

1つ目の引数はインポート時のオプションになります。

job, err := bulk.Import(&ImportParams{
  IgnoreErrors: true, // エラーデータを無視する
  Immediate: true, // インポート後、即時配信する
})

job がインポートプロセスを完了するのを待機します。

for {
  b, err := job.Finished()
  if err != nil {
    t.Errorf("Expected error to be nil, but got %v", err)
  }
  if b {
    break
  }
  time.Sleep(1 * time.Second)
}

インポートが終われば、メール送信できます。

バルクメールを送信するには、 Send メソッドを実行します。

err := bulk.Send(nil)
if err != nil {
  fmt.Println("Failed to send bulk:", err)
} else {
  fmt.Println("Bulk sent successfully")
}

配信日時を指定する際には、引数に time.Time を指定します。

sendTime := time.Now().Add(time.Hour)
err := bulk.Send(&sendTime)
if err != nil {
  fmt.Println("Failed to send bulk:", err)
} else {
  fmt.Println("Bulk sent successfully")
}

もしデータに不具合があれば、そのレポートをダウンロードできます。

s, err := job.Download()
if err != nil {
  t.Errorf("Expected error to be nil, but got %v", err)
}
fmt.Print(s)

たとえば以下のような文字列が返ってきます。

"エラーメッセージ","email","__key__"
"atsushi+be@: data is invalid email address.","atsushi+be@","001"

メールの削除

バルクメールは途中で削除できます。

err := bulk.Delete()
if err != nil {
  fmt.Println("Failed to delete bulk:", err)
} else {
  fmt.Println("Bulk deleted successfully")
}

メール配信をキャンセルする

予約設定した後で、メール配信をキャンセルする際には Cancel メソッドを実行します。

err := bulk.Cancel()
if err != nil {
  fmt.Println("Failed to cancel bulk:", err)
} else {
  fmt.Println("Bulk canceled successfully")
}

メールの情報を取得する

トランザクションメール(即時配信メール)とバルクメール、どちらも Get メソッドでメールに関する情報を取得できます。

transaction.Get()
// または
bulk.Get()

bulk.Status // EDIT
bulk.DeliveryType // BULK
bulk.CreatedTime // time.Time

コードについて

コードはGitHubにアップしてあります。ライセンスはMIT Licenseになります。

blastengineMania/blastengine-go: Blastengine Golang SDK

まとめ

blastengineには、他にも1クリックでの購読停止機能や、Webhookなどの仕組みがあります。他の機能も随時実装していきますので、ぜひ皆さんのシステム開発に役立ててください。

エンジニア向けメール配信システム「ブラストエンジン」

0
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
0
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?