簡體   English   中英

使用PEAR的多重附件的php電子郵件表單

[英]php email form with multipule attachments using 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');
}
}

好吧,首先,你只需要調用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']);

您必須在其余代碼中傳播此模式,並在附加文件時循環遍歷這些文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM