使用 SendGrid 安排電子郵件,但隨后我立即取消并洗掉(查看 schedule_events 顯示狀態實際上已取消)。但是,電子郵件仍然發送給用戶。
我知道它說“您可以通過將 batch_id 傳遞給“取消或暫停計劃發送”端點,在計劃發送時間之前最多 10 分鐘取消或暫停與批處理 ID 關聯的所有郵件/發送請求。(https://docs.sendgrid.com/api-reference/cancel-scheduled-sends/delete-a-cancellation-or-pause-from-a-scheduled-send)
但在這個示例代碼中,我將時間設定為 20 分鐘,并且嘗試了 90 分鐘甚至更長時間,但電子郵件仍然發送。最后,我嘗試在洗掉或不洗掉電子郵件的情況下執行相同的操作(請求 4)。
我聯系了他們的支持,但還沒有收到回復,我想知道我在這里做的事情是不尋常的還是錯誤的。
順便說一句,我檢查了狀態代碼和回應 - 它們看起來都很好(添加了描述列印狀態的注釋)
謝謝
package main
import (
"fmt"
"time"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/tidwall/gjson"
)
const apiKey = "*******" // replace with yours
const host = "https://api.sendgrid.com"
func main() {
const MINUTES_TO_ADD = 20 // 20 mins
email := "[email protected]"
from := mail.NewEmail("X", "[email protected]")
subject := "subject"
to := mail.NewEmail("Test", email)
request := sendgrid.GetRequest(apiKey, "/v3/mail/batch", host)
request.Method = "POST"
response, err := sendgrid.API(request)
if err != nil {
return
}
batchId := gjson.Get(
response.Body,
"batch_id",
).Str
htmlContent := `Hi`
message := mail.NewSingleEmail(from, subject, to, "", htmlContent)
message.SendAt = int(time.Now().Add(time.Minute * MINUTES_TO_ADD).Unix())
message.BatchID = batchId
client := sendgrid.NewSendClient(apiKey)
mailSendResponse, err := client.Send(message)
if err != nil {
return
} else {
fmt.Println(mailSendResponse.StatusCode) // prints 202
}
request3 := sendgrid.GetRequest(apiKey, "/v3/user/scheduled_sends", host)
request3.Method = "POST"
request3.Body = []byte(`{
"batch_id": "` batchId `",
"status": "cancel"
}`)
response3, err := sendgrid.API(request3)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response3.StatusCode) // prints 201
request4 := sendgrid.GetRequest(apiKey, "/v3/user/scheduled_sends/" batchId, host)
request4.Method = "DELETE"
response4, err := sendgrid.API(request4)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response4.StatusCode) // prints 204
request5 := sendgrid.GetRequest(apiKey, "/v3/user/scheduled_sends", host)
request5.Method = "GET"
response5, err := sendgrid.API(request5)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(response5.StatusCode) // prints 200
}
uj5u.com熱心網友回復:
當您批量發送電子郵件時,您可以通過取消該批次來取消這些電子郵件。但是,我認為您在這里所做的是通過洗掉批次來取消取消。
從檔案:
取消批次后,與該批次關聯的所有訊息都將保留在您的發送佇列中。當它們的 send_at 值達到時,它們將被丟棄。
當一個批次暫停時,與該批次關聯的所有訊息都將保留在您的發送佇列中,即使它們的 send_at 值已通過。這意味著您可以移除暫停狀態,一旦移除暫停,您的預定發送就會被發送。任何超過 72 小時的暫停狀態的訊息都將被丟棄為過期。
該批次保持取消/暫停狀態,但訊息仍保留在佇列中。如果您洗掉該批次,則訊息仍在佇列中并且不再知道它們已被取消。
要解決此問題,我相信您需要在第三次請求時停止上述代碼。不要執行請求 4(洗掉批次),您的訊息應該無法按要求發送。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/444034.html
標籤:走 发送网格 sendgrid-api-v3
