使用OpenCart 2.1.1.1用PHP发送附件

利亚姆·麦克阿瑟(Liam McArthur)

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

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

进入catalog / controller / information / contact.php-这样默认的联系表单还可以包含附件上传功能吗?我尝试过,但没有骰子。

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

首先您需要上传文件

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

现在我们需要上传脚本来上传文件

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

最后,现在您可以将附件文件传递给邮件功能

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

发送多个附件 PHP imap 只发送 1 个附件

来自分类Dev

用1个ajax发送2个请求

来自分类Dev

如何同时使用1,2和2,1

来自分类Dev

使用 UTF-8 字符发送附件时,Flask 会引发 UnicodeEncodeError (latin-1)

来自分类Dev

使用Scala收集方法可帮助将[0,0,0,1,1,1,1,0,0,1,1]的列表转换为[3,4,2,2]

来自分类Dev

mail()函数在PHP中发送的附件不超过1mb

来自分类Dev

将'1-1-1至1-10-1'转换为'1-1-1','1-2-1',...'1-10-1'的十个值

来自分类Dev

使用 1:M 关系创建 1:1

来自分类Dev

为什么1/2 == 0使用double?

来自分类Dev

RewriteRule并使用php获得1,2或3个值

来自分类Dev

用CSS绘制1/2或1/4等宽线

来自分类Dev

为什么alert(“ 1”--“ 1”); 用javascript产生2个?

来自分类Dev

如何使用嵌套的for循环返回字符串“ 1 + 2 + 3 + .. 1 + 2 + 3 + .. 1 + 2 + 3 + ..”?

来自分类Dev

条件1或条件2

来自分类Dev

的Django 2模型1

来自分类Dev

如何在python中将['2 1','2 1']转换为[['1','2'],['2','1']]?

来自分类Dev

TSQL 2查询1

来自分类Dev

MySQL:LIMIT 2,-1

来自分类Dev

ArrayIndexOutOfBoundsException:2> = 1 on每个

来自分类Dev

&>测试或>测试2>&1

来自分类Dev

2 条件 1 AND 语句

来自分类Dev

标尺函数的迭代实现(1,2,1,3,1,2,1,4,1,2,1,3,...)

来自分类Dev

mailgun发送的附件为1kb空白图像

来自分类Dev

sum(1 / prime [i] ^ 2)> = 1?

来自分类Dev

Laravel关系1-2-1

来自分类Dev

调整 2:1 图像的大小以放入 1:1 div

来自分类Dev

理解矩阵子设置:`mat[2, mat[1, ] == 1] == 1`

来自分类Dev

获取旧数字的整数总和 1 + (1 + 2) + (1 + 2 + 3) + ... + (1 + 2 + 3 + ... + n)

来自分类Dev

用1替换2个空格