使用PEAR的多个附件的php电子邮件表单

时间:2012-08-15 20:52:33

标签: php pear

我创建了一个简单的HTML表单,我试图在提交表单后使用PEAR发送多个附件。对于PHP来说,我是一个noobie,所以我有点过头了。每次我提交表单时,我只会得到两个附件中的一个(letter_uploaded_file)。任何帮助表示赞赏。

    <?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');
}
}

2 个答案:

答案 0 :(得分:1)

嗯,首先,你只需要调用一次addAttachment:

$message->addAttachment($path_of_uploaded_file);

您需要为每个上传的文件执行一次。

另外,我建议您使用move_uploaded_file()而不是copy()将上传的文件放在最终的休息位置。

答案 1 :(得分:1)

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

$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']);

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