Temp Mail - Script

CREATE TABLE `temp_mailboxes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `token` varchar(64) NOT NULL,
  `created_at` datetime NOT NULL,
  `expires_at` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `token` (`token`),
  KEY `expires_at` (`expires_at`)
);

CREATE TABLE temp_emails ( id int(11) NOT NULL AUTO_INCREMENT, mailbox_id int(11) NOT NULL, sender varchar(255) DEFAULT NULL, subject varchar(255) DEFAULT NULL, body text, received_at datetime NOT NULL, is_read tinyint(1) DEFAULT 0, PRIMARY KEY (id), KEY mailbox_id (mailbox_id), FOREIGN KEY (mailbox_id) REFERENCES temp_mailboxes(id) ON DELETE CASCADE );

If you are evaluating or developing a temp mail script, look for these features:

| Component | Description | |-----------|-------------| | Email domain | A catch‑all domain (e.g., tempmail.example.com) | | Mail server | Handles incoming SMTP traffic | | Storage | Stores emails temporarily (in‑memory, Redis, or DB with TTL) | | API/UI | Interface to generate addresses & retrieve messages | | Cleanup cron | Deletes emails older than X hours | temp mail script

A Temp Mail Script is a powerful tool for privacy and development testing. Whether it is a simple API wrapper or a full-fledged SMTP server implementation, the goal remains the same: providing a short-lived, anonymous communication channel that keeps a user's real identity secure.

Important Note: For simplicity, this example uses a local email server (smtp and imap libraries) which might not be suitable for production use or scenarios requiring high deliverability. For real-world applications, consider integrating with actual email services or more sophisticated email handling solutions.

Configure your server to pipe incoming email to this script (e.g., in cPanel: Forwarder → Pipe to Program). CREATE TABLE `temp_mailboxes` ( `id` int(11) NOT NULL

#!/usr/bin/php -q
<?php
// Read raw email from STDIN
$fd = fopen("php://stdin", "r");
$rawEmail = "";
while (!feof($fd)) 
    $rawEmail .= fread($fd, 1024);
fclose($fd);

// Parse recipient (To: field) preg_match('/^To: .*<(.+?)>/m', $rawEmail, $toMatches); $toEmail = $toMatches[1] ?? ''; if (!$toEmail) exit;

// Extract local part -> find mailbox $stmt = $pdo->prepare("SELECT id FROM temp_mailboxes WHERE email = ? AND expires_at > NOW()"); $stmt->execute([$toEmail]); $mailbox = $stmt->fetch(); if (!$mailbox) exit; // expired or invalid

// Parse subject & sender preg_match('/^From: .*<(.+?)>/m', $rawEmail, $fromMatches); $sender = $fromMatches[1] ?? 'unknown'; preg_match('/^Subject: (.+?)$/m', $rawEmail, $subjectMatches); $subject = $subjectMatches[1] ?? '(no subject)'; If you are evaluating or developing a temp

// Simple body extraction (for plain text) $body = trim(substr($rawEmail, strpos($rawEmail, "\n\n") ?? 0));

$stmt = $pdo->prepare("INSERT INTO temp_emails (mailbox_id, sender, subject, body, received_at) VALUES (?, ?, ?, ?, NOW())"); $stmt->execute([$mailbox['id'], $sender, $subject, $body]); ?>