如何从sendgrid获取活动列表?他们是否提供了一些API来获取基于不同类别的响应?

Meg*_*tes 5 email sendgrid

在我正在使用的项目中,Sendgrid它用于发送批量交易或营销电子邮件.在我的项目中它工作正常.我可以从我的项目发送交易电子邮件.但是,为了阅读每封电子邮件的活动(如打开,点击,退回,无效电子邮件),我需要进入我的sendgrid帐户并逐一检查,这是非常耗时且耗时的选项.我想在我的项目中自动完成.我已经阅读了sendgrid documentation,但我没有找到任何方法来获取OPEN邮件列表,BOUNCED电子邮件.你能帮帮我吗,我怎么能这样做

Mat*_*att 4

您需要使用 SendGrid 中内置的 WebHooks。它称为事件 Webhook。

https://sendgrid.com/docs/API_Reference/Webhooks/event.html

基本上,您在 SendGrid 中设置 Webhook(转到“设置”->“邮件设置”)并单击“事件通知”。打开它并选择您感兴趣的事件。您可以使用名为 Request bin 的网站来测试会发生什么。

http://requestb.in/

我发现测试此功能的最简单方法是通过 SendGrid 发送电子邮件,然后查看它通过 requestb.in 发送的内容。然后我根据这些数据在我的系统中编写了 API(C# WebApi)。

我不知道你正在使用什么语言/框架,但这是我在 WebApi/C# 中的 API。

API控制器:

        [HttpPost]
        [Route("api/emailWebHooks")]
        public async Task<IHttpActionResult> UpdateEmail(IList<SendGridNotification> sendGridNotifications)
        {
            Log.Debug("UpdateEmail: " + JsonConvert.SerializeObject(sendGridNotifications));
            var updated = await _sendEmailService.UpdateSendGridEmailState(sendGridNotifications);
            return Ok(updated);
        }
Run Code Online (Sandbox Code Playgroud)

还有班级:

    public class SendGridNotification
    {
        /// <summary>
        /// The SendGrid event id
        /// </summary>
        [JsonProperty(PropertyName = "sg_event_id")]
        public string SgEventId { get; set; }

        /// <summary>
        /// The SendGrid message id
        /// </summary>
        [JsonProperty(PropertyName = "sg_message_id")]
        public string SgMessageId { get; set; }

        /// <summary>
        /// The 'event' property. e.g. Processed
        /// </summary>
        [JsonProperty(PropertyName = "event")]
        public string EventName { get; set; }

        /// <summary>
        /// The email id - this is unique argument added when the email was sent
        /// </summary>
        [JsonProperty(PropertyName = "emailId")]
        public string EmailId { get; set; }

        /// <summary>
        /// The email address of the recipient
        /// </summary>
        [JsonProperty(PropertyName = "email")]
        public string Email { get; set; }

        /// <summary>
        /// SendGrid's smtp id
        /// </summary>
        [JsonProperty(PropertyName = "smtp-id")]
        public string SmtpId { get; set; }

        /// <summary>
        /// The timestamp of the email
        /// </summary>
        [JsonProperty(PropertyName = "timestamp")]
        public long Timestamp { get; set; }

        /// <summary>
        /// The IP address - of the server?
        /// </summary>
        [JsonProperty(PropertyName = "ip")]
        public string IpAddress { get; set; }

        /// <summary>
        /// The http response
        /// </summary>
        [JsonProperty(PropertyName = "response")]
        public string Response { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

SendGrid 发送一组响应,因此请确保您的 API 处理列表。

API 中的 EmailId 是我发送到 SendGrid 的唯一参数,以便当此消息返回时我知道该消息引用的是哪封电子邮件。

希望这可以帮助