【问题标题】:Attach form-file to email, Codeigniter将表单文件附加到电子邮件,Codeigniter
【发布时间】:2014-06-10 07:56:03
【问题描述】:

我需要从用户那里获取文件并将其附加到邮件而不将其保存在服务器上。这是否可以在 Codeigniter 中将上传的文件存储在变量中,然后将其附加到来自 Codeigniter 电子邮件库的电子邮件?如何做到这一点? 在 CI 中,我只找到将上传的文件存储在硬盘上的类,而不是变量。

【问题讨论】:

  • 有什么问题? $this->email->attach() 您是否尝试过阅读ellislab.com/codeigniter/user-guide/libraries/email.html 的整个文档?你想把文件保存在哪里?我认为您无法完成您想要实现的目标,在将某些内容附加到文件时,您需要先上传它,然后再将其附加到电子邮件中。
  • 所以我必须上传、附加、发送邮件,然后从服务器上删除文件?
  • 没错,发件人 (PHP) 必须知道文件的内容。

标签: php codeigniter email file-upload


【解决方案1】:

首先您必须将文件上传到服务器目录,然后将文件的名称和路径传递到电子邮件附件行,即,

    **$this->email->attach('/path/to/file.ext');**

上传类和邮件库见以下代码。

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());

    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

    $this->load->library('email');

    $this->email->from('your@example.com', 'Your Name');
    $this->email->to('someone@example.com');
    $this->email->cc('another@another-example.com');
    $this->email->bcc('them@their-example.com');

    $this->email->subject('Email Test');
    $this->email->attach('/path/to/file.ext');
    $this->email->message('Testing the email class.');

    $this->email->send();

    }

【讨论】:

  • 有什么办法可以避免在服务器上保存文件?
  • 没办法。发送电子邮件后,您可以使用 php 中的 unlink() 函数删除文件。
  • 那我就按你的方式做吧:/
【解决方案2】:

试试这个

https://www.codeigniter.com/user_guide/libraries/email.html

效果很好。我在最近的项目中尝试过这个

【讨论】:

  • 问题在于将文件上传到服务器而不将其保存在驱动器上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-25
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-18
相关资源
最近更新 更多