[英]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.