我正在尝试处理需要多个文件输入的表单(在表单的不同部分),每个表单可能有多个附件。我已经设置了这样的每个输入w /同名(附加),据我所知,$ _FILES ['attach']将获取所有文件,对吗?
<input type="file" class="file" name="attach[]" title="" dropzone="copy" multiple="multiple" required></input>
我从很多来源拼凑了这个php,你能不能回顾一下。它没有发送,所以很明显我在某个地方犯了一些错误。
(other variables not shown, but sanitized as well)
$attachments = $_FILES['attach'];
// email body
$mes_body = "<html><head></head><body>";
// $mes_body .= "<img src='http://www.domain.com/marketing/image.png' alt='Image' /><br /><br />"; (optional header image)
// Sanitize unknown input data
function sanitize(text) {
$san = filter_var(text, FILTER_SANITIZE_STRING);
return $san;
}
$mes_body .= "<br /><br />";
foreach ($_POST as $key => $value) { //define & insert unknown inputs into message body
if ($key != 'From' && $key != 'Subject' && $key != 'Filecheck') {
$mes_body .= sanitize($key) . ': ' . sanitize($value) . '<br />';
}
}
$mes_body .= "</body></html>";
// multipart email
$boundary = md5("text_for_hash");
if ($filecheck == 1) { // multipart email w/ attachment(s) & $filecheck=1 means files present
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: " . $from_email . "\r\n";
$headers .= "Reply-To: " . $from_email . "" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
// message text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/html; charset=\"ISO-8859-1\"\r\n";
$body .= "Content-Transfer-Encoding: 8bit\r\n";
$body .= $mes_body;
$body .= "boundary = $boundary\r\n";
// attachment(s)
foreach ($attachments as $attachment) {
if ($attachment['size']) {
$body .= "\r\n" . $boundary;
$body .= "Content-Type: " . $attachment['type'] . "; name=\"". $attachment['filename'] . "\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= $attachment['content'];
}
}
$body .= "boundary = $boundary\r\n";
$message_body = $mes_body;
}
else { // plain text email
$headers = "From: " . $from_email . "\r\n" . "Reply-To: " . $from_email . "\r\n" . "Content-type: text/html" . "\r\n" . "X-Mailer: PHP/" . phpversion();
$message_body = $mes_body;
}
$send_mail = mail($to_email, $subject, $message_body, $headers);
更新: 专注于附件部分。我正在使用FormData&amp;在附加到FormData之前,典型的键/值控制台输出可能如下所示:
Files1&amp; Files2是密钥&amp;我也需要将它们放在电子邮件中。例如:
Files1 :(附上与Files1关联的所有文件)
Files2 :(附上与文件2相关的所有文件)
注意:我不会提前知道密钥的名称,因为它们是用户定义的。
有人建议将此作为附件部分,因为它已经是FormData:
// attachment(s)
foreach ($_FILES as $attachment) {
$name = $attachment["tmp_name"];
if ($attachment["size"]) {
$body .= "--$boundary\r\n";
$body .= "Content-Type: " . $attachment["type"] . "; name=\"" . $name . "\"; Content-Disposition: attachment" . "\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
echo $attachment . "<br />";
}
}
现在表格正在静默提交&amp;我没有在服务器上看到任何错误。我做错了什么 - 代码修正将不胜感激。
答案 0 :(得分:1)
$_FILES
是一个二维数组。第一个索引是文件上载到的字段名称,第二个索引是有关文件的信息。而不是以下循环:
foreach ($attachments as $attachment) {
if ($attachment['size']) {
$body .= "\r\n" . $boundary;
$body .= "Content-Type: " . $attachment['type'] . "; name=\"". $attachment['filename'] . "\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= $attachment['content'];
}
}
($attachments
只是你的第一个附件,而不是所有附件),你可能想尝试:
foreach ($_FILES as $attachment) {
if ($attachment['size']) {
$body .= "\r\n" . $boundary;
$body .= "Content-Type: " . $attachment['type'] . "; name=\"". $attachment['filename'] . "\"\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= base64_encode(file_get_contents($attachment['tmp_name']));
}
}
这将遍历所有附件,读入文件内容并对其进行base64编码,然后再附加到您的电子邮件中。