2016-04-13 23 views
-1

データベースに複数の添付ファイルを送信する必要があります。このコードでは、1つのファイルのみを送信します(ループから選択してください)。しかし、私はファイル以上を送る必要があります。この問題で私を助けてください、本当に感謝します。私に何か聞いてください。ありがとうございました。複数のファイル添付ファイルを電子メールで送信します。 smarty/php

//////////( '管理/ employeeView.tpl')フォームページ

フェッチするためのフォームページ

<form novalidate="novalidate" action="{$CMS_base_url}admin/employee_view_email.php?id={$id}" method="post" enctype="multipart/form-data"> 
    <input type="hidden" id="id" name="id" value="{$id}" /> 

    {if is_array ($cvs) && $cvs != "" } 
     <table class="table table-hover"> 
      <thead> 
       <tr> 
        <th class="col-md-2">Create date</th> 
        <th class="col-md-7">Document name/Description</th> 
        <th class="col-md-3">Select All <input type="checkbox" name="all" id="all" value="1" onclick="checkUncheckAll(this);" /></th> 
        <th class="col-md-3">Download</th> 
       </tr> 
      </thead> 
      <tbody> 

      {foreach from=$cvs key=k item=i} 
       <tr> 
        <td><em>{$i.createdAt}</em></td> 
         <td><strong>{$i.title}</a></strong></td> 
         <td><input type="checkbox" name="txt_existed_cv_attach" id="txt_existed_cv_attach" value="{$i.id}" /></td> 
         <td><a href="{$CMS_base_url}admin/employee_cv_download.php?id={$i.id}&amp;u_id={$i.employee_id}">Download</a></td> 
       </tr> 
      {/foreach} 
      {else} 
       <tr> 
        <td>{lang mkey='error' skey='noResult'}</td> 
        <td>Upload a new document to send by Admin Email {lang mkey='info' skey='fileAttachments'}</td> 
        <td colspan="2"><input class="btn-u btn-u-dark-blue" type="file" name="txt_cv_attach" id="txt_cv_attach" multiple="multiple"></td> 
       </tr> 
      </tbody> 
     {/if} 
      </table> 

    <input type="submit" name="bt_send_atach" value="SEND EMAIL TO ADMIN" class="btn-u btn-u-lg btn-u-dark-blue pull-left" /> 
</form> 

////////// PAGE

$id = (int)$_GET['id']; 
$smarty->assign('id', $id); 
$employee = Employee::find_by_id($id); 

require (CMS_LIB_PATH.DS.'class.cv.php'); 

///... more classes for employee/user to fetching the related page 

$cvs = CV::manageEmployeeCV($id); 
if (is_array($cvs) and !empty($cvs)) 
{ 
    $temp = array(); 
    $i=1; 
    foreach($cvs as $cv) 
    { 
     $showTo = lang('select','cvShowType'); 

     $temp[$i]['id']    = $cv->id; 

     $temp[$i]['name']   = $cv->originalName; 
     $temp[$i]['type']   = $cv->fileType; 
     $temp[$i]['tmp_name']  = CMS_SITE_ROOT.DS.'curriculum_vitae_file'.DS.$cv->fileName; 
     $temp[$i]['error']   = 0; 
     $temp[$i]['size']   = $cv->fileSize; 

     $temp[$i]['title']   = $cv->title; 
     $temp[$i]['description'] = $cv->description; 
     $temp[$i]['showTo']   = $showTo[$cv->showTo]; 
     $temp[$i]['defaultCV']  = $cv->defaultCV; 
     $temp[$i]['createdAt']  = strftime(dateFormat, strtotime($cv->createdAt)); 
     $temp[$i]['modifyAt']  = strftime(dateFormat, strtotime($cv->modifyAt)); 
     $temp[$i]['noViews']  = $cv->noViews; 
     $temp[$i]['employee_id'] = $cv->employeeIDFK; 
     $i++; 
    } 
    $smarty->assign('cvs', $temp); 

} 

