Module: TempmailSdk::Providers::Tempmail365

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

Overview

Tempmail365 渠道实现 API: https://tempmail365.cn/tempemail.php

Constant Summary collapse

CHANNEL =
"tempmail365"
BASE =
"https://tempmail365.cn/tempemail.php"
FALLBACK_DOMAINS =

后备域名列表

%w[fengyou.cc shop345.com nutemail.com qvrf.cn].freeze
HEADERS =
{
  "Accept" => "application/json, text/plain, */*",
  "Cache-Control" => "no-cache",
  "DNT" => "1",
  "Referer" => "https://tempmail365.cn/",
  "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

.extract_sender(content) ⇒ Object

从 HTML 邮件内容中提取发件人



49
50
51
52
# File 'lib/tempmail_sdk/providers/tempmail365.rb', line 49

def extract_sender(content)
  m = content.match(/(?:发件人|From)\s*[::]\s*(.+?)(?:<br|<\/|<p|\n|\r)/i)
  m ? m[1].strip : ""
end

.extract_subject(content) ⇒ Object

从 HTML 邮件内容中提取主题



55
56
57
58
# File 'lib/tempmail_sdk/providers/tempmail365.rb', line 55

def extract_subject(content)
  m = content.match(/(?:主题|Subject)\s*[::]\s*(.+?)(?:<br|<\/|<p|\n|\r)/i)
  m ? m[1].strip : ""
end

.fetch_domainsObject

从 API 获取可用域名列表



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tempmail_sdk/providers/tempmail365.rb', line 27

def fetch_domains
  resp = Http.get("#{BASE}?action=get_config", headers: HEADERS, timeout: 15)
  resp.raise_for_status
  data = resp.json
  return FALLBACK_DOMAINS.dup unless data.is_a?(Hash)

  raw = data["domains"]
  return FALLBACK_DOMAINS.dup unless raw.is_a?(Array) && !raw.empty?

  out = raw.select { |d| d.is_a?(String) && !d.strip.empty? }.map(&:strip)
  out.empty? ? FALLBACK_DOMAINS.dup : out
rescue StandardError
  FALLBACK_DOMAINS.dup
end

.generate_email(domain = nil) ⇒ EmailInfo

创建临时邮箱

Parameters:

  • domain (String, nil) (defaults to: nil)

Returns:



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

def generate_email(domain = nil)
  domains = fetch_domains
  raise "tempmail365: 无可用域名" if domains.empty?

  if domain && !domain.to_s.strip.empty?
    d = domain.strip.downcase
    matched = domains.select { |x| x.downcase == d }
    raise "tempmail365: 域名不可用: #{d}" if matched.empty?

    selected = matched.first
  else
    selected = domains.sample
  end

  username = random_username
  addr = "#{username}@#{selected}"

  url = "#{BASE}?action=create_email&email=#{URI.encode_www_form_component(addr)}&domain=#{URI.encode_www_form_component(selected)}"
  resp = Http.get(url, headers: HEADERS, timeout: 15)
  resp.raise_for_status
  data = resp.json
  raise "tempmail365: 创建邮箱失败" unless data.is_a?(Hash) && data["success"]

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

.get_emails(email) ⇒ Array<Email>

获取邮件列表

Parameters:

  • email (String)

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tempmail_sdk/providers/tempmail365.rb', line 92

def get_emails(email)
  addr = email.to_s.strip
  raise "tempmail365: 邮箱地址为空" if addr.empty?

  resp = Http.get("#{BASE}?action=fetch_mail&email=#{URI.encode_www_form_component(addr)}",
                  headers: HEADERS, timeout: 15)
  resp.raise_for_status
  data = resp.json
  return [] unless data.is_a?(Hash)

  content = data["content"]
  return [] if content.nil? || !content.is_a?(String) || content.strip.empty?
  return [] if content.strip == "无邮件"

  sender = extract_sender(content)
  subject = extract_subject(content)

  raw = {
    "from" => sender,
    "subject" => subject,
    "body" => content,
    "date" => Time.now.utc.iso8601
  }
  [Normalize.normalize_email(raw, addr)]
end

.random_username(length = 8) ⇒ Object

生成随机用户名



43
44
45
46
# File 'lib/tempmail_sdk/providers/tempmail365.rb', line 43

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