我正在尝试使用php mail()将html <input type="file"
文档作为附件发送到php电子邮件中。
电子邮件已发送,但附件不在电子邮件中。
以下是相关的HTML:
<td> <label class="custom-file-upload">
<input type="file" onchange="fileNameC()" tabindex="28" id="fileupload" name="fileupload" required/>
<i class="fa fa-cloud-upload"></i>Upload profile</label>
</td>
<td><p id="fileNameT"></p></td>
<td><button id="remove" style="visibility: hidden" onclick="remove();">Remove</button></td>
在HTML中,我使用的是JavaScript函数fileNameC()
,下面是该函数的代码:
<script type="text/javascript">
function (){
var file = document.getElementById('fileupload').value;
if(file){
f = file.replace(/.*[\/\\]/, '');
if(confirm("Do you want to select " + f) == true){
document.getElementById("fileNameT").innerHTML = "File " + f + " attached!";
document.getElementById('remove').style.visibility='visible';fileNameC
document.getElementById('remove').style.visibility='inline';
document.getElementById('remove').innerHTML = "Remove :" + f;
}else{
document.getElementById('remove').style.visibility='hidden';
document.getElementById('remove').innerHTML = "";
}
}
}
</script>
和一个remove()
函数:
<script type="text/javascript">
function remove(){
document.getElementById('fileupload').value = "";
document.getElementById('remove').style.visibility='hidden';
document.getElementById("fileNameT").innerHTML = " ";
}
</script>
该表单的操作在php中执行:
$fileName = $_FILES["fileupload"]["name"]; // The file name
$fileTmpLoc = $_FILES["fileupload"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["fileupload"]["type"]; // The type of file it is
$fileSize = $_FILES["fileupload"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["fileupload"]["error"]; // 0 for false... and 1 for true
$temp = explode(".",$_FILES["fileupload"]["name"]);
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($fileName, "r");
$content = fread($handle, $fileSize);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
$boundary = md5("random"); // define boundary with a md5 hashed value
//header
$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version
$headers .= "From:".$from_email."\r\n"; // Sender Email
$headers .= "Reply-To: ".$reply_to_email."\r\n"; // Email addrress to reach back
$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type
$headers .= "boundary = $boundary\r\n"; //Defining the Boundary
//attachment
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=".$file_name."\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
$body .= $encoded_content; // Attaching the encoded file with email
$to_email = 'myemail@mysite.com';
$subject = 'Subject';
$body = "Email written content goes here - This part works";
//attachment
$body .= "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
$body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000, 99999)."\r\n\r\n";
$body .= $encoded_content; // Attaching the encoded file with email
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= "From: Identykidz Info <" . $from_email . ">\r\n";
//$headers .= 'Cc: myboss@example.com' . "\r\n";
//send email
mail($to_email, "$subject", $body, $headers);
我已尝试以下方法对问题进行排序,
1)我已经检查了error_log并得到了以下内容:
fopen():在第11行的/path/to/my/file/myfile.php中文件名不能为空
fread()期望参数1为资源,在第12行的/path/to/my/file/myfile.php中给出布尔值
fclose()期望参数1为资源,在第13行的/path/to/my/file/myfile.php中给出布尔值
2)然后,我运行了一个测试以回显$fileName
,以查看带到php的文件名,但没有任何显示。
3)我搜索并尝试了其他php代码来创建附件。我不走运。
php文件似乎没有接收到任何文件详细信息。我不明白原因,我已经仔细检查了所有内容,却找不到问题。
请帮助。
**编辑:
表单代码:**
<form name="register" id='register' enctype="multipart/form-data" action='PersonsEmail.php' method='post' autocomplete="off" accept-charset='UTF-8' onsubmit="displayLoader()">
<div class="row">
<div class="col-50">
<center><h3 style="font-family: Verdana; color: #1e73be;"> Person Report Form</h3></center>
<h4 style="font-family: Verdana;">Pererson's detail</h4>
<label for="fname" id="lblmpname">Person's Name and Surname:</label>
<input type='text' name='mpName' id='mpName' tabindex="1" spellcheck="false" placeholder="Person's Name and Surname" class="txt idnr" required maxlength="150" />
<label for="pname" id="lblmpage">Age:</label>
<input type='text' name='mpAge' id='mpAge' tabindex="2" class="txt" spellcheck="false" placeholder="Age of person" required maxlength="50" /><br/>
<label>Gender</label>
<select tabindex="3" name='mpgender' required>
<option value="select" selected="selected" disabled>Please select gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<label>Province</label>
<select tabindex="5" name="mpprovince" required>
<option value="select" selected="selected" disabled>Please select province</option>
<option value="Limpopo">Limpopo</option>
<option value="Gauteng">Gauteng</option>
<option value="North West">North West</option>
<option value="Northern Cape">Northern Cape</option>
<option value="Western Cape">Western Cape</option>
<option value="East Cape">East Cape</option>
<option value="Mpumalanga">Mpumalanga</option>
<option value="Kwazulu Natal">Kwazulu Natal</option>
<option value="Freestate">Freestate</option>
</select>
<br>
<hr>
<h4 style="font-family: Verdana;">Person's description:</h4>
<label>Hair colour:</label>
<p><input tabindex="13" placeholder="Hair colour of person" type="text" id="haircolour" name="haircolour"></p>
<label>Hair lenght / style:</label>
<p><input tabindex="14" placeholder="Hair lenght or style of person" type="text" id="hairlenght" name="hairlenght"></p>
<label>Eye colour:</label>
<p><input tabindex="15" placeholder="Eye colour of person" type="text" id="eyecolour" name="eyecolour"></p>
<label>Height:</label>
<p><input tabindex="16" placeholder="Height of person" type="text" id="height" name="height"></p>
<label>Weight / Build:</label>
<p><input tabindex="17" placeholder="Weight or build of the person" type="text" id="weight" name="weight"></p>
<label>Distinctive marks, tattoos or any characteristics:</label>
<p><input tabindex="18" placeholder="Distinctive marks, tattoos or any characteristics of person" type="text" id="marks" name="marks"></p>
<label for="adr" id="lblalternative">Any alternative description or information?</label>
<textarea name='alternative' id='alternative' tabindex="19" cols="60" rows="4" tabindex="6" spellcheck="false" placeholder="Optional" required="required" maxlength="124" /></textarea><br/>
<hr>
<label for="email" id="lblfamilycontactemail">Contact Person's Email</label>
<input type='text' name='familycontactemail' id='familycontactemail' spellcheck="false" tabindex="27" class="txt" placeholder="Email address of the family contact person." pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required maxlength="50" /><br/>
<br>
<hr>
<h4 style="font-family: Verdana;">Identity profile / Photo</h4>
<table>
<tr>
<td> <label id="fileload" class="custom-file-upload">
<input type="file" onchange="fileNameC()" tabindex="28" id="fileupload" name="fileupload" required/>
<i class="fa fa-cloud-upload"></i>Upload IdentyKidz profile or recent photo</label>
</td>
<td><p id="fileNameT"></p></td>
<td><button id="remove" style="visibility: hidden" onclick="remove();">Remove</button></td>
</tr>
</table>
<br>
<script type="text/javascript">
function remove(){
document.getElementById('fileupload').value = "";
document.getElementById('remove').style.visibility='hidden';
document.getElementById("fileNameT").innerHTML = " ";
}
</script>
<script type="text/javascript">
function fileCheck(){
var file = document.getElementById('fileupload').value;
if(file){
//Do nothing, because document.getElementById("fileNameT").innerHTML = was changed by function fileNameC
}
else{
alert('No File Uploaded');
}
}
</script>
<script type="text/javascript">
function fileNameC(){
var file = document.getElementById('fileupload').value;
if(file){
f = file.replace(/.*[\/\\]/, '');
if(confirm("Do you want to select " + f) == true){
document.getElementById("fileNameT").innerHTML = "File " + f + " attached!";
document.getElementById('remove').style.visibility='visible';
document.getElementById('remove').style.visibility='inline';
document.getElementById('remove').innerHTML = "Remove :" + f;
}else{
document.getElementById('remove').style.visibility='hidden';
document.getElementById('remove').innerHTML = "";
}
}
}
</script>
<br>
<br>
<input type="submit" value="Continue" id="submit" onclick="fileCheck();" tabindex="30" class="btn">
</form>