Module: TempmailSdk::Providers::TempMailOrg

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

Overview

temp-mail.org 渠道 — https://temp-mail.org API Base: https://web2.temp-mail.org

Constant Summary collapse

CHANNEL =
"temp-mail-org"
BASE_URL =
"https://web2.temp-mail.org"
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",
  "Origin" => "https://temp-mail.org",
  "Referer" => "https://temp-mail.org/"
}.freeze

Class Method Summary collapse

Class Method Details

.auth_headers(token) ⇒ Object



21
22
23
# File 'lib/tempmail_sdk/providers/temp_mail_org.rb', line 21

def auth_headers(token)
  HEADERS.merge("Authorization" => "Bearer #{token}")
end

.fetch_message_detail(token, msg_id) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/tempmail_sdk/providers/temp_mail_org.rb', line 78

def fetch_message_detail(token, msg_id)
  resp = Http.get("#{BASE_URL}/messages/#{msg_id}", headers: auth_headers(token), timeout: 15)
  return {} unless resp.ok?

  data = resp.json
  data.is_a?(Hash) ? data : {}
rescue StandardError
  {}
end

.generate_emailEmailInfo

创建临时邮箱

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tempmail_sdk/providers/temp_mail_org.rb', line 27

def generate_email
  resp = Http.post("#{BASE_URL}/mailbox", headers: HEADERS, timeout: 15)
  resp.raise_for_status
  data = resp.json
  raise "temp-mail-org: 响应格式无效" unless data.is_a?(Hash)

  token = data["token"] || ""
  mailbox = data["mailbox"] || ""
  raise "temp-mail-org: 创建邮箱失败" if token.empty? || mailbox.empty?

  EmailInfo.new(channel: CHANNEL, email: mailbox, token: token)
end

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

获取邮件列表

Parameters:

  • token (String)

    JWT 令牌

  • email (String)

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tempmail_sdk/providers/temp_mail_org.rb', line 44

def get_emails(token, email)
  raise "temp-mail-org: token 不能为空" if token.nil? || token.empty?

  addr = (email || "").strip
  raise "temp-mail-org: 邮箱地址不能为空" if addr.empty?

  resp = Http.get("#{BASE_URL}/messages", headers: auth_headers(token), timeout: 15)
  resp.raise_for_status
  data = resp.json
  return [] unless data.is_a?(Hash)

  messages = data["messages"]
  return [] unless messages.is_a?(Array) && !messages.empty?

  messages.filter_map do |msg|
    next unless msg.is_a?(Hash)

    msg_id = msg["_id"] || ""
    next if msg_id.empty?

    detail = fetch_message_detail(token, msg_id)
    raw = {
      "id" => msg_id,
      "from" => detail["from"] || msg["from"] || "",
      "to" => addr,
      "subject" => detail["subject"] || msg["subject"] || "",
      "text" => detail["bodyPreview"] || msg["bodyPreview"] || "",
      "html" => detail["bodyHtml"] || "",
      "date" => detail["createdAt"] || msg["receivedAt"].to_s
    }
    Normalize.normalize_email(raw, addr)
  end
end