Я написал следующий бит кода, чтобы прочитать через пропущенный поток электронной почты (или .eml файл во время тестирования). < /p>
Во время теста он должен прочитать каждую строку потока, но кажется, что он просто выводит как одна строка, даже есть новая линия в потоке. PrettyPrint-Override ">
function sendMailForward($email) {
// Load recipients from ENV (comma-separated list)
$newRecipients = preg_split("/,\s*/", $_ENV['ADMIN_EMAIL'] ?? '', -1, PREG_SPLIT_NO_EMPTY);
// For StackOverflow: This passes in a comma-separated list of emails, this line is not part of the problem.
$raw_email = "";
while (!feof($email)) {
$raw_email .= fread($email, 1024);
}
fclose($email);
// Extract headers for a clean subject and sender
$headers = [];
$lines = explode("\n", $raw_email); // This line, somehow, doesn't see newlines in the stream.
foreach ($lines as $line) {
echo "New line!\n"; // This was me trying to test and what should show up multiple times, only shows up once.
if (strpos($line, ":") !== false) {
$parts = explode(":", $line, 2);
$key = trim($parts[0]);
$value = trim($parts[1]);
$headers[$key] = $value;
}
// Stop at the first blank line, which marks the end of headers
if (trim($line) == "") {
break;
}
}
$original_subject = isset($headers['Subject']) ? "FWD: " . $headers['Subject'] : "FWD: No Subject";
$original_from = isset($headers['From']) ? $headers['From'] : "unknown sender";
$subject = $original_subject;
$message = "--- Original message from $original_from ---\n\n" . $raw_email;
$extra_headers = "From: your_alias@example.com\r\n"; // Customize 'From' address
foreach ($newRecipients as $recipient) {
$to = $recipient;
// Send the email
mail($to, $subject, $message, $extra_headers);
}
}
< /code>
Что я должен изменить в приведенном выше коде? Должен ли я заставить новую строку быть добавленным? Или есть что-то еще, о чем я должен знать. Это электронное письмо, которое я использую для тестирования.
From: "Megan at TCGplayer"
To: "TCGPlayer Account"
Subject: Test Email for Forwarding
Date: Sat, 28 Sep 2025 08:00:00 -0400
Message-ID:
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
Hello,
This is a test email to verify the mail forwarding module.
Best regards,
Test System
< /code>
Изменить 2: кто-то спросил, как используется код.// Read original message from stdin
$rawEmail = file_get_contents('php://stdin');
sendMailForward($rawEmail);
Я написал следующий бит кода, чтобы прочитать через пропущенный поток электронной почты (или .eml файл во время тестирования). < /p> Во время теста он должен прочитать каждую строку потока, но кажется, что он просто выводит как одна строка, даже есть новая линия в потоке. PrettyPrint-Override ">[code]function sendMailForward($email) { // Load recipients from ENV (comma-separated list) $newRecipients = preg_split("/,\s*/", $_ENV['ADMIN_EMAIL'] ?? '', -1, PREG_SPLIT_NO_EMPTY);
// For StackOverflow: This passes in a comma-separated list of emails, this line is not part of the problem.
// Extract headers for a clean subject and sender $headers = []; $lines = explode("\n", $raw_email); // This line, somehow, doesn't see newlines in the stream. foreach ($lines as $line) { echo "New line!\n"; // This was me trying to test and what should show up multiple times, only shows up once. if (strpos($line, ":") !== false) { $parts = explode(":", $line, 2); $key = trim($parts[0]); $value = trim($parts[1]); $headers[$key] = $value; } // Stop at the first blank line, which marks the end of headers if (trim($line) == "") { break; } }
foreach ($newRecipients as $recipient) { $to = $recipient; // Send the email mail($to, $subject, $message, $extra_headers); } }
< /code> Что я должен изменить в приведенном выше коде? Должен ли я заставить новую строку быть добавленным? Или есть что-то еще, о чем я должен знать. Это электронное письмо, которое я использую для тестирования. From: "Megan at TCGplayer" To: "TCGPlayer Account" Subject: Test Email for Forwarding Date: Sat, 28 Sep 2025 08:00:00 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit
Hello,
This is a test email to verify the mail forwarding module.
Best regards, Test System < /code> Изменить 2: кто-то спросил, как используется код.// Read original message from stdin $rawEmail = file_get_contents('php://stdin');
sendMailForward($rawEmail); [/code] просто положите просто на это.