Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-将多个文件附加到电子邮件,附加的文件为空?!!?_Php_Email_Attachment - Fatal编程技术网

PHP-将多个文件附加到电子邮件,附加的文件为空?!!?

PHP-将多个文件附加到电子邮件,附加的文件为空?!!?,php,email,attachment,Php,Email,Attachment,这只是一个示例代码,本质上我有一个表单,其中有4个文件要上传。文件已成功保存在我的服务器上的指定文件夹中。然而,我显然没有正确地连接它们,因为它们是空的……有人能帮忙吗 此外,附件显示的是目录名而不是文件名…我缺少什么 非常感谢 <?php $username= protect($_POST['username']); $firstName= protect($_POST['firstName']); $lastName= protect($_POST['lastName']); $

这只是一个示例代码,本质上我有一个表单,其中有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.0\n";
    $headers .= "Content-Type: multipart/mixed;\n"; 
    $headers .= " boundary=\"{$mime_boundary}\"";

    $message ="\n\n--{$mime_boundary}\n";
    $message .="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .="Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n"; 
    $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: base64\n\n" . $data . "\n\n";

$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>"; 
    } 
}

}
?> 


我知道这不是你帖子的答案,但在过去与此类问题斗争之后,我放弃了,并使用了它。如果你还想自己动手,你应该看看它的源代码。

不要使用邮件()使用邮件库。我认为停止用错误的方法,开始用正确的方法是一个很好的答案。