我这里有问题。我创建了一个 mail() PHP 脚本来发送附件,您通常会通过常规电子邮件提供商发送。问题是我只能发送不超过一定字符数的文件。例如,我可以发送一个名为“New Text Document”的文件,但如果我尝试发送一个名为“New Document of Microsoft Word (3)”的文件,它永远不会收到我的电子邮件。
有人可以告诉我为什么会这样吗?
error_reporting(-1);
if(empty($_POST) === false){
$errors = array();
$name = $_POST['name'];
$email = $_POST['email'];
$file = $_FILES['filename'];
if(empty($name) === true || empty($email) === true || empty($_POST['message']) === true){
$errors[] = 'Name, email and message are required!';
} else {
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$errors[] ='Not a valid email';
}
if(ctype_alpha($name) === false){
$errors[] ='Name must only contain letters';
}
}
$message;
if(empty($errors) === true){
if($_FILES['filename']['error'] == UPLOAD_ERR_OK){
$boundary = '-----' . md5(rand()) . '---';
$headers = array(
'MIME-Version: 1.0',
"Content-type: multipart/mixed; boundary=\"{$boundary}\"",
"From: {$email}"
);
$message = array(
"--{$boundary}",
'Content-type: text/html',
'Content-Transfer-Encoding: 7bit',
'',
chunk_split($_POST['message']),
"--{$boundary}",
"Content-type: {$file['type']}; name=\"{$file['name']}\"",
"Content-Disposition: attachment; filename=\"{$file['name']}\"",
'Content-Transfer-Encoding: base64',
'',
chunk_split(base64_encode(file_get_contents($file['tmp_name']))),
"--{$boundary}--"
);
$message = implode("\r\n", $message);
} else {
$headers = array(
"From: {$email}"
);
$message = &$_POST['message'];
}
//send email
var_dump(mail($email, 'Contact form', $message, implode("\r\n", $headers)));
echo $_FILES['filename']['name'];
/*//redirect user
header('Location: index.php?sent');
exit();*/
}
}
?
<!doctype html>
<html>
<head>
<title>A contact form</title>
</head>
<body>
<?php
if(isset($_GET['sent']) === true){
echo '<p>Thanks for contacting us</p>';
} else {
if(empty($errors) === false){
echo '<ul>';
foreach($errors as $error){
echo '<li>', $error ,'</li>';
}
echo '</ul>';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<p>
<label for="name">Name:</label><br />
<input type="text" name="name" id="name" <?php if(isset($_POST['name']) === true){ echo 'value="', strip_tags($_POST['name']), '"';} ?>>
</p>
<p>
<label for="email">Email:</label><br />
<input type="text" name="email" id="email" <?php if(isset($_POST['email']) === true){ echo 'value="', strip_tags($_POST['email']), '"';} ?>>
</p>
<p>
<label for="message">Message:</label><br />
<textarea name="message" id="message"><?php if(isset($_POST['message']) === true){ echo strip_tags($_POST['message']); } ?></textarea>
</p>
<p>
List of files allowed: .pdf, .odt, .doc(x), xls(x), ppt(x), >xps, xml
</p>
<p>
<input type="file" name="filename">
</p>
<p>
<input type="submit" value="submit">
</p>
<?php
}
?>
</form>
</body>
</html>
它发送附件,但仅当文件具有例如文件名称为 10 个字符时,但如果它有 15 个字符,则不会发送