Module: TempmailSdk::Providers::Tempgmailer
- Defined in:
- lib/tempmail_sdk/providers/tempgmailer.rb
Overview
TempGmailer 渠道 — https://tempgmailer.com Laravel + CSRF,POST /get-gmail 创建,POST /get-inbox 获取邮件
Constant Summary collapse
- CHANNEL =
"tempgmailer"- BASE_URL =
"https://tempgmailer.com"- CSRF_RE =
从 HTML 中提取 CSRF token
/<meta\s+name="csrf-token"\s+content="([^"]+)"/- 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", "Accept" => "application/json, text/plain, */*", "Accept-Language" => "zh-CN,zh;q=0.9,en;q=0.8", "Referer" => "#{BASE_URL}/", "X-Requested-With" => "XMLHttpRequest", "X-TempGmailer-Auth" => "frontend", "Content-Type" => "application/json" }.freeze
Class Method Summary collapse
- .extract_cookies(resp) ⇒ Object
-
.generate_email ⇒ EmailInfo
创建临时邮箱.
-
.get_emails(token, email) ⇒ Array<Email>
获取邮件列表.
- .init_session ⇒ Object
Class Method Details
.extract_cookies(resp) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/tempmail_sdk/providers/tempgmailer.rb', line 28 def (resp) = {} resp..each do |line| pair = line.split(";").first.to_s.strip k, v = pair.split("=", 2) [k] = v if k && v end end |
.generate_email ⇒ EmailInfo
创建临时邮箱
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tempmail_sdk/providers/tempgmailer.rb', line 58 def generate_email csrf_token, = init_session headers = HEADERS.merge( "X-CSRF-TOKEN" => csrf_token, "Cookie" => ) resp = Http.post("#{BASE_URL}/get-gmail", headers: headers, json: { "refresh" => true, "adblock" => 0 }) resp.raise_for_status data = resp.json raise "tempgmailer: 创建邮箱失败: #{data['message'] || '未知错误'}" unless data["success"] email = ((data["data"] || {})["email"] || "").strip raise "tempgmailer: 创建邮箱失败,未返回邮箱地址" if email.empty? token_payload = JSON.generate({ "csrfToken" => csrf_token, "cookies" => }) EmailInfo.new(channel: CHANNEL, email: email, token: token_payload) end |
.get_emails(token, email) ⇒ Array<Email>
获取邮件列表
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/tempmail_sdk/providers/tempgmailer.rb', line 83 def get_emails(token, email) session = JSON.parse(token) csrf_token = session["csrfToken"] = session["cookies"] headers = HEADERS.merge( "X-CSRF-TOKEN" => csrf_token, "Cookie" => ) resp = Http.post("#{BASE_URL}/get-inbox", headers: headers, json: { "email" => email, "adblock" => 0 }) resp.raise_for_status data = resp.json return [] unless data["success"] = (data["data"] || {})["messages"] || [] return [] unless .is_a?(Array) .filter_map do |msg| next unless msg.is_a?(Hash) from_field = msg["from"] from_field = from_field["address"] if from_field.is_a?(Hash) from_field = from_field.to_s html_content = msg["body"] || msg["intro"] || "" text_content = msg["intro"] || "" raw = { "id" => msg["id"] || "", "from" => from_field, "to" => email, "subject" => msg["subject"] || "", "text" => text_content, "html" => html_content, "date" => msg["createdAt"] || "" } Normalize.normalize_email(raw, email) end end |
.init_session ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tempmail_sdk/providers/tempgmailer.rb', line 38 def init_session resp = Http.get(BASE_URL, headers: { "User-Agent" => HEADERS["User-Agent"], "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" }, timeout: 15) resp.raise_for_status m = resp.body.match(CSRF_RE) raise "tempgmailer: 无法提取 CSRF token" unless m csrf_token = m[1] = (resp) = .map { |k, v| "#{k}=#{v}" }.join("; ") raise "tempgmailer: 未获取到 session cookie" if .empty? [csrf_token, ] end |