来自表单的Codeigniter 3电子邮件附件 [英] Codeigniter 3 email attachment from form

查看:75
本文介绍了来自表单的Codeigniter 3电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a consultancy controller where users can upload their resume

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>

以及完成发送邮件的邮政电子邮件控制器表格

and a post email controller form where the send mail is done

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

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

The mail is going through but there is no file attached to the mail i am receiving.

我在google上查找了一下,但无法获得有关此事的任何帖子。

I looked up google but am not able to get my hands on any posts regarding this matter.

在此基础上,我还是php和Codeigniter 3的新手。

And over that I am new to php and Codeigniter 3 any kind of help is appreciated

推荐答案


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

You have to add attachment file path in attachment argument

按以下所示替换您的帖子电子邮件控制器

Replace Your post email controller as per below

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(您的文件路径); >这样您的代码就可以正常工作。

If This is not working then you refer this question it says You cannot attach file without upload your Server so first you have to upload file in your server and then pass $this->email->attach(youy file path); so your code definetely work.

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

这篇关于来自表单的Codeigniter 3电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