附加多个文件到电子邮件,附加的文件是空白的吗?!



这只是一个示例代码,基本上我有一个表单与4个文件要上传。文件已成功保存在服务器上的指定文件夹中。然而,我显然没有附上他们正确,因为他们是空的....有人能帮忙吗?

另外,附件显示的是目录名而不是文件名…我错过了什么?

多谢! !

<?php
$username= protect($_POST['username']); 
$firstName= protect($_POST['firstName']); 
$lastName= protect($_POST['lastName']); 
$email= protect($_POST['email']); 
$program= protect($_POST['program']); 
$dob= protect($_POST['dob']); 
$transcript= $_FILES['transcript']; 
$resume= $_FILES['resume']; 
$letterIntent= protect($_POST['letterIntent']); 
$reference1= $_FILES['reference1']; 
$reference2= $_FILES['reference2']; 
$relevantExperience= protect($_POST['relevantExperience']); 
$volunteerExperience= protect($_POST['volunteerExperience']); 
$reasonsAbroad= protect($_POST['reasonsAbroad']); 
$definitionSuccess= protect($_POST['definitionSuccess']); 
$futureGoals= protect($_POST['futureGoals']); 
$challengeYourself= protect($_POST['challengeYourself']); 
$challengeDevelopment= protect($_POST['challengeDevelopment']); 

if (isset($_FILES) && (bool) $_FILES) {
$allowed_ext = array('doc', 'pdf', 'txt', '');
$files = array();
foreach($_FILES as $name=>$file) {
    $file_name = $file['name'];
    $file_tmp = $file['tmp_name'];
    $file_size = $file['size'];
    $path_parts = pathinfo($file_name);
    $file_ext = $path_parts['extension'];
    if(!in_array($file_ext, $allowed_ext)) {
        die ("extension for file $file_name not allowed");
    }
    if($file_size > 2097152) {
        die ("File size must be under 2MB");
    }
    $server_file = "wp-content/uploads/application-documents";
    move_uploaded_file($file_tmp, "$server_file/$file_name");
    array_push($files, $server_file);
}
    $to = ".....@gmail.com";
    $from = "$email"; 
    $subject ="Application from $firstName $lastName with attachments"; 
    $msg = "hello";
    $headers = "From: $from";
    //define email boundaries
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    $headers .= "nMIME-Version: 1.0n";
    $headers .= "Content-Type: multipart/mixed;n"; 
    $headers .= " boundary="{$mime_boundary}"";
    $message ="nn--{$mime_boundary}n";
    $message .="Content-Type: text/plain; charset="iso-8859-1"n";
    $message .="Content-Transfer-Encoding: 7bitnn" . $msg . "nn"; 
    $message .= "--{$mime_boundary}n";
foreach($files as $file) {
$aFile = fopen($file,"rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {"application/octet-stream"};n";
$message .= " name="$file"n";
$message .= "Content-Disposition: attachment;n";
$message .= " filename="$file"n";
$message .= "Content-Transfer-Encoding: base64nn" . $data . "nn";
$message .= "--{$mime_boundary}n";
}
$ok = @mail($to, $subject, $message, $headers); 
    if ($ok) { 
        echo "<p>mail sent to $to</p>"; 
    } else { 
        echo "<p>mail could not be sent!</p>"; 
    } 
}
}
?> 

我知道这不是你帖子的答案,但是在过去与这种问题斗争之后,我放弃了并使用PHPMailer。如果您仍然想要运行自己的脚本,您应该看看它的源代码。

相关内容

  • 没有找到相关文章

最新更新