Ctrice
Ctrice

Reputation: 33

Sending email through contact form with file attachment

I'm trying to create a contact form using Codeigniter that will send an email with a file attachment. Right now when the email is sent, all I get is the name of the file that was uploaded and not the actual attachment. Can someone help me out?

Here is the code I'm using:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class sendmail_contact extends CI_Model {

function __construct()
{

    parent::__construct();

}

function send()
{

    $this->load->library('email');
    $this->load->helper('date');

    $now = time();
    $contact_date = unix_to_human($now); // U.S. time, no seconds

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

    $name = $this->input->post('name', TRUE);
    $file = $this->input->post('file', TRUE);


    $this->email->from($email);
    $this->email->to('[email protected]'); 

    $this->email->subject('Subject');
    $this->email->message("Email" . $name . "\r\n" . $file); 

    $this->email->send();


}

Upvotes: 0

Views: 1448

Answers (2)

Sergio Varela
Sergio Varela

Reputation: 53

The $name should be the full path to the file, you can find your system´s root by echoing

echo $this->config->item('server_root');

and you can build it like this:

$this->config->item('server_root')."/path to needed folder/"

the path to your file will be:

$name = $this->config->item('server_root')."/path to needed folder/"."file´s name";

if you need to construct a relative path, you can you can consult how to do it here link

Upvotes: 1

Filip G&#243;rny
Filip G&#243;rny

Reputation: 1769

First of all, save your attached file to the storage.

See http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Then, after you save the file that user uploaded, get a name of that file and use

$this->email->attach($name);

It will attach the file to the email you want to send.

Upvotes: 1

Related Questions