Send Attachment in PHP with OpenCart 2.1.1.1

前端 未结 1 1540
臣服心动
臣服心动 2021-01-05 23:54

Does anyone with knowledge of OpenCart 2.0.1.1 know how I could implement the following addAttachment function found in system/libary/mail.php:

public functi         


        
相关标签:
1条回答
  • 2021-01-06 00:51

    You can not directly pass file to $mail->addAttachment($this->request->post['file']);

    First you need to upload file

    //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>
    

    Now we need upload script to upload file

    //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 now you can pass attachment file to mail function

    //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 );
        }
    
    0 讨论(0)
提交回复
热议问题