Send Attachment in PHP with OpenCart 2.1.1.1

不想你离开。 提交于 2019-11-30 22:18:28

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 );
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!