$smarty->assign('employee', $employee); 
$smarty->assign('lang', $lang); 
$smarty->assign('message', $session->getMessage()); 
$smarty->assign('rendered_page', $smarty->fetch('admin/employeeView.tpl')); 
$smarty->display('admin/index.tpl'); 
$session->unsetMessage(); 

////////// CLASS.CV.PHP PAGE LINE

public static function getCVByEmployee($id=0, $employee_id=0) 
    { 
     global $database, $db; 
     $sql = " SELECT * FROM ". self::$table_name; 
     $sql .= " WHERE id=".$db->escape_value($id)." AND employeeIDFK=".(int)$employee_id; 
     $sql .= " LIMIT 1 "; 
     $result = self::find_by_sql($sql); 
     return !empty($result) ? array_shift($result) : false; 
    } 

//します$ db-> escape_value($番号)用のデータベースクエリに。より多くのデータベースクラスで//他のページ

public function escape_value ($string){ 
     if($this->mysqli_real_escape_string){ 
      if($this->magic_quotes_active){ $string=stripslashes($string); } 
      $string = mysqli_real_escape_string($this->connection, $string); 
     }else{ 
      if(!$this->magic_quotes_active){ $string= addslashes($string); } 
     } 
     return $string; 
    } 

////////// EMAILプロセスPAGE .....管理/ employee_view_email.php?ID = {$番号}

$id = (int)$_GET['id']; 
$smarty->assign('id', $id); 
$employee = Employee::find_by_id($id); 

///... more classes for employee/user data to send my email 

include_once CMS_LIB_PATH.DS.'class.cv.php'; 

$cvFile = $_FILES['txt_cv_attach']; 
$cv_already_existed = $_POST['txt_existed_cv_attach']; 

if($cv_already_existed != '') 
{ 

     include_once CMS_LIB_PATH.DS.'class.cv.php'; 
     $cvFile = array(); 
     $user_id = $session->getUserID(); 
     $cv_f = CV::getCVByEmployee($cv_already_existed, $id); 

     $cvFile['name']  = $cv_f->originalName; 
     $cvFile['type']  = $cv_f->fileType; 
     $cvFile['tmp_name'] = CMS_SITE_ROOT.DS.'curriculum_vitae_file'.DS.$cv_f->fileName; 
     $cvFile['error'] = 0; 
     $cvFile['size']  = $cv_f->fileSize; 

} 
else 
{ 
/* 
    * check for upload file 
*/ 
    if($cvFile['error'] == 4) 
    { 
     $errors[] = lang('error', 'noCVFileApplication'); 
    } 

    if($cvFile['error'] == 0) 
    { 
      $ext = end(explode(".", basename($cvFile['name']))); 
      $ext = strtolower($ext); 
      if(!in_array($ext, $allowed_files)) 
      { 
       $e = lang('error', 'fileNotAllowed'); 
       $e = str_replace('@[email protected]', basename($cvFile['name']) , $e); 
       $errors[] = $e; 
      } 

      if($cvFile['size'] > max_upload_cv_size) 
      { 
       $errors[] = lang('error', 'max_file_size'); 
      } 

    } 
} 


