0
votes

my problem is: Sending email with attachtment in Codeigniter 3.1.5 I want to send a form with name, email, phone and pdf file upload by user as attachment to email but I get problem. please help me

form:

    <div>
        <label for="name" class="label">Name:</label>
        <input type="text" name="name">
    </div>

    <div>
        <label for="email" class="label">Email:</label>
        <input type="email" name="email">
    </div>

        <div>
            <label for="email" class="label">Phone:</label>
            <input type="text" name="phone">
        </div>

        <hr>
    <div>
        <label for="file">File: </label>
        <input type="file" name="file">
    </div>

        <hr>
    <button type="submit" name="submit" class="button is-danger" >Send Form</button>
    </div>

    <?php echo form_close(); ?>

And this is controller that the form return

Controller:

public function send(){

    $config = [
        'porotocol' => 'sendmail',
        'smtp_host' => 'data',
        'smtp_port' => 993,
        'smtp_user' => 'email',
        'smtp_pass' => 'password',
        'mailtype' => 'html',
        'charset' => 'utf-8',
        'wordwrap' => TRUE,
    ];

        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");

    $data = [
        'name' => $this->input->post('name'),
        'email' => $this->input->post('email'),
        'phone' => $this->input->post('phone'),
    ];

        $this->email->from($data['email'], $data['name']);
        $this->email->to('[email protected]');
        $this->email->subject('form');

    $file_name= $this->input->post('file');
    $this->email->attach($file_name);

        $message = $this->load->view('forms/formformat', $data, TRUE);
        $this->email->message($message);

    if($this->email->send()){
        $this->session->set_flashdata("email_sent","ok");
        redirect('forms/mailform');
    } else {
        $this->session->set_flashdata("email_nosent","error");
        $data['content'] = 'forms/mailform';
        $this->load->view('master', $data);
    }

}

Also I have tried with upload library but the problem didn't solved. would you please help me how I can send the form with attachment to email. thank you

1
you need to upload locally, then attach to email stackoverflow.com/questions/43340427/… - qwertzman

1 Answers

0
votes

this code solved for me:

        $aConfig['upload_path']      = './uploads/';
        $aConfig['allowed_types']    = 'pdf';
        $aConfig['max_size']     = '3000';
     $config['encrypt_name']     = true;
        $this->load->library('upload', $aConfig);


    foreach ($_FILES as $key => $file)
    {
        if ($file['error'] == 0)
        {
            $this->email->attach($file['tmp_name'], '', $file['name']);
        }
    }