我正在尝试制作一个表单,以便人们可以将多张照片上传到一封电子邮件中,我的想法是,当我选择3,4或5张照片时,照片会被发送在1封电子邮件中但是,如果我选择3张照片,我会收到3封电子邮件,每封电子邮件附带1个附件,我不知道为什么,我已经在这个问题上坚持了好几个小时了!
查看我的HTML代码
<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
<div id="drop">
Add images
<input type="file" name="photo[]" multiple accept="image/*;capture=camera">
<input type="submit" name="Submit" value="Send">
</div>
<ul>
<!-- The file uploads will be shown here -->
</ul>
</form>
这是我的PHP代码
<?php
$to = "email@email.com";
$from = "test";
$name = "test";
$subject = "test";
$message = "test";
$message = wordwrap($message,70);
foreach ($_FILES['photo']['name'] as $key => $tmp_name) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["photo"]["name"][$key]);
$extension = end($temp);
if ((($_FILES["photo"]["type"][$key] == "image/gif")
|| ($_FILES["photo"]["type"][$key] == "image/jpeg")
|| ($_FILES["photo"]["type"][$key] == "image/jpg")
|| ($_FILES["photo"]["type"][$key] == "image/pjpeg")
|| ($_FILES["photo"]["type"][$key] == "image/x-png")
|| ($_FILES["photo"]["type"][$key] == "image/png"))
&& in_array($extension, $allowedExts)) {
/* GET File Variables */
$tmpName = $_FILES['photo']['tmp_name'][$key];
$fileType = $_FILES['photo']['type'][$key];
$fileName = $_FILES['photo']['name'][$key];
/* Start of headers */
$headers = "From: $from";
if (file($tmpName)) {
/* Reading file ('rb' = read binary) */
$file = fopen($tmpName,'rb');
$data = fread($file,filesize($tmpName));
fclose($file);
/* a boundary string */
$randomVal = md5(time());
$mimeBoundary = "==Multipart_Boundary_x{$randomVal}x";
/* Header for File Attachment */
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n" ;
$headers .= " boundary=\"{$mimeBoundary}\"";
/* Multipart Boundary above message */
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mimeBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
/* Encoding file data */
$data = chunk_split(base64_encode($data));
/* Adding attchment-file to message*/
$message .= "--{$mimeBoundary}\n" .
"Content-Type: {$fileType};\n" .
" name=\"{$fileName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mimeBoundary}--\n";
}
}
}
$sent = mail($to, $subject, $message, $headers);
if ($sent) {
echo "Het bericht is met succes verstuurd!";
} else {
echo "Er is een probleem opgetreden, probeer het later nog eens.";
}
?>
我需要帮助,我已经尝试了很多不同的解决方案,但我似乎无法让它工作,也许这是Gmail或其他方面的问题?我迫切需要帮助!
答案 0 :(得分:0)
将代码拉到:
/* Start of headers */
/* a boundary string */
/* Header for File Attachment */
/* Multipart Boundary above message */
走出foreach循环 - 只需执行一次该代码。每个文件不一次。直接在foreach下面的3条线应该在foreach之外。
而不是使用fread()
使用file_get_contents()
。