Module: TempmailSdk::Providers::Tempfastmail

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

Overview

TempFastMail 渠道 — https://tempfastmail.com

Constant Summary collapse

CHANNEL =
"tempfastmail"
BASE_URL =
"https://tempfastmail.com"
HEADERS =
{ "Accept" => "application/json", "User-Agent" => "Mozilla/5.0" }.freeze

Class Method Summary collapse

Class Method Details

.flatten(raw, recipient) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tempmail_sdk/providers/tempfastmail.rb', line 15

def flatten(raw, recipient)
  {
    "id" => raw["uuid"],
    "from" => raw["from"],
    "to" => raw["real_to"] || recipient,
    "subject" => raw["subject"],
    "html" => raw["html"],
    "date" => raw["received_at"],
    "attachments" => raw["attachments"]
  }
end

.generate_emailEmailInfo

创建临时邮箱

Returns:



29
30
31
32
33
34
35
36
37
38
# File 'lib/tempmail_sdk/providers/tempfastmail.rb', line 29

def generate_email
  resp = Http.post("#{BASE_URL}/api/email-box", headers: HEADERS)
  resp.raise_for_status
  data = resp.json
  email = (data["email"] || "").strip
  uuid = (data["uuid"] || "").strip
  raise "tempfastmail: 创建邮箱响应无效" if email.empty? || !email.include?("@") || uuid.empty?

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

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

获取邮件列表

Parameters:

  • token (String)

    UUID

  • email (String)

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tempmail_sdk/providers/tempfastmail.rb', line 44

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

  resp = Http.get("#{BASE_URL}/api/email-box/#{URI.encode_www_form_component(uuid)}/emails",
                  headers: HEADERS)
  resp.raise_for_status
  rows = resp.json
  return [] unless rows.is_a?(Array)

  rows.filter_map do |row|
    raw = row.is_a?(Hash) ? row : {}
    message_id = (raw["uuid"] || "").strip
    unless message_id.empty?
      begin
        dr = Http.get("#{BASE_URL}/api/email-box/#{URI.encode_www_form_component(uuid)}/email/#{URI.encode_www_form_component(message_id)}",
                      headers: HEADERS)
        if dr.ok?
          detail = dr.json
          raw = detail if detail.is_a?(Hash)
        end
      rescue StandardError
        # 忽略
      end
    end
    Normalize.normalize_email(flatten(raw, address), address)
  end
end