Module: TempmailSdk::Providers::Tempmailten

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

Overview

tempmailten.com 临时邮箱渠道(Laravel + CSRF) GET /en 获取 session cookie + CSRF token POST /messages 获取邮箱地址和邮件列表 GET /view/id 获取单封邮件 HTML 正文

Constant Summary collapse

CHANNEL =
"tempmailten"
BASE_URL =
"https://tempmailten.com"
TOK_PREFIX =
"tmt1:"
CSRF_RE =
/<meta\s+name="csrf-token"\s+content="([^"]+)"/
UA =
"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"

Class Method Summary collapse

Class Method Details

.ajax_headersObject



32
33
34
35
36
37
38
39
40
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 32

def ajax_headers
  {
    "User-Agent" => UA,
    "Accept" => "application/json, text/javascript, */*; q=0.01",
    "Accept-Language" => "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
    "X-Requested-With" => "XMLHttpRequest",
    "Referer" => "#{BASE_URL}/en"
  }
end

.browser_headersObject



24
25
26
27
28
29
30
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 24

def browser_headers
  {
    "User-Agent" => UA,
    "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
    "Accept-Language" => "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7"
  }
end

.decode_token(token) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 57

def decode_token(token)
  raise "tempmailten: 无效的 token" unless token&.start_with?(TOK_PREFIX)

  data = JSON.parse(Base64.decode64(token[TOK_PREFIX.length..]))
  csrf = (data["t"] || "").strip
  cookie_hdr = (data["c"] || "").strip
  raise "tempmailten: token 中缺少 CSRF" if csrf.empty?

  [csrf, cookie_hdr]
end

.encode_token(csrf, cookie_hdr) ⇒ Object



52
53
54
55
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 52

def encode_token(csrf, cookie_hdr)
  raw = JSON.generate({ "t" => csrf, "c" => cookie_hdr })
  "#{TOK_PREFIX}#{Base64.strict_encode64(raw)}"
end


42
43
44
45
46
47
48
49
50
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 42

def extract_cookie_header(resp)
  cookies = {}
  resp.set_cookies.each do |line|
    pair = line.split(";").first.to_s.strip
    k, v = pair.split("=", 2)
    cookies[k] = v if k && v
  end
  cookies.sort.map { |k, v| "#{k}=#{v}" }.join("; ")
end

.fetch_view(cookie_hdr, mid) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 136

def fetch_view(cookie_hdr, mid)
  return "" if mid.nil? || mid.empty?

  headers = browser_headers.merge("Referer" => "#{BASE_URL}/en")
  headers["Cookie"] = cookie_hdr unless cookie_hdr.empty?
  resp = Http.get("#{BASE_URL}/view/#{URI.encode_www_form_component(mid)}", headers: headers)
  return "" unless resp.ok?

  resp.body
rescue StandardError
  ""
end

.generate_emailEmailInfo

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 85

def generate_email
  resp = Http.get("#{BASE_URL}/en", headers: browser_headers)
  resp.raise_for_status

  cookie_hdr = extract_cookie_header(resp)
  m = resp.body.match(CSRF_RE)
  raise "tempmailten: 未能从首页提取 CSRF token" unless m

  csrf = m[1]
  data = post_messages(csrf, cookie_hdr)
  mailbox = (data["mailbox"] || "").strip
  raise "tempmailten: 邮箱地址无效" if mailbox.empty? || !mailbox.include?("@")

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

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

Parameters:

  • email (String)
  • token (String)

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 104

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

  csrf, cookie_hdr = decode_token((token || "").strip)
  data = post_messages(csrf, cookie_hdr)

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

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

    mid = (msg["id"] || "").to_s.strip
    next if mid.empty?

    from_addr = (msg["from_email"] || "").to_s
    from_name = (msg["from"] || "").to_s
    from_addr = "#{from_name} <#{from_addr}>" if !from_name.empty? && from_name != from_addr

    raw = {
      "id" => mid,
      "from" => from_addr,
      "to" => addr,
      "subject" => msg["subject"] || "",
      "html" => fetch_view(cookie_hdr, mid),
      "isRead" => !!msg["is_seen"]
    }
    Normalize.normalize_email(raw, addr)
  end
end

.post_messages(csrf, cookie_hdr) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tempmail_sdk/providers/tempmailten.rb', line 68

def post_messages(csrf, cookie_hdr)
  headers = ajax_headers.merge(
    "Content-Type" => "application/x-www-form-urlencoded",
    "X-CSRF-TOKEN" => csrf
  )
  headers["Cookie"] = cookie_hdr unless cookie_hdr.empty?

  resp = Http.post("#{BASE_URL}/messages", headers: headers,
                                            body: "_token=#{URI.encode_www_form_component(csrf)}&captcha=")
  resp.raise_for_status
  data = resp.json
  raise "tempmailten: 解析响应 JSON 失败" unless data.is_a?(Hash)

  data
end