Module: TempmailSdk::Providers::TempMailNow

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

Overview

Temp-Mail.Now 渠道 — https://temp-mail.now 基于会话 Cookie 的临时邮箱服务

Constant Summary collapse

CHANNEL =
"temp-mail-now"
BASE_URL =
"https://temp-mail.now"
PAGE_HEADERS =
{
  "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}.freeze
API_HEADERS =
{
  "Accept" => "application/json, text/javascript, */*; q=0.01",
  "X-Requested-With" => "XMLHttpRequest",
  "Referer" => "#{BASE_URL}/en/",
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
}.freeze

Class Method Summary collapse

Class Method Details

.cookies_to_str(cookies) ⇒ Object



35
36
37
# File 'lib/tempmail_sdk/providers/temp_mail_now.rb', line 35

def cookies_to_str(cookies)
  cookies.map { |k, v| "#{k}=#{v}" }.join("; ")
end

.extract_cookies(resp) ⇒ Object



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

def extract_cookies(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
end

.generate_emailEmailInfo

创建临时邮箱

Returns:



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

def generate_email
  # 获取 session cookie
  resp = Http.get("#{BASE_URL}/en/", headers: PAGE_HEADERS, timeout: 15)
  resp.raise_for_status
  cookies = extract_cookies(resp)
  cookie_str = cookies_to_str(cookies)
  raise "temp-mail-now: 无法获取 session cookie" if cookie_str.empty?

  # 创建新邮箱
  resp2 = Http.post("#{BASE_URL}/change_email",
                    headers: API_HEADERS.merge("Cookie" => cookie_str), timeout: 15)
  resp2.raise_for_status

  # 合并可能更新的 cookie
  extract_cookies(resp2).each { |k, v| cookies[k] = v }
  cookie_str = cookies_to_str(cookies)

  data = resp2.json
  raise "temp-mail-now: 响应格式无效" unless data.is_a?(Hash)

  address = data["new_email"] || ""
  raise "temp-mail-now: 创建邮箱失败" if address.empty? || !address.include?("@")

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

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

获取邮件列表

Parameters:

  • token (String)

    session cookie

  • email (String)

Returns:



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/temp_mail_now.rb', line 71

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

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

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

  emails_list = data["emails"]
  return [] unless emails_list.is_a?(Array)

  emails_list.filter_map do |item|
    next unless item.is_a?(Hash)

    raw = {
      "id" => item["id"] || "",
      "from" => item["from"] || item["from_address"] || item["sender"] || "",
      "to" => item["to"] || addr,
      "subject" => item["subject"] || "",
      "text" => item["text"] || item["body_text"] || "",
      "html" => item["html"] || item["body_html"] || "",
      "date" => item["date"] || item["received_at"] || ""
    }
    Normalize.normalize_email(raw, addr)
  end
end