2015-03-04 16 views
5

Czy ktoś ze znajomością OpenCart 2.0.1.1 wiem jak mogłem realizować następujące funkcje addAttachment znaleziona w systemie/libary/mail.php:Wysłać załączników w PHP z OpenCart 2.1.1.1

public function addAttachment($filename) { 
    $this->attachments[] = $filename; 
} 

do katalogu/controller/information/contact.php - aby domyślny formularz kontaktowy mógł również zawierać funkcję przesyłania załączników? Próbowałem tego, ale nie miałem kości.

if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 
    unset($this->session->data['captcha']); 

$mail = new Mail($this->config->get('config_mail')); 
$mail->setTo($this->config->get('config_email')); 
$mail->setFrom($this->request->post['email']); 
$mail->setSender($this->request->post['name']); 
$mail->setSubject(sprintf($this->language->get('email_subject'), $this->request->post['name'])); 
$mail->setText(strip_tags($this->request->post['enquiry'])); 
$mail->addAttachment($this->request->post['file']); 
$mail->send(); 
$this->response->redirect($this->url->link('information/contact/success')); 
} 

Odpowiedz

9

nie można bezpośrednio przekazać plik $mail->addAttachment($this->request->post['file']);

najpierw trzeba przesłać plik

//catalog/view/theme/default/template/information/contact.tpl 

<div class="form-group"> 
    <label class="col-sm-2 control-label" for="input-file">File</label> 
    <div class="col-sm-10"> 
     <button type="button" id="button-upload" data-loading-text="Uploading.." class="btn btn-default btn-block"><i class="fa fa-upload"></i> <?php echo 'Upload'; ?></button> 
     <input type="hidden" name="file" value="" id="file"/> 
    </div> 
</div> 

Teraz musimy wgrać skrypt Prześlij plik

//before footer in catalog/view/theme/default/template/information/contact.tpl 
<script> 
    $('button[id^=\'button-upload\']').on('click', function() { 
     var node = this; 

     $('#form-upload').remove(); 

     $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>'); 

     $('#form-upload input[name=\'file\']').trigger('click'); 

     timer = setInterval(function() { 
      if ($('#form-upload input[name=\'file\']').val() != '') { 
       clearInterval(timer); 

       $.ajax({ 
        url: 'index.php?route=tool/upload', 
        type: 'post', 
        dataType: 'json', 
        data: new FormData($('#form-upload')[0]), 
        cache: false, 
        contentType: false, 
        processData: false, 
        beforeSend: function() { 
         $(node).button('loading'); 
        }, 
        complete: function() { 
         $(node).button('reset'); 
        }, 
        success: function(json) { 
         $('.text-danger').remove(); 

         if (json['error']) { 
          $(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>'); 
         } 

         if (json['success']) { 
          alert(json['success']); 

          $(node).parent().find('input').attr('value', json['code']); 
         } 
        }, 
        error: function(xhr, ajaxOptions, thrownError) { 
         alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText); 
        } 
       }); 
      } 
     }, 500); 
    }); 
</script> 

Finally teraz cię może przekazać plik załącznika do funkcji poczty

//catalog/controller/information/contact.php 

    if($this->request->post['file']){ 
     $this->load->model('tool/upload'); 
     $upload_info = $this->model_tool_upload->getUploadByCode($this->request->post['file']); 
     $phyname = DIR_UPLOAD.$upload_info['filename']; 
     $temp_name = DIR_UPLOAD.$upload_info['name']; 
     copy($phyname,$temp_name); 
     $mail->AddAttachment($temp_name); 
    } 

    $mail->send(); 
    if(isset($temp_name)){ 
    unlink($temp_name); 
    } 
Powiązane problemy