Module: TempmailSdk::Providers::Nimail
- Defined in:
- lib/tempmail_sdk/providers/nimail.rb
Overview
nimail 渠道实现 API: https://www.nimail.cn POST 表单 API,无需认证,token 即邮箱地址本身
Constant Summary collapse
- CHANNEL =
"nimail"- BASE_URL =
"https://www.nimail.cn"
Class Method Summary collapse
-
.first(msg, *keys) ⇒ Object
按优先级从消息字典中提取首个非空值.
-
.generate_email ⇒ EmailInfo
创建 nimail.cn 临时邮箱.
-
.get_emails(email) ⇒ Array<Email>
获取邮件列表.
-
.random_local(n = 10) ⇒ Object
生成随机邮箱前缀.
-
.user_agent ⇒ Object
获取 User-Agent.
Class Method Details
.first(msg, *keys) ⇒ Object
按优先级从消息字典中提取首个非空值
26 27 28 29 30 31 32 |
# File 'lib/tempmail_sdk/providers/nimail.rb', line 26 def first(msg, *keys) keys.each do |key| val = msg[key] return val.to_s if !val.nil? && val.to_s != "" end "" end |
.generate_email ⇒ EmailInfo
创建 nimail.cn 临时邮箱
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/tempmail_sdk/providers/nimail.rb', line 36 def generate_email email = "#{random_local(10)}@nimail.cn" resp = Http.post( "#{BASE_URL}/api/applymail", headers: { "User-Agent" => user_agent, "Content-Type" => "application/x-www-form-urlencoded", "Origin" => BASE_URL, "Referer" => "#{BASE_URL}/" }, body: "mail=#{URI.encode_www_form_component(email)}" ) resp.raise_for_status data = resp.json unless data.is_a?(Hash) && data["success"] == "true" && data["user"] raise "nimail: 创建邮箱失败 #{data.inspect}" end user = data["user"].to_s EmailInfo.new(channel: CHANNEL, email: user, token: user) end |
.get_emails(email) ⇒ Array<Email>
获取邮件列表
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 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tempmail_sdk/providers/nimail.rb', line 63 def get_emails(email) addr = email.to_s.strip raise "nimail: 缺少邮箱地址" if addr.empty? resp = Http.post( "#{BASE_URL}/api/getmails", headers: { "User-Agent" => user_agent, "Content-Type" => "application/x-www-form-urlencoded", "Origin" => BASE_URL, "Referer" => "#{BASE_URL}/" }, body: "mail=#{URI.encode_www_form_component(addr)}&time=0" ) resp.raise_for_status data = resp.json return [] unless data.is_a?(Hash) && data["success"] == "true" mail = data["mail"] return [] unless mail.is_a?(Array) mail.select { |msg| msg.is_a?(Hash) }.map do |msg| row = { "id" => first(msg, "id", "time"), "from" => first(msg, "from", "sender"), "to" => addr, "subject" => first(msg, "subject", "title"), "text" => first(msg, "text", "content"), "html" => first(msg, "html", "content"), "date" => first(msg, "date", "time") } Normalize.normalize_email(row, addr) end end |
.random_local(n = 10) ⇒ Object
生成随机邮箱前缀
15 16 17 18 |
# File 'lib/tempmail_sdk/providers/nimail.rb', line 15 def random_local(n = 10) chars = ("a".."z").to_a + ("0".."9").to_a Array.new(n) { chars.sample }.join end |
.user_agent ⇒ Object
获取 User-Agent
21 22 23 |
# File 'lib/tempmail_sdk/providers/nimail.rb', line 21 def user_agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.0.0.0 Safari/537.36" end |