Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.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
使用PEAR的带有多模块附件的php电子邮件表单_Php_Pear - Fatal编程技术网

使用PEAR的带有多模块附件的php电子邮件表单

使用PEAR的带有多模块附件的php电子邮件表单,php,pear,Php,Pear,我创建了一个简单的HTML表单,我试图在表单提交后使用PEAR发送多个附件。说到PHP,我是个傻瓜,所以我有点不知所措。每次我提交表格时,我只会收到两个附件中的一个(letter_上传的文件)。感谢您的帮助 <?php // Pear library includes // You should have the pear lib installed include('PEAR/Mail.php'); include('PEAR/Mail/mime.php'); //Setti

我创建了一个简单的HTML表单,我试图在表单提交后使用PEAR发送多个附件。说到PHP,我是个傻瓜,所以我有点不知所措。每次我提交表格时,我只会收到两个附件中的一个(letter_上传的文件)。感谢您的帮助

    <?php 
// Pear library includes
// You should have the pear lib installed
include('PEAR/Mail.php');
include('PEAR/Mail/mime.php');

//Settings 
$max_allowed_file_size = 4096; // size in KB 
$allowed_extensions = array("pdf", "txt", "doc", "docx");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'gradysapp@gmail.com';//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{
//Get the uploaded file information
$names_of_files = array();
$names_of_files[] = basename($_FILES["resume_uploaded_file"]['name']);
$names_of_files[] = basename($_FILES["letter_uploaded_file"]['name']);

//get the file extension of the file
$type_of_uploaded_file = array();
$type_of_uploaded_file[] = basename($_FILES['resume_uploaded_file']['type']);
$type_of_uploaded_file[] = basename($_FILES['letter_uploaded_file']['type']);

$size_of_uploaded_file = array();
$size_of_uploaded_file[] = basename($_FILES["resume_uploaded_file"]["size"]/2048);
$size_of_uploaded_file[] = basename($_FILES["letter_uploaded_file"]["size"]/2048);

///------------Do Validations-------------
if(empty($_POST['name'])||empty($_POST['email']))
{
$errors .= "\n Name and Email are required fields. ";   
}
if(IsInjected($visitor_email))
{
$errors .= "\n Bad email value!";
}

if($size_of_uploaded_file > $max_allowed_file_size ) 
{
$errors .= "\n Size of file should be less than $max_allowed_file_size";
}

//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++) 
{ 
    if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
    $allowed_ext = true;        
}
}

if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Only the following file types are supported: ".implode(',',$allowed_extensions);
}

//send the email 
if(empty($errors))
{
    //copy the temp. uploaded file to uploads folder
    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
    $tmp_path = array();
    $tmp_path[] = basename($_FILES["resume_uploaded_file"]["tmp_name"]);
    $tmp_path[] = basename($_FILES["letter_uploaded_file"]["tmp_name"]);

    if(is_uploaded_file($tmp_path))
    {
    if(!move_uploaded_file($tmp_path,$path_of_uploaded_file))
    {
        $errors .= '\n error while moving the uploaded file';
    }
}

//send the email
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$phone = $_POST["phone"];
$position = $_POST["position"];
$to = $your_email;
$subject="New Job Applicant Submission";
$from = $your_email;
$text = "A user  $name has sent you this message:\n $user_message";
$text .= "Phone: " . $phone . "\n";
$text .= "Email: " . $visitor_email . "\n";
$text .= "Position: " . $position . "\n";

$message = new Mail_mime();
$message->setTXTBody($text);
$message->addAttachment($path_of_uploaded_file);
$message->addAttachment($path_of_uploaded_file);
$body = $message->get();
$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
$headers = $message->headers($extraheaders);
$mail = Mail::factory("mail");
$mail->send($to, $headers, $body);
    //redirect to 'thank-you page
    header('Location: careers_thank-you.html');
}
}

首先,您只调用了addAttachment一次:

$message->addAttachment($path_of_uploaded_file);
您需要为每个上载的文件执行一次


此外,我建议您使用move_uploaded_file()而不是copy(),将上载的文件放置在它们的最终存放位置。

您只能得到一个文件,因为您使用相同的变量来处理这两个文件:

$name_of_uploaded_file =  basename($_FILES['resume_uploaded_file']['name']);
$name_of_uploaded_file =  basename($_FILES['letter_uploaded_file']['name']);
这应该是这样的:

$names_of_files = array();
$names_of_files[] = basename($_FILES['resume_uploaded_file']['name']);
$names_of_files[] = basename($_FILES['letter_uploaded_file']['name']);

您必须在代码的其余部分传播此模式,并在附加文件时循环文件。

那么您遇到了什么问题?你收到了什么错误?每次我提交表单时,我只收到两个附件(信件上传文件)中的一个。好的,我仍然没有收到这个。每次我在用户端单击“发送”时,我都会收到一条错误消息:“上传的文件不支持文件类型。仅支持以下文件类型:pdf、doc、docx、txt”。我已验证这两个文件确实都是受支持的文件。如果需要,我可以提供代码。2件事:1。这不是检查文件类型的方式。使用$_FILE['filename']['type']获取文件的mimetype(不是扩展名),并将其与mimetype数组(同样不是扩展名)进行比较。2.不要在数组中循环并逐个检查,使用in_array()。如何编写类型数组?$type=array(“pdf”、“doc”、“docx”、“txt”);关闭,但不要将扩展名放在数组中,而是使用mimetype(请参阅)如何为此编写代码?$message->addAttachment($path_of_uploaded_file);两次?如何在这两个文件之间划界或需要划界?我是新手,正在寻求帮助。谢谢