[英]Issue with Content-Transfer-Encoding
我在这里有问题。 我创建了一个mail()PHP脚本来发送附件,您通常会通过常规的电子邮件提供商发送邮件。 问题是我只能发送不超过一定数量字符的文件。 例如,我可以发送一个名称为“ New Text Document”的文件,但是如果我尝试发送该名称为“ New Word 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个字符,则不会发送附件
$filename = preg_replace("/[^A-Za-z0-9\.]/", "" $file['name']); ;
"Content-Disposition: attachment; filename=\"{$filename}\""
应该管用。 不是字符的数量,而是使它们混乱的奇怪字符(“(”,“)”等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.