kyuurzy

TempMail

Dec 18th, 2025
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.79 KB | Source Code | 0 0
  1. // tempmail.js
  2. const fetch = require('node-fetch');
  3.  
  4. class TempMailCore {
  5.     constructor(baseUrl = 'https://api.internal.temp-mail.io/api/v3') {
  6.         this.baseUrl = baseUrl;
  7.     }
  8.  
  9.     async listDomains() {
  10.         try {
  11.             const response = await fetch(`${this.baseUrl}/domains`);
  12.             if (!response.ok) throw new Error(`HTTP ${response.status}`);
  13.             const data = await response.json();
  14.             return {
  15.                 success: true,
  16.                 domains: data.domains
  17.             };
  18.         } catch (error) {
  19.             return {
  20.                 success: false,
  21.                 error: error.message
  22.             };
  23.         }
  24.     }
  25.  
  26.     async generateEmail(username, domain) {
  27.         try {
  28.             const response = await fetch(`${this.baseUrl}/email/new`, {
  29.                 method: 'POST',
  30.                 headers: { 'Content-Type': 'application/json' },
  31.                 body: JSON.stringify({
  32.                     name: username,
  33.                     domain: domain
  34.                 })
  35.             });
  36.             if (!response.ok) throw new Error(`HTTP ${response.status}`);
  37.             const data = await response.json();
  38.             return {
  39.                 success: true,
  40.                 email: data.email,
  41.                 token: data.token
  42.             };
  43.         } catch (error) {
  44.             return {
  45.                 success: false,
  46.                 error: error.message
  47.             };
  48.         }
  49.     }
  50.  
  51.     async checkEmail(emailAddress) {
  52.         try {
  53.             const response = await fetch(`${this.baseUrl}/email/${emailAddress}/messages`);
  54.             if (!response.ok) throw new Error(`HTTP ${response.status}`);
  55.             const messages = await response.json();
  56.             return {
  57.                 success: true,
  58.                 email: emailAddress,
  59.                 messages: messages
  60.             };
  61.         } catch (error) {
  62.             return {
  63.                 success: false,
  64.                 email: emailAddress,
  65.                 error: error.message
  66.             };
  67.         }
  68.     }
  69.  
  70.     async pollEmail(emailAddress, intervalSeconds = 3, maxAttempts = 25) {
  71.         const results = [];
  72.         for (let attempt = 1; attempt <= maxAttempts; attempt++) {
  73.             const checkResult = await this.checkEmail(emailAddress);
  74.             results.push({
  75.                 attempt: attempt,
  76.                 timestamp: new Date().toISOString(),
  77.                 ...checkResult
  78.             });
  79.             if (attempt < maxAttempts) {
  80.                 await new Promise(resolve => setTimeout(resolve, intervalSeconds * 1000));
  81.             }
  82.         }
  83.         return {
  84.             success: true,
  85.             email: emailAddress,
  86.             polling_results: results
  87.         };
  88.     }
  89. }
  90.  
  91. module.exports = TempMailCore;
Advertisement
Add Comment
Please, Sign In to add comment