Module: TempmailSdk::Providers::Tempmail

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

Overview

tempmail.ing 渠道实现 API: https://api.tempmail.ing/api

Constant Summary collapse

CHANNEL =
"tempmail"
BASE_URL =
"https://api.tempmail.ing/api"
DEFAULT_HEADERS =
{
  "User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36",
  "Content-Type" => "application/json",
  "Referer" => "https://tempmail.ing/",
  "DNT" => "1"
}.freeze

Class Method Summary collapse

Class Method Details

.flatten(raw, recipient) ⇒ Object

将原始格式扁平化:content -> html, from_address -> from, received_at -> date



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

def flatten(raw, recipient)
  {
    "id" => raw["id"] || "",
    "from" => raw["from_address"] || raw["from"] || "",
    "to" => recipient,
    "subject" => raw["subject"] || "",
    "text" => raw["text"] || "",
    "html" => raw["content"] || raw["html"] || "",
    "date" => raw["received_at"] || raw["date"] || "",
    "is_read" => raw["is_read"] == 1 || raw["is_read"] == true,
    "attachments" => raw["attachments"] || []
  }
end

.generate_email(duration = 30) ⇒ EmailInfo

创建临时邮箱,支持自定义有效期(分钟)

Parameters:

  • duration (Integer) (defaults to: 30)

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tempmail_sdk/providers/tempmail.rb', line 23

def generate_email(duration = 30)
  resp = Http.post("#{BASE_URL}/generate", headers: DEFAULT_HEADERS, json: { "duration" => duration })
  resp.raise_for_status
  data = resp.json
  raise "Failed to generate email" unless data["success"]

  info = data["email"]
  EmailInfo.new(
    channel: CHANNEL,
    email: info["address"],
    expires_at: info["expiresAt"],
    created_at: info["createdAt"]
  )
end

.get_emails(email) ⇒ Array<Email>

获取邮件列表

Parameters:

  • email (String)

Returns:



41
42
43
44
45
46
47
48
# File 'lib/tempmail_sdk/providers/tempmail.rb', line 41

def get_emails(email)
  resp = Http.get("#{BASE_URL}/emails/#{URI.encode_www_form_component(email)}", headers: DEFAULT_HEADERS)
  resp.raise_for_status
  data = resp.json
  raise "Failed to get emails" unless data["success"]

  (data["emails"] || []).map { |raw| Normalize.normalize_email(flatten(raw, email), email) }
end