Module: TempmailSdk::Providers::MailTm
- Defined in:
- lib/tempmail_sdk/providers/mail_tm.rb
Overview
mail.tm 渠道实现 API: https://api.mail.tm 流程: 获取域名 -> 生成随机邮箱/密码 -> 创建账号 -> 获取 Bearer Token
Constant Summary collapse
- CHANNEL =
"mail-tm"- BASE_URL =
"https://api.mail.tm"- DEFAULT_HEADERS =
{ "Content-Type" => "application/json", "Accept" => "application/json" }.freeze
Class Method Summary collapse
- .create_account(address, password) ⇒ Object
-
.fetch_domains ⇒ Object
获取可用域名列表,兼容 Hydra 格式和纯数组.
- .fetch_token(address, password) ⇒ Object
-
.flatten(msg, recipient) ⇒ Object
将 mail.tm 消息格式扁平化.
- .generate_email ⇒ EmailInfo
- .get_emails(email, token) ⇒ Array<Email>
- .random_string(length) ⇒ Object
Class Method Details
.create_account(address, password) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/tempmail_sdk/providers/mail_tm.rb', line 31 def create_account(address, password) resp = Http.post("#{BASE_URL}/accounts", headers: DEFAULT_HEADERS.merge("Content-Type" => "application/ld+json"), json: { "address" => address, "password" => password }) resp.raise_for_status resp.json end |
.fetch_domains ⇒ Object
获取可用域名列表,兼容 Hydra 格式和纯数组
23 24 25 26 27 28 29 |
# File 'lib/tempmail_sdk/providers/mail_tm.rb', line 23 def fetch_domains resp = Http.get("#{BASE_URL}/domains", headers: DEFAULT_HEADERS) resp.raise_for_status data = resp.json members = data.is_a?(Array) ? data : (data["hydra:member"] || []) members.select { |d| d["isActive"] }.map { |d| d["domain"] } end |
.fetch_token(address, password) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/tempmail_sdk/providers/mail_tm.rb', line 39 def fetch_token(address, password) resp = Http.post("#{BASE_URL}/token", headers: DEFAULT_HEADERS, json: { "address" => address, "password" => password }) resp.raise_for_status resp.json["token"] end |
.flatten(msg, recipient) ⇒ Object
将 mail.tm 消息格式扁平化
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/tempmail_sdk/providers/mail_tm.rb', line 82 def flatten(msg, recipient) from_addr = msg["from"].is_a?(Hash) ? msg["from"]["address"].to_s : msg["from"].to_s to_addr = recipient if msg["to"].is_a?(Array) && msg["to"][0].is_a?(Hash) to_addr = msg["to"][0]["address"] || recipient end html = msg["html"] html = html.join if html.is_a?(Array) { "id" => msg["id"] || "", "from" => from_addr, "to" => to_addr, "subject" => msg["subject"] || "", "text" => msg["text"] || "", "html" => html || "", "createdAt" => msg["createdAt"] || "", "seen" => msg["seen"] || false } end |
.generate_email ⇒ EmailInfo
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/tempmail_sdk/providers/mail_tm.rb', line 47 def generate_email domains = fetch_domains raise "No available domains" if domains.empty? address = "#{random_string(12)}@#{domains.sample}" password = random_string(16) account = create_account(address, password) token = fetch_token(address, password) EmailInfo.new(channel: CHANNEL, email: address, token: token, created_at: account["createdAt"]) end |
.get_emails(email, token) ⇒ Array<Email>
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tempmail_sdk/providers/mail_tm.rb', line 61 def get_emails(email, token) resp = Http.get("#{BASE_URL}/messages", headers: DEFAULT_HEADERS.merge("Authorization" => "Bearer #{token}")) resp.raise_for_status data = resp.json = data.is_a?(Array) ? data : (data["hydra:member"] || []) return [] if .empty? .map do |msg| flat = begin dr = Http.get("#{BASE_URL}/messages/#{msg['id']}", headers: DEFAULT_HEADERS.merge("Authorization" => "Bearer #{token}")) flatten(dr.ok? ? dr.json : msg, email) rescue StandardError flatten(msg, email) end Normalize.normalize_email(flat, email) end end |
.random_string(length) ⇒ Object
17 18 19 20 |
# File 'lib/tempmail_sdk/providers/mail_tm.rb', line 17 def random_string(length) chars = ("a".."z").to_a + ("0".."9").to_a Array.new(length) { chars.sample }.join end |