š Source: get_email.php
<?php
session_start();
require_once 'vendor/autoload.php';
header('Content-Type: application/json');
if (!isset($_SESSION['access_token'])) {
echo json_encode(['error' => 'Not authenticated']);
exit();
}
$input = json_decode(file_get_contents('php://input'), true);
$emailId = $input['email_id'] ?? '';
if (!$emailId) {
echo json_encode(['error' => 'No email ID provided']);
exit();
}
try {
$client = new Google\Client();
$client->setAccessToken($_SESSION['access_token']);
$service = new Google\Service\Gmail($client);
$message = $service->users_messages->get('me', $emailId, ['format' => 'full']);
$payload = $message->getPayload();
function decodeBase64($data) {
return base64_decode(strtr($data, '-_', '+/'));
}
// Find and extract the body
$body = '';
if ($payload->getBody() && $payload->getBody()->getData()) {
$body = decodeBase64($payload->getBody()->getData());
}
if (empty($body) && $payload->getParts()) {
foreach ($payload->getParts() as $part) {
if ($part->getMimeType() == 'text/plain' && $part->getBody() && $part->getBody()->getData()) {
$body = decodeBase64($part->getBody()->getData());
break;
}
if ($part->getMimeType() == 'text/html' && $part->getBody() && $part->getBody()->getData()) {
$body = decodeBase64($part->getBody()->getData());
}
if ($part->getParts()) {
foreach ($part->getParts() as $subPart) {
if ($subPart->getMimeType() == 'text/plain' && $subPart->getBody() && $subPart->getBody()->getData()) {
$body = decodeBase64($subPart->getBody()->getData());
break 2;
}
if ($subPart->getMimeType() == 'text/html' && $subPart->getBody() && $subPart->getBody()->getData()) {
$body = decodeBase64($subPart->getBody()->getData());
}
}
}
}
}
// Convert HTML to text
if (strpos($body, '<') !== false) {
$body = preg_replace('/<style[^>]*>.*?<\/style>/is', '', $body);
$body = preg_replace('/<script[^>]*>.*?<\/script>/is', '', $body);
$body = preg_replace('/<br\s*\/?>/i', "\n", $body);
$body = preg_replace('/<\/p>/i', "\n", $body);
$body = preg_replace('/<\/div>/i', "\n", $body);
$body = preg_replace('/<li[^>]*>/i', "\n⢠", $body);
$body = strip_tags($body);
$body = html_entity_decode($body, ENT_QUOTES | ENT_HTML5, 'UTF-8');
}
// Remove invisible characters
$body = preg_replace('/[\x{200B}-\x{200D}\x{FEFF}]/u', '', $body);
// Remove excessive blank lines (this is the key fix)
$body = preg_replace("/[ \t]+/", ' ', $body);
$body = preg_replace("/\n[ \t]+/", "\n", $body);
$body = preg_replace("/\n{3,}/", "\n\n", $body);
// Remove lines that are just spaces or empty
$lines = explode("\n", $body);
$cleanLines = [];
foreach ($lines as $line) {
$line = trim($line);
if ($line !== '') {
$cleanLines[] = $line;
}
}
$body = implode("\n", $cleanLines);
// Remove specific footer lines
$footerLines = [
'Unsubscribe', 'Privacy', 'Terms', 'View in Browser', 'All rights reserved',
'You are receiving this email because', 'Our mailing address is',
'To unsubscribe from receiving email', 'Unsubscribe Here'
];
$lines = explode("\n", $body);
$cleanLines = [];
foreach ($lines as $line) {
$isFooter = false;
foreach ($footerLines as $footer) {
if (stripos($line, $footer) !== false) {
$isFooter = true;
break;
}
}
if (!$isFooter && strlen($line) > 3) {
$cleanLines[] = $line;
}
}
$body = implode("\n", $cleanLines);
// Remove duplicate consecutive lines (for ChatGPT email)
$lines = explode("\n", $body);
$uniqueLines = [];
$prev = '';
foreach ($lines as $line) {
if ($line !== $prev) {
$uniqueLines[] = $line;
}
$prev = $line;
}
$body = implode("\n", $uniqueLines);
// Limit size
if (strlen($body) > 4000) {
$body = substr($body, 0, 4000) . "\n\n...(truncated)";
}
if (empty($body) || strlen($body) < 10) {
$body = '(No readable content in this email)';
}
echo json_encode(['content' => $body]);
} catch (Exception $e) {
echo json_encode(['error' => $e->getMessage()]);
}
?>
ā Back