登录
首页 >  Golang >  Go问答

无法取消预定的 SendGrid 电子邮件(带有batch_id)

来源:stackoverflow

时间:2024-03-16 08:12:29 279浏览 收藏

使用 SendGrid 安排电子邮件后,无法取消,原因是取消操作仅适用于批量发送。用户取消批量发送后,所有关联邮件仍保留在发送队列中,直到达到发送时间被丢弃。但是,用户删除了批量,导致邮件从队列中移除,失去了取消标记。因此,邮件仍然会被发送。要解决此问题,用户应在取消批量后停止操作,而不是删除批量。

问题内容

使用 sendgrid 安排电子邮件,但随后我立即取消并删除(查看 scheduled_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
}

正确答案


当您批量发送电子邮件时,您可以通过取消批量来取消这些电子邮件。但是,我认为您在这里所做的是通过删除批次来取消取消。

From the docs

取消批次后,与该批次关联的所有消息都将保留在您的发送队列中。当达到它们的send_at值时,它们将被丢弃。

当批次暂停时,与该批次关联的所有消息都将保留在您的发送队列中,即使在它们的 send_at 值已经过去之后也是如此。这意味着您可以删除暂停状态,一旦删除暂停,您的预定发送就会被传送。任何超过 72 小时且处于暂停状态的消息都将被视为已过期而被丢弃。

批次保持已取消/暂停状态,但消息仍保留在队列中。如果删除该批次,则消息仍在队列中并且不再知道它们已被取消。

要解决此问题,我相信您需要在第三次请求时停止上述代码。不要执行请求 4(删除批次),您的消息将无法按要求发送。

好了,本文到此结束,带大家了解了《无法取消预定的 SendGrid 电子邮件(带有batch_id)》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>