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

Codeigniter 3 email attachment from form

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

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>

和一个 post 电子邮件控制器表单,用于发送邮件

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

邮件正在处理中,但我收到的邮件中没有附加文件。

我查阅了 google,但无法得到任何关于此事的 post。

除此之外,我是 php 和 Codeigniter 3 的新手,感谢任何帮助

You have to add attachment file path in attachment argument

根据以下内容更换您的 post 电子邮件控制器

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); 这样你的代码就可以正常工作。

参考这个问题: