Module: TempmailSdk::Providers::TenMinuteMailNet

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

Overview

10minutemail.net 临时邮箱渠道 GET /address.api.php 创建邮箱(获取 PHPSESSID) GET /address.api.php 获取邮件列表(带 session cookie) GET /mail.api.php?mailid=id 获取单封邮件详情

Constant Summary collapse

CHANNEL =
"10minutemail-net"
BASE_URL =
"https://10minutemail.net"
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

.fetch_detail(headers, mail_id) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/tempmail_sdk/providers/ten_minute_mail_net.rb', line 115

def fetch_detail(headers, mail_id)
  resp = Http.get("#{BASE_URL}/mail.api.php?mailid=#{URI.encode_www_form_component(mail_id)}",
                  headers: headers, timeout: 15)
  resp.raise_for_status
  resp.json
rescue StandardError
  nil
end

.generate_emailEmailInfo

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tempmail_sdk/providers/ten_minute_mail_net.rb', line 24

def generate_email
  resp = Http.get("#{BASE_URL}/address.api.php", headers: HEADERS, timeout: 15)
  resp.raise_for_status
  data = resp.json
  raise "10minutemail-net: 响应格式无效" unless data.is_a?(Hash)

  address = (data["mail_get_mail"] || "").strip
  raise "10minutemail-net: 创建邮箱失败" if address.empty?

  phpsessid = resp.cookie_value("PHPSESSID")
  raise "10minutemail-net: 未获取到 PHPSESSID cookie" if phpsessid.nil? || phpsessid.empty?

  token = JSON.generate({ "cookie" => "PHPSESSID=#{phpsessid}" })

  expires_at = nil
  duetime = data["mail_get_duetime"]
  if duetime
    begin
      expires_at = duetime.to_i * 1000
    rescue StandardError
      # 忽略
    end
  end

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

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

Parameters:

  • token (String)
  • email (String)

Returns:



54
55
56
57
58
59
60
61
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tempmail_sdk/providers/ten_minute_mail_net.rb', line 54

def get_emails(token, email)
  address = (email || "").strip
  raise "10minutemail-net: 邮箱地址为空" if address.empty?

  token_data = JSON.parse(token)
  cookie_str = token_data["cookie"] || ""

  headers = HEADERS.merge("Cookie" => cookie_str)

  resp = Http.get("#{BASE_URL}/address.api.php", headers: headers, timeout: 15)
  resp.raise_for_status
  data = resp.json

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

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

    mail_id = (item["mail_id"] || "").to_s.strip
    next if mail_id.empty? || mail_id == "welcome"

    detail = fetch_detail(headers, mail_id)
    next unless detail.is_a?(Hash)

    html_body = ""
    text_body = ""
    body_list = detail["body"]
    if body_list.is_a?(Array)
      body_list.each do |part|
        next unless part.is_a?(Hash)

        content_type = part["content"] || ""
        if content_type.include?("text/html")
          html_body = part["body"] || ""
        elsif content_type.include?("text/plain")
          text_body = part["body"] || ""
        end
      end
    end

    if text_body.empty?
      plain_list = detail["plain"]
      if plain_list.is_a?(Array) && !plain_list.empty? && plain_list[0].is_a?(String)
        text_body = plain_list[0]
      end
    end

    raw = {
      "id" => mail_id,
      "from" => (detail["from"] || item["from"] || "").to_s,
      "to" => (detail["to"] || address).to_s,
      "subject" => (detail["subject"] || item["subject"] || "").to_s,
      "text" => text_body,
      "html" => html_body,
      "date" => (detail["datetime"] || item["datetime"] || "").to_s
    }
    Normalize.normalize_email(raw, address)
  end
end