Module: TempmailSdk::Providers::TempgoEmail

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

Overview

tempgo-email 渠道 — https://tempgo.email POST /api/generate 创建邮箱,GET /api/inbox 获取邮件

Constant Summary collapse

CHANNEL =
"tempgo-email"
BASE_URL =
"https://tempgo.email"
HEADERS =
{
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36",
  "Accept" => "application/json"
}.freeze

Class Method Summary collapse

Class Method Details

.generate_emailEmailInfo

创建临时邮箱

Returns:



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

def generate_email
  resp = Http.post("#{BASE_URL}/api/generate", headers: HEADERS, timeout: 15)
  resp.raise_for_status
  body = resp.json
  raise "tempgo-email: 响应格式无效" unless body.is_a?(Hash)

  address = body["email"] || ""
  mailbox_id = body["mailbox_id"] || ""
  raise "tempgo-email: 缺少邮箱地址" if address.empty?
  raise "tempgo-email: 缺少 mailbox_id" if mailbox_id.empty?

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

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

获取邮件列表

Parameters:

  • token (String)

    mailbox_id

  • email (String)

Returns:



40
41
42
43
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
# File 'lib/tempmail_sdk/providers/tempgo_email.rb', line 40

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

  address = (email || "").strip
  raise "tempgo-email: 缺少邮箱地址" if address.empty?

  resp = Http.get("#{BASE_URL}/api/inbox?email=#{URI.encode_www_form_component(address)}&mailbox_id=#{URI.encode_www_form_component(token)}",
                  headers: HEADERS, timeout: 15)
  return [] if resp.status_code == 404
  resp.raise_for_status

  body = resp.json
  return [] unless body.is_a?(Hash)

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

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

    raw = {
      "id" => msg["id"] || "",
      "from" => msg["from"] || msg["sender"] || "",
      "to" => address,
      "subject" => msg["subject"] || "",
      "text" => msg["text"] || msg["body_plain"] || "",
      "html" => msg["html"] || msg["body_html"] || "",
      "date" => (msg["date"] || msg["received_at"] || "").to_s
    }
    Normalize.normalize_email(raw, address)
  end
end