Module: TempmailSdk::Providers::Anonymmail

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

Overview

anonymmail.net 渠道实现 POST JSON API,需要 cookie session

Constant Summary collapse

CHANNEL =
"anonymmail"
BASE =
"https://anonymmail.net"
HEADERS =
{
  "Accept" => "*/*",
  "Content-Type" => "application/x-www-form-urlencoded",
  "Referer" => "https://anonymmail.net/",
  "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"
}.freeze

Class Method Summary collapse

Class Method Details

.fetch_domainsObject

获取可用域名列表



26
27
28
29
30
31
32
33
34
# File 'lib/tempmail_sdk/providers/anonymmail.rb', line 26

def fetch_domains
  resp = Http.post("#{BASE}/api/getDomains", headers: HEADERS, timeout: 15)
  resp.raise_for_status
  data = resp.json
  return [] unless data.is_a?(Array)

  data.select { |item| item.is_a?(Hash) && item["domain"].to_s.strip != "" }
      .map { |item| item["domain"].to_s.strip }
end

.generate_emailEmailInfo

Returns:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tempmail_sdk/providers/anonymmail.rb', line 37

def generate_email
  domains = fetch_domains
  raise "anonymmail: no domains available" if domains.empty?

  domain = domains.sample
  username = random_username
  email = "#{username}@#{domain}"

  # HEAD 请求获取 cookie session
  Http.get("#{BASE}/", headers: HEADERS, timeout: 15)

  # POST 创建邮箱
  resp = Http.post("#{BASE}/api/create", headers: HEADERS, body: "email=#{email}", timeout: 15)
  resp.raise_for_status
  data = resp.json
  raise "anonymmail: create inbox failed" unless data.is_a?(Hash) && data["success"]

  addr = data["email"].to_s.strip
  raise "anonymmail: missing email in response" if addr.empty?

  EmailInfo.new(channel: CHANNEL, email: addr)
end

.get_emails(email) ⇒ Array<Email>

Parameters:

  • email (String)

Returns:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/tempmail_sdk/providers/anonymmail.rb', line 62

def get_emails(email)
  e = email.to_s.strip
  raise "anonymmail: empty email" if e.empty?

  resp = Http.post("#{BASE}/api/get", headers: HEADERS, body: "email=#{e}", timeout: 15)
  resp.raise_for_status
  data = resp.json
  return [] unless data.is_a?(Hash)

  inbox = data[e]
  return [] unless inbox.is_a?(Hash)

  rows = inbox["emails"]
  return [] unless rows.is_a?(Array)

  rows.select { |r| r.is_a?(Hash) }.map do |raw|
    normalized = raw.dup
    # 将 token 字段映射为 id
    if normalized.key?("token") && !normalized.key?("id")
      normalized["id"] = normalized.delete("token")
    end
    # 将 body 字段映射为 html
    if normalized.key?("body") && !normalized.key?("html")
      normalized["html"] = normalized.delete("body")
    end
    Normalize.normalize_email(normalized, e)
  end
end

.random_username(length = 10) ⇒ Object



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

def random_username(length = 10)
  chars = ("a".."z").to_a + ("0".."9").to_a
  Array.new(length) { chars.sample }.join
end