多输入附件 PHP
Multiple Input Attachments PHP
我正在尝试处理需要多个文件输入(在表单的不同部分)的表单,每个文件可能有多个附件。我已经将每个输入都设置成这样 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,在附加到 FormData 之前,典型的 key/value 控制台输出可能如下所示:
Files1 和 Files2 是密钥,我也需要将它们放在电子邮件中。例如:
Files1:(附上与 Files1 关联的所有文件)
Files2:(附上与 Files2 相关的所有文件)
注意:我不会提前知道键的名称,因为它们是用户定义的。
有人在附件部分建议这样做,因为它已经是 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 />";
}
}
现在表单正在静默提交,我没有看到服务器上 error_log 有任何内容。我做错了什么 - 代码更正将不胜感激。
$_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 编码,然后再将它们附加到您的电子邮件中。
我正在尝试处理需要多个文件输入(在表单的不同部分)的表单,每个文件可能有多个附件。我已经将每个输入都设置成这样 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,在附加到 FormData 之前,典型的 key/value 控制台输出可能如下所示:
Files1 和 Files2 是密钥,我也需要将它们放在电子邮件中。例如:
Files1:(附上与 Files1 关联的所有文件)
Files2:(附上与 Files2 相关的所有文件)
注意:我不会提前知道键的名称,因为它们是用户定义的。
有人在附件部分建议这样做,因为它已经是 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 />";
}
}
现在表单正在静默提交,我没有看到服务器上 error_log 有任何内容。我做错了什么 - 代码更正将不胜感激。
$_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 编码,然后再将它们附加到您的电子邮件中。