Module: TempmailSdk::Providers::Mffac

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

Overview

MFFAC 渠道实现 API: https://www.mffac.com/api

Constant Summary collapse

CHANNEL =
"mffac"
BASE =
"https://www.mffac.com/api"
HEADERS =
{
  "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",
  "Content-Type" => "application/json",
  "Accept" => "*/*",
  "Origin" => "https://www.mffac.com",
  "Referer" => "https://www.mffac.com/"
}.freeze
GET_HEADERS =
HEADERS.reject { |k, _| k == "Content-Type" }.freeze

Class Method Summary collapse

Class Method Details

.fetch_detail(message_id) ⇒ Object

获取单封邮件详情



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tempmail_sdk/providers/mffac.rb', line 51

def fetch_detail(message_id)
  r = Http.get("#{BASE}/emails/#{URI.encode_www_form_component(message_id)}", headers: GET_HEADERS)
  return nil unless r.ok?

  data = r.json
  return nil unless data.is_a?(Hash) && data["success"]

  email = data["email"]
  email.is_a?(Hash) ? email : nil
rescue StandardError
  nil
end

.flatten_email(raw, recipient) ⇒ Object

扁平化邮件字段



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tempmail_sdk/providers/mffac.rb', line 36

def flatten_email(raw, recipient)
  {
    "id" => raw["id"] || "",
    "from" => raw["fromAddress"] || "",
    "to" => raw["toAddress"] || recipient,
    "subject" => raw["subject"] || "",
    "text" => raw["textContent"] || "",
    "html" => raw["htmlContent"] || "",
    "date" => received_at_to_iso(raw["receivedAt"]),
    "isRead" => raw["isRead"] || false,
    "attachments" => []
  }
end

.generate_emailEmailInfo

创建临时邮箱

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tempmail_sdk/providers/mffac.rb', line 66

def generate_email
  r = Http.post("#{BASE}/mailboxes", headers: HEADERS, json: { "expiresInHours" => 24 })
  r.raise_for_status
  data = r.json
  raise "mffac: 创建失败" unless data.is_a?(Hash) && data["success"] && data["mailbox"]

  mb = data["mailbox"]
  addr = (mb["address"] || "").strip
  mid = (mb["id"] || "").strip
  raise "mffac: 无效的邮箱数据" if addr.empty? || mid.empty?

  EmailInfo.new(channel: CHANNEL, email: "#{addr}@mffac.com", token: mid, created_at: mb["createdAt"])
end

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

获取邮件列表

Parameters:

  • email (String)
  • token (String) (defaults to: "")

Returns:



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

def get_emails(email, token = "")
  local = email.to_s.split("@", 2).first.to_s.strip
  raise "mffac: 地址为空" if local.empty?

  r = Http.get("#{BASE}/mailboxes/#{local}/emails", headers: GET_HEADERS)
  r.raise_for_status
  data = r.json
  raise "mffac: 获取列表失败" unless data.is_a?(Hash) && data["success"]

  (data["emails"] || []).select { |raw| raw.is_a?(Hash) }.map do |raw|
    mid = raw["id"].to_s.strip
    detail = mid.empty? ? nil : fetch_detail(mid)
    Normalize.normalize_email(flatten_email(detail || raw, email), email)
  end
end

.received_at_to_iso(value) ⇒ Object

将 receivedAt 秒级时间戳转为 ISO8601



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

def received_at_to_iso(value)
  seconds = Float(value)
  return "" if seconds <= 0

  Time.at(seconds).utc.strftime("%Y-%m-%dT%H:%M:%SZ")
rescue TypeError, ArgumentError
  ""
end