Codeigniter 3来自表单的电子邮件附件

时间:2017-04-11 08:20:35

标签: php codeigniter codeigniter-3 php-7.1

我有一个咨询控制器,用户可以上传他们的简历

public function consultancy($page = 'consultancy') {
  if (! file_exists(APPPATH.'views/pages'.$page.'.php'))
  {
    show_404();
  }
  $data['title'] = ucfirst($page);


  $this->load->view('templates/header', $data);
  $this->load->view('pages/'.$page, $data);
  $this->load->view('templates/footer', $data);
}

视图

<form enctype="multipart/form-data" style="text-align:left;font-size:12px;" action="<?php echo base_url(); ?>postconEmail/"method="POST">

               Name <input class="form-control" id="id_name" name="name" type="text" required />
               Phone <input class="form-control" id="id_phone" name="phone" type="text" required />
               From email <input class="form-control" id="id_from_email" name="from_email" type="email" required />
               Subject <input class="form-control" id="id_subject" name="subject" type="text" required />
               Message <textarea class="form-control" cols="40" id="id_message" name="message" rows="10" required></textarea>
            <br>
            <label for="id_resume" class="custom-file-upload"><i class="fa fa-cloud-upload"></i> Resume Upload</label>
            <input class="custom-file-upload" id="filename" type="text" size="35" placeholder="Upload Your Resume"/>
            <input class="btn btn-primary form-control test" id="id_resume" name="resume" type="file" />
               <div class="form-group">
            <button style="float:right;display: inline;" type="submit" class="btn btn-primary">
                <span class="glyphicon glyphicon-star"></span> Submit
              </button> </div>
        </form>

和发送邮件的邮件电​​子邮件控制器表格

public function postconEmail(){

  $data = $this->input->post();
  $this->load->library('email');
  $config = array();
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'mail.example.com';
    $config['smtp_user'] = 'user@example.com';
    $config['smtp_pass'] = 'password';
    $config['smtp_port'] = 'xxx';
    $this->email->initialize($config);

    $this->email->set_newline("\r\n");

    $this->email->from($data['from_email']);
    $this->email->to('info@example.com');
    $this->email->subject($data['subject']);
    $this->email->message($data['message']);
    $this->email->attach($data['resume']);
    if ($this->email->send()) {
      $this->session->set_flashdata('success','Email Sent');
      redirect(base_url());
    } else{
      show_error($this->email->print_debugger());
    }
  }

邮件正在通过,但我收到的邮件没有附加文件。

我查了一下谷歌,但我无法掌握有关此事的任何帖子。

而且我是php和Codeigniter 3的新手,我们非常感谢

1 个答案:

答案 0 :(得分:0)

  

您必须在附件参数

中添加附件文件路径

按照以下

替换您的帖子电子邮件控制器
public function postconEmail(){
    $data = $this->input->post();
    $this->load->library('email');
    $config = array();
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'mail.example.com';
    $config['smtp_user'] = 'user@example.com';
    $config['smtp_pass'] = 'password';
    $config['smtp_port'] = 'xxx';
    $this->email->initialize($config);

    $this->email->set_newline("\r\n");

    $this->email->from($data['from_email']);
    $this->email->to('info@example.com');
    $this->email->subject($data['subject']);
    $this->email->message($data['message']);

    $resume_tmp_path = $_FILES['resume']['tmp_name'].'/'.$_FILES['resume']['name'];

    $this->email->attach($resume_tmp_path);
    if ($this->email->send()) {
      $this->session->set_flashdata('success','Email Sent');
      redirect(base_url());
    } else{
      show_error($this->email->print_debugger());
    }
  }

如果这不起作用,那么你提到这个问题它说你不能在没有上传服务器的情况下附加文件,所以首先你必须在你的服务器上传文件,然后通过$this->email->attach(youy file path);,这样你的代码才能正常工作。

请参阅此问题 https://stackoverflow.com/a/3628203/3377733