使用OpenCart 2.1.1.1用PHP发送附件 [英] Send Attachment in PHP with OpenCart 2.1.1.1

查看:76
本文介绍了使用OpenCart 2.1.1.1用PHP发送附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何了解OpenCart 2.0.1.1的人都知道我如何实现system/libary/mail.php中的以下addAttachment函数:

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 function addAttachment($filename) {
  $this->attachments[] = $filename;
}

进入目录/控制器/信息/contact.php-这样默认的联系表还可以包含附件上传功能吗?我尝试过但没有骰子.

into catalog/controller/information/contact.php - so that the default contact form can also include an attachment upload feature? I tried this but no dice.

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'));
}

推荐答案

您不能直接将文件传递给$mail->addAttachment($this->request->post['file']);

You can not directly pass file to $mail->addAttachment($this->request->post['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 );
    }

这篇关于使用OpenCart 2.1.1.1用PHP发送附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