英文:
Unable to cancel scheduled SendGrid email (with batch_id)
问题
使用SendGrid来安排发送电子邮件,但我立即取消并删除它(查看scheduled_events显示状态确实已取消)。
然而,电子邮件仍然发送给用户。
我知道文档上说:“您可以通过向“取消或暂停预定发送”端点传递批次ID来在预定发送时间前的10分钟内取消或暂停与批次ID相关联的所有邮件/发送请求。”
(https://docs.sendgrid.com/api-reference/cancel-scheduled-sends/delete-a-cancellation-or-pause-from-a-scheduled-send)
但在这个示例代码中,我将时间设置为20分钟,还尝试了90分钟甚至更长时间,电子邮件仍然被发送。最后,我尝试了删除和不删除电子邮件的情况(request4)。
我联系了他们的支持,但还没有收到回复,想知道我是否在这里做了什么特殊或错误的操作。
顺便说一下,我检查了状态码和响应 - 它们都看起来正常(添加了描述打印出的状态的注释)。
谢谢
英文:
Using SendGrid to schedule an email, but then I cancel it right away and delete (looking at scheduled_events shows that status is in fact canceled).
However, e-mail is still delievered to user.
I know it says "You can cancel or pause all of the mail/send requests associated with a batch ID up to 10 minutes before the scheduled send time by passing a batch_id to the "Cancel or pause a scheduled send" endpoint."
(https://docs.sendgrid.com/api-reference/cancel-scheduled-sends/delete-a-cancellation-or-pause-from-a-scheduled-send)
But in this example code I have the time set to 20 minutes, and also tried it for 90 minutes and more and the emails was still delivered. Lastly, I tried doing the same with and without deleting the email (request4).
I contacted their support, but haven't received a response yet, and was wondering if I am doing something out of the ordinary or wrong here.
BTW, I checked the status code and responses - they all seem fine (added comments describing the printed out statuses)
Thanks
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
}
答案1
得分: 1
当你批量发送电子邮件时,你可以通过取消批次来取消这些电子邮件。然而,我认为你在这里所做的是通过删除批次来取消取消操作。
根据文档:
当批次被取消时,与该批次相关的所有消息将保留在发送队列中。当达到它们的send_at值时,它们将被丢弃。
当批次被暂停时,与该批次相关的所有消息将保留在发送队列中,即使它们的send_at值已经过去。这意味着你可以取消暂停状态,一旦取消暂停,你的预定发送将会被传送。任何保持暂停状态且超过72小时的消息将被视为过期并丢弃。
批次保持了取消/暂停状态,但消息仍然保留在队列中。如果你删除批次,那么消息仍然在队列中,并且不再知道它们被取消了。
为了解决这个问题,我认为你需要在第三个请求处停止你上面的代码。不要执行第四个请求(删除批次),这样你的消息应该无法发送,符合要求。
英文:
When you send emails in a batch, you are able to cancel those emails by cancelling the batch. However, I think what you are doing here is cancelling the cancellation by deleting the batch.
> 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.
The batch holds the cancelled/paused status but the messages remain in the queue. If you delete the batch, then the messages are still in the queue and no longer know that they are cancelled.
To fix this, I believe you need to stop your above code at your 3rd request. Do not perform request 4 (deleting the batch) and your messages should fail to send as required.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论