无法取消预定的 SendGrid 电子邮件(batch_id)
Unable to cancel scheduled SendGrid email (with batch_id)
使用 SendGrid 安排电子邮件,但我立即取消并删除(查看 scheduled_events 显示状态实际上已取消)。
但是,电子邮件仍会发送给用户。
我知道它说“您可以取消或暂停与批次 ID 关联的所有 mail/send 请求,方法是将 batch_id 传递给“取消”或暂停预定的发送“端点”。
(https://docs.sendgrid.com/api-reference/cancel-scheduled-sends/delete-a-cancellation-or-pause-from-a-scheduled-send)
但在这个示例代码中,我将时间设置为 20 分钟,并且还尝试了 90 分钟或更长时间,电子邮件仍然送达。最后,我尝试在删除和不删除电子邮件的情况下执行相同的操作 (request4)。
我联系了他们的支持人员,但还没有收到回复,想知道我是否在这里做错了什么。
顺便说一句,我检查了状态代码和响应 - 它们看起来都很好(添加了描述打印状态的评论)
谢谢
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 := "test@test.com"
from := mail.NewEmail("X", "sender@test.com")
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
}
当您批量发送电子邮件时,您可以通过取消批量来取消这些电子邮件。但是,我认为你在这里做的是通过删除批处理来取消取消。
When a batch is canceled, all messages associated with that batch will stay in your sending queue. When their send_at value is reached, they will be discarded.
When a batch is paused, all messages associated with that batch will stay in your sending queue, even after their send_at value has passed. This means you can remove a pause status, and your scheduled send will be delivered once the pause is removed. Any messages left with a pause status that are more than 72 hours old will be discarded as Expired.
批次保持 cancelled/paused 状态,但邮件仍在队列中。如果删除批次,则消息仍在队列中,不再知道它们已被取消。
要解决此问题,我认为您需要在第三次请求时停止上述代码。不执行请求4(删除批量),您的消息应该无法按要求发送。
使用 SendGrid 安排电子邮件,但我立即取消并删除(查看 scheduled_events 显示状态实际上已取消)。 但是,电子邮件仍会发送给用户。
我知道它说“您可以取消或暂停与批次 ID 关联的所有 mail/send 请求,方法是将 batch_id 传递给“取消”或暂停预定的发送“端点”。 (https://docs.sendgrid.com/api-reference/cancel-scheduled-sends/delete-a-cancellation-or-pause-from-a-scheduled-send)
但在这个示例代码中,我将时间设置为 20 分钟,并且还尝试了 90 分钟或更长时间,电子邮件仍然送达。最后,我尝试在删除和不删除电子邮件的情况下执行相同的操作 (request4)。
我联系了他们的支持人员,但还没有收到回复,想知道我是否在这里做错了什么。
顺便说一句,我检查了状态代码和响应 - 它们看起来都很好(添加了描述打印状态的评论)
谢谢
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 := "test@test.com"
from := mail.NewEmail("X", "sender@test.com")
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
}
当您批量发送电子邮件时,您可以通过取消批量来取消这些电子邮件。但是,我认为你在这里做的是通过删除批处理来取消取消。
When a batch is canceled, all messages associated with that batch will stay in your sending queue. When their send_at value is reached, they will be discarded.
When a batch is paused, all messages associated with that batch will stay in your sending queue, even after their send_at value has passed. This means you can remove a pause status, and your scheduled send will be delivered once the pause is removed. Any messages left with a pause status that are more than 72 hours old will be discarded as Expired.
批次保持 cancelled/paused 状态,但邮件仍在队列中。如果删除批次,则消息仍在队列中,不再知道它们已被取消。
要解决此问题,我认为您需要在第三次请求时停止上述代码。不执行请求4(删除批量),您的消息应该无法按要求发送。