在問我的問題之前,請允許我說我目前不是PHP開發人員。我對PHP有一點經驗,但我的背景主要是HTML5,CSS3,SASS,JS,JQuery,Basic PHP。通過HTML5和PHP中的聯繫表單發送文件
考慮到這一點,這是我的問題。目前我有一個基於html5的表單,我正在使用它來解析名稱和收件人電子郵件字段並附加文檔。
我有電子郵件發送和文檔,但無論我發送哪種文檔類型它通過作爲txt文件並且文件本身總是空的。所以我的問題是爲什麼?/我究竟做錯了什麼?
我知道我需要做的其他事情的形式,即驗證,驗證碼等,但在這個階段,我只想發送完整的文件。
下面是我到目前爲止,請原諒任何格式的問題:
HTML5
<form role="form" action="addDocument.php" method="post" onsubmit="return validateForm()">
<!-- file browse and upload-->
<div class="form-group">
<input type="file" name="file" class="file">
<div class="input-group col-xs-12">
<span class="input-group-addon"><i class="fa fa-file-image-o"></i></span>
<input type="text" class="form-control input-lg" disabled placeholder="Upload Image">
<span class="input-group-btn">
<button class="browse btn btn-primary input-lg" type="button"><i class="fa fa-search"></i> Browse</button>
</span>
</div>
</div>
<div class="form-group">
<label class="sr-only" for="c-form-1-name">Name</label>
<input id="c-form-1-name" class="c-form-1-name form-control" name="name" placeholder="Name..." type="text">
</div>
<div class="form-group">
<label class="sr-only" for="c-form-1-email">Recipient Email</label>
<input id="c-form-1-email" class="c-form-1-email form-control" name="remail" placeholder="Recipient Email..." type="text">
</div>
<button class="btn btn-primary btn-lg btn-block" type="submit">Send</button> </form>
PHP
<?php
ini_set('display_errors', 1);
$senderEmail = '<[email protected]>';
$recipientEmail = filterInput($_POST['remail']);
$message = 'You have been sent an electronic document via the Demo';
$subject = 'Demo';
echo $senderEmail.$recipientEmail.$message.$subject;
if(sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message))
{
echo "Email sent successfully!";
}
else
{
echo "Failed to send the email...";
}
function filterInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function sendEmailWithAttachments($recipientEmail,$senderEmail,$subject,$message){
if(isset($_FILES)) {
echo "iamhere";
$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","JPG","png","PNG","rtf","txt","xml");
$files = array();
foreach($_FILES as $name=>$file) {
//die("file size: ".$file['size']);
if($file['size']>=5242880)//5mb
{
$fileSize=$file['size'];
return false;
}
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
$file_type = $file['type'];
$path_parts = pathinfo($file_name);
$ext = $path_parts['extension'];
if(!in_array($ext,$allowedExtensions)) {
return false;
die("File $file_name has the extensions $ext which is not allowed");
}
//move the file to the server, cannot be skipped
$server_file="/tmp/$path_parts[basename]";
move_uploaded_file($temp_name,$server_file);
array_push($files,$server_file);
//array_push($files,$temp_name);
}
// email fields
$headers = "From: $senderEmail";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
return @mail($recipientEmail, $subject, $message, $headers);
}
}
你其實它不應該使用PHP的'mail'發送此類郵件 - 那些結構複雜,而且很容易把事情搞得一團糟。出於此目的,請使用PHPMailer或Swift Mailer等腳本。 – CBroe