<?php
declare(strict_types=1);
$cases = [];
// Case 1: mail() header construction, subject newline stripping, and 70-char line wrapping
$to = 'someone@example.com';
$rawSubject = "参数邮件\r\n通知";
$safeSubject = preg_replace('/[\r\n]+/', ' ', $rawSubject);
$rawMessage = 'Hello! This is a long demonstration email body line exceeding seventy ASCII characters for RFC line wrapping.';
$wrappedMessage = wordwrap($rawMessage, 70, "\n", true);
$from = 'someonelse@example.com';
$headers = implode("\r\n", [
'From: ' . $from,
'Reply-To: ' . $from,
'Content-Type: text/plain; charset=UTF-8',
]);
$lines = explode("\n", $wrappedMessage);
$maxLineLen = max(array_map('strlen', $lines));
$headerLines = explode("\r\n", $headers);
$actual1 = [
'to_valid' => filter_var($to, FILTER_VALIDATE_EMAIL) !== false,
'subject_has_newline' => preg_match('/[\r\n]/', $safeSubject) === 1,
'safe_subject' => $safeSubject,
'wrapped_line_count' => count($lines),
'max_line_len_le_70' => $maxLineLen <= 70,
'header_count' => count($headerLines),
'first_header' => $headerLines[0],
];
$expected1 = [
'to_valid' => true,
'subject_has_newline' => false,
'safe_subject' => '参数邮件 通知',
'wrapped_line_count' => 2,
'max_line_len_le_70' => true,
'header_count' => 3,
'first_header' => 'From: someonelse@example.com',
];
$cases[] = [
'name' => 'mail_header_and_linewrap',
'passed' => $actual1 === $expected1,
'expected' => $expected1,
'actual' => $actual1,
];
// Case 2: feedback form branch logic (GET initial render vs POST submission)
$renderOrQueue = static function (array $request): array {
if (isset($request['email'])) {
$email = (string)$request['email'];
$subject = (string)($request['subject'] ?? '');
$message = (string)($request['message'] ?? '');
return [
'mode' => 'submitted',
'payload' => [
'to' => 'someone@example.com',
'subject' => $subject,
'message' => $message,
'from_header' => 'From:' . $email,
],
'notice' => '邮件发送成功',
];
}
$html = "<form method='post' action='mailform.php'>"
. "Email: <input name='email' type='text'>"
. "Subject: <input name='subject' type='text'>"
. "Message: <textarea name='message'></textarea>"
. "<input type='submit'></form>";
return [
'mode' => 'form',
'has_post_form' => str_contains($html, "method='post'") && str_contains($html, "name='email'"),
];
};
$initialState = $renderOrQueue([]);
$submittedState = $renderOrQueue([
'email' => 'visitor@example.com',
'subject' => '网站反馈',
'message' => '希望增加更多示例。',
]);
$actual2 = [
'initial_mode' => $initialState['mode'],
'has_post_form' => $initialState['has_post_form'],
'submitted_mode' => $submittedState['mode'],
'from_header' => $submittedState['payload']['from_header'],
'notice' => $submittedState['notice'],
];
$expected2 = [
'initial_mode' => 'form',
'has_post_form' => true,
'submitted_mode' => 'submitted',
'from_header' => 'From:visitor@example.com',
'notice' => '邮件发送成功',
];
$cases[] = [
'name' => 'mail_form_branch_logic',
'passed' => $actual2 === $expected2,
'expected' => $expected2,
'actual' => $actual2,
];
// Case 3: mail() availability and optional fifth parameter (-f envelope sender) formatting
$envelopeSender = 'bounce@example.com';
$validEnvelope = filter_var($envelopeSender, FILTER_VALIDATE_EMAIL);
$additionalParam = $validEnvelope !== false ? '-f' . $validEnvelope : '';
$actual3 = [
'mail_function_exists' => function_exists('mail'),
'smtp_ini_readable' => ini_get('SMTP') !== false,
'smtp_port_numeric' => ctype_digit((string)ini_get('smtp_port')),
'additional_param' => $additionalParam,
];
$expected3 = [
'mail_function_exists' => true,
'smtp_ini_readable' => true,
'smtp_port_numeric' => true,
'additional_param' => '-fbounce@example.com',
];
$cases[] = [
'name' => 'mail_parameters_and_mta_config',
'passed' => $actual3 === $expected3,
'expected' => $expected3,
'actual' => $actual3,
];
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-mail-lab/explore-mail.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-mail.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1263.zip