Module: TempmailSdk::Providers::Smailpro

Defined in:
lib/tempmail_sdk/providers/smailpro.rb

Overview

smailpro.com 渠道实现 两段式流程:先获取 JWT payload,再调用 api.sonjj.com 接口

Constant Summary collapse

CHANNEL =
"smailpro"
BASE_URL =
"https://smailpro.com"
API_BASE_URL =
"https://api.sonjj.com/v1/temp_email"
HEADERS =
{
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0",
  "Accept" => "application/json, text/plain, */*",
  "Referer" => "#{BASE_URL}/"
}.freeze

Class Method Summary collapse

Class Method Details

.call_api(target_url, extra = nil) ⇒ Object

携带 JWT 调用 sonjj API



35
36
37
38
39
40
# File 'lib/tempmail_sdk/providers/smailpro.rb', line 35

def call_api(target_url, extra = nil)
  payload = fetch_payload(target_url, extra)
  resp = Http.get("#{target_url}?payload=#{URI.encode_www_form_component(payload)}", headers: HEADERS)
  resp.raise_for_status
  resp.json
end

.fetch_message(email, mid) ⇒ Object

获取单封邮件正文



55
56
57
58
59
60
61
62
63
64
# File 'lib/tempmail_sdk/providers/smailpro.rb', line 55

def fetch_message(email, mid)
  return ["", ""] if mid.to_s.empty?

  data = call_api("#{API_BASE_URL}/message", { "email" => email, "mid" => mid })
  return ["", ""] unless data.is_a?(Hash)

  [(data["body"] || "").to_s, (data["textBody"] || "").to_s]
rescue StandardError
  ["", ""]
end

.fetch_payload(target_url, extra = nil) ⇒ Object

获取访问 sonjj API 所需的 JWT



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tempmail_sdk/providers/smailpro.rb', line 21

def fetch_payload(target_url, extra = nil)
  url = "#{BASE_URL}/app/payload?url=#{URI.encode_www_form_component(target_url)}"
  if extra
    extra.each { |k, v| url += "&#{URI.encode_www_form_component(k)}=#{URI.encode_www_form_component(v)}" }
  end
  resp = Http.get(url, headers: HEADERS)
  resp.raise_for_status
  payload = resp.body.strip.delete('"')
  raise "smailpro: payload 为空" if payload.empty?

  payload
end

.generate_emailEmailInfo

创建临时邮箱

Returns:



44
45
46
47
48
49
50
51
52
# File 'lib/tempmail_sdk/providers/smailpro.rb', line 44

def generate_email
  data = call_api("#{API_BASE_URL}/create")
  raise "smailpro: 创建响应格式异常" unless data.is_a?(Hash)

  email = data["email"].to_s.strip
  raise "smailpro: 创建邮箱失败" if email.empty?

  EmailInfo.new(channel: CHANNEL, email: email, token: email, expires_at: data["expired_at"])
end

.get_emails(email, token = "") ⇒ Array<Email>

获取邮件列表

Parameters:

  • email (String)
  • token (String) (defaults to: "")

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tempmail_sdk/providers/smailpro.rb', line 70

def get_emails(email, token = "")
  addr = email.to_s.strip
  raise "smailpro: 邮箱地址为空" if addr.empty?

  data = call_api("#{API_BASE_URL}/inbox", { "email" => addr })
  raise "smailpro: 邮件列表响应格式异常" unless data.is_a?(Hash)

  # API 响应: {"status": true, "data": {"messages": [...]}}
  inner = data["data"]
  messages = if inner.is_a?(Hash)
               inner["messages"]
             else
               data["messages"]
             end
  return [] unless messages.is_a?(Array) && !messages.empty?

  messages.select { |m| m.is_a?(Hash) }.map do |m|
    mid = m["mid"].to_s.strip
    html_body, text_body = fetch_message(addr, mid)
    raw = {
      "id" => mid,
      "from" => m["from"] || "",
      "to" => addr,
      "subject" => m["subject"] || "",
      "date" => m["datetime"] || ""
    }
    raw["html"] = html_body if !html_body.empty? || !text_body.empty?
    raw["text"] = text_body if !html_body.empty? || !text_body.empty?
    Normalize.normalize_email(raw, addr)
  end
end