if($employee) 
{ 
    $full_name = $employee->full_name(); 
    $from = array("email" => no_reply_email, "name" => $full_name); 

    include CMS_LANGUAGE.DS.'emailTemplate.php'; 
    //send email to admin 
      $to  = array("email" => notify_email, "name" => site_name); 
      $emailTemplate = lang('email_template','notify_candidate_view_email'); 
      $subject = $emailTemplate['subject']; 

      $body = $emailTemplate['body']; 
      $body = str_replace("@[email protected]", $full_name , $body); 
      $body = str_replace("@[email protected]", $employee->firstName , $body); 
      $body = str_replace("@[email protected]", $employee->middleName, $body); 
      $body = str_replace("@[email protected]", $employee->surname, $body); 
      $body = str_replace("@[email protected]", $employee->address, $body); 
      $body = str_replace("@[email protected]", $cv_f->originalName, $body); 
      $body = str_replace("@[email protected]", $cv_f->fileType, $body);  
      $body = str_replace("@[email protected]", $user_id , $body); 

      $emailBody= array('html' => $body, 'plain' => $body); 
      $mail = sendMail($from, $to, $subject, $emailBody, $cvFile); 

    unset($_SESSION['account']); 
    $message = '<div class="alert success">'.lang('success','email_send_to_admin').'</div>'; 
} 
else 
{ 
    $message = lang('error', 'errorHead'); 
    $message .= "<ul> <li />"; 
    $message .= join(" <li /> ", $employee->errors); 
    $message .= "</ul>"; 
    $message = "<div class='alert error'>".$message."</div>"; 
} 

$smarty->assign('cvs', $temp); 
$smarty->assign('employee', $employee); 
$smarty->assign('lang', $lang); 
$session->setMessage($message); 
redirect_to(CMS_BASE_URL . 'admin/employee_view.php?id='.$id); 
exit; 

////////// Emailクラスページに

function sendMail($from='', $to='', $subject='', $message='', $cvFile='', $clFile='') 
{ 
    global $smarty; 
    if($cvFile || !empty($cvFile) || is_array($cvFile)) 
    { 
      $CV_temp_path = $cvFile['tmp_name']; 
      $CV_filename = basename($cvFile['name']); 
      $CV_type  = $cvFile['type']; 
      $CV_size  = $cvFile['size']; 
    } 

    if(mail_type == 1) 
    { 

     $mail->IsSMTP(); 
     $mail->Host  = "ukm3.siteground.biz"; 
     $mail->Port  = "465"; 
     $mail->SMTPSecure = "ssl"; 
     $mail->SMTPAuth = true; 
     $mail->Username = "admin username"; 
     $mail->Password = "password"; 
    } 

    try { 
     $mail->AddAddress($to_email, $to_name); 
     $mail->SetFrom($from_email, $from_name); //email, name 
     $mail->Sender=$from_email; 

     $mail->Subject = $subject; 
     $mail->AltBody = $plain; 
     $mail->MsgHTML($html); 

     if(!empty($cvFile) && is_array($cvFile) && !empty($CV_filename)) 
     { 
      $mail->AddAttachment($CV_temp_path, $CV_filename, "base64", $CV_type); // attachment 
     } 

     return $mail->Send(); 
    } 

    catch (phpmailerException $e) 
    { 
     echo $e->errorMessage(); //error messages from PHPMailer 
     die; 
    } 

    catch (Exception $e) 
    { 
     echo $e->getMessage(); //error messages from anything else! 
     die; 
    } 
} 

答えて

0

を送ることが(また、物事が私にはわからないことが含まれているため)すべてのコードを判断するのは難しいですが、それはここのように見えます問題は:

あなたが複数の添付ファイルを追加したい
if(!empty($cvFile) && is_array($cvFile) && !empty($CV_filename)) 
    { 
     $mail->AddAttachment($CV_temp_path, $CV_filename, "base64", $CV_type); // attachment 
    } 

、ない1

あなたは不十分という名前(配列$ cvFileを与えます。それを明確にするために、$ cvFileArrのような名前を付けてください。今は単一の名前/パスのように見えます)。

ので、私はあなたがこの(テストしていない)のように、いくつかの値をループ必要期待:

if(!empty($cvFile) && is_array($cvFile) && !empty($CV_filename)) { 
    foreach($cvFile as $oneFile){ 
     $mail->AddAttachment($CV_temp_path, $oneFile, "base64", $CV_type); // attachment 
    } 
} 

しかし、正確に伝えるのは難しいです。しかし、あなたはあなたが持っているファイルごとに$ mail-> AddAttachmentが必要になると思います。

関連する問題