You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Postmark发送邮件前检查邮箱激活状态(Golang场景)

在发送前检查Postmark收件人活跃状态的解决方案

核心方法:调用Postmark的Suppressions查询API

Postmark提供了Suppressions API用于查询单个邮箱是否处于抑制(inactive)状态,你可以在发送邮件前通过这个接口做前置校验——这个接口可能你之前没注意到,它专门用来排查硬 bounce、垃圾投诉、手动加入抑制列表的收件人。

1. 接口逻辑说明

调用GET /suppressions/{email}接口(将{email}替换为目标收件人邮箱):

  • 返回200状态码:说明该邮箱在抑制列表中,属于inactive状态,禁止发送
  • 返回404状态码:说明该邮箱不在抑制列表中,处于活跃状态,可以发送

2. Golang实现示例

方式一:使用Postmark官方SDK

先安装SDK:

go get github.com/wildbit/postmark-go

编写查询代码:

package main

import (
	"fmt"
	"log"

	"github.com/wildbit/postmark-go"
)

func main() {
	// 替换为你的Postmark Server API Token
	client := postmark.NewClient("YOUR_SERVER_API_TOKEN", "")

	targetEmail := "user@example.com"
	suppression, err := client.Suppressions.Get(targetEmail)

	if err != nil {
		// 404表示邮箱不在抑制列表,可正常发送
		if apiErr, ok := err.(postmark.APIError); ok && apiErr.ErrorCode == 404 {
			fmt.Printf("邮箱 %s 状态正常,可发送邮件\n", targetEmail)
			// 这里执行你的邮件发送逻辑
		} else {
			log.Printf("查询邮箱状态失败:%v", err)
		}
		return
	}

	// 走到这里说明邮箱在抑制列表中
	fmt.Printf("邮箱 %s 处于inactive状态,抑制类型:%s\n", targetEmail, suppression.Type)
	// 跳过该收件人,不执行发送
}

方式二:直接发起HTTP请求(不依赖SDK)

如果不想引入SDK,也可以直接写HTTP请求:

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	targetEmail := "user@example.com"
	apiToken := "YOUR_SERVER_API_TOKEN"
	url := fmt.Sprintf("https://api.postmarkapp.com/suppressions/%s", targetEmail)

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Accept", "application/json")
	req.Header.Set("X-Postmark-Server-Token", apiToken)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	switch resp.StatusCode {
	case http.StatusOK:
		fmt.Printf("邮箱 %s 已被标记为inactive,禁止发送\n", targetEmail)
	case http.StatusNotFound:
		fmt.Printf("邮箱 %s 状态活跃,可发送邮件\n", targetEmail)
	default:
		log.Printf("查询失败,返回状态码:%d", resp.StatusCode)
	}
}

3. 关键注意事项

  • 权限验证:确保你的Server API Token拥有查询Suppressions的权限(默认权限已包含)
  • 速率限制:批量查询多个邮箱时,要控制请求频率,避免触发Postmark的API限流
  • 时间差风险:即使提前查询,也可能出现“查询后邮箱被标记为inactive”的情况,所以发送邮件时依然要捕获422错误,做降级处理(比如跳过该收件人、记录日志)
  • 批量优化:如果需要检查大量邮箱,建议使用POST /suppressions/list接口批量查询,减少请求次数

内容的提问来源于stack exchange,提问作者alina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 15:52:07