0

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

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 的新手,感谢您提供任何帮助

4

1 回答 1

1

您必须在附件参数中添加附件文件路径

更换您的邮政电子邮件控制器,如下所示

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

于 2017-04-11T08:54:55.443 回答