メールを送信する
今すぐ、またはあとで 1 通。添付ファイル付きでも、1 回のリクエストで 100 通でも送れます。
メッセージを送信する
sent, err := client.Emails.Send(ctx, openemail.Body{ "from": "Acme Billing <[email protected]>", "to": []string{"[email protected]", "[email protected]"}, "replyTo": "[email protected]", "subject": "Your September invoice", "html": "<p>The invoice is attached.</p>", "text": "The invoice is attached.", "tags": openemail.Body{"invoice": "inv_2026_09_4192"},})if err != nil { return err} fmt.Println(sent.ID(), sent.String("status"))to、cc、bcc にはアドレス 1 つまたはリストを渡せます。アドレスは Ada <[email protected]> のようなテキストか、email と name を持つ map です。本文は html と text、保存済みの template、または draftId から取られます。
すべての送信は冪等性キーを持ちます。クライアントは呼び出しごとにキーを 1 つ作り、再試行でも同じキーを使うため、再試行されたリクエストは最初のメッセージを再生します。プロセスや再起動をまたいでも保証するには openemail.WithIdempotencyKey を渡してください。
添付ファイル
invoice, err := os.ReadFile("invoice.pdf")if err != nil { return err} _, err = client.Emails.Send(ctx, openemail.Body{ "from": "[email protected]", "to": "[email protected]", "subject": "Your September invoice", "text": "The invoice is attached.", "attachments": []openemail.Body{ {"filename": "invoice.pdf", "content": invoice, "contentType": "application/pdf"}, {"fileId": "file_3f9c2a7b1e4d8f60a5c7b92d"}, },}) return errcontent は []byte または io.Reader を受け取り、クライアントが base64 にエンコードします。すでに base64 になっているテキストも渡せます。fileId は client.Files.Upload でアップロードしたファイルを指し、5 MB を超えるファイルはこの方法で送ります。
あとで送信する
scheduled, err := client.Emails.Send(ctx, openemail.Body{ "from": "[email protected]", "to": "[email protected]", "subject": "Your invoice is due tomorrow", "text": "A reminder that the invoice is due tomorrow.", "scheduledAt": time.Now().Add(24 * time.Hour),})if err != nil { return err} if _, err := client.Emails.Reschedule(ctx, scheduled.ID(), time.Now().Add(48*time.Hour)); err != nil { return err} cancelled, err := client.Emails.Cancel(ctx, scheduled.ID())if err != nil { return err} fmt.Println(cancelled.String("status"))scheduledAt は time.Time、ISO 8601 の時刻、または PT1H のような期間を受け取ります。ボディのどこにある time.Time も、UTC の ISO 8601 の時刻として送信されます。予約したメッセージは、送信されるまで変更またはキャンセルできます。
バッチ送信する
result, err := client.Emails.SendBatch(ctx, []openemail.Body{ {"from": "[email protected]", "to": "[email protected]", "subject": "Order AC-4192 has shipped", "text": "It is on its way."}, {"from": "[email protected]", "to": "[email protected]", "subject": "Order AC-4193 has shipped", "text": "It is on its way."},})if err != nil { return err} fmt.Println(result.Sent, "sent,", result.Failed, "failed") for _, item := range result.Items { if item.String("status") == "error" { fmt.Println(item.Int("index"), item.Object("error").String("message")) }}バッチには最大 100 通のメッセージを入れられ、全部成功か全部失敗かにはなりません。不正なアドレスはその項目だけを失敗させ、残りは送信されます。エラーに頼らず、Failed と各項目の status を確認してください。