Code Igniter -> attach email


How do you use the email->attach function?

I can't figure what is happen, cos when i put the code for email->attach the mesage came in blank(the mail body) and there is no attach.

If i remove that code line, everything come back to normal..

thank you

my controller (sendmail.php)


 class Sendmail extends Controller {

      function __construct() {

      function index() {

           $info = array (
                'nome'  => $this->input->post('nome'),
                'mail'  => $this->input->post('email'),
                'motivo'    => $this->input->post('motivo'),
                'mensagem'  => $this->input->post('mensagem'),
                'anexo' => $this->input->post('upload'),


           $this->email->from($info['mail'], $info['nome']);
     /* $this->email->cc(''); # não é preciso */

           if ($this->email->send() ) {
                echo 'sent';

           else {
    # show_error( $this->email->print_debugger() );



my view (formulario.php)

    echo form_open_multipart('davidslv/index.php/sendmail');
          <label for="nome">nome</label>
          <input type="text" name="nome" id="nome" required />

          <label for="email">email</label>
          <input type="text" name="email" id="email" required />

          <label for="assunto">assunto</label>
          <select name="motivo">
               <option value="motivo1">motivo1</option>
               <option value="motivo2">motivo2</option>
               <option value="motivo3">motivo3</option>

          <p> <label for="mensagem">mensagem</label>
          <textarea name="mensagem" id="mensagem" rows="8" cols="30" required></textarea>

          <label for="upload">documento</label>
          <input type="file" id="upload" name="upload" size="18"/>
          <input type="submit" id="enviar" name="enviar" value="Enviar!" />

9/22/2015 1:54:56 PM

Accepted Answer

You can not directly attach a file from the upload field of your form to an email. You can only attach files to your email from your server, so you need to upload the file from the form with CIs upload library: $this->upload->do_upload() to your server into some directory. the upload library needs to be configured, which file types are allowed etc. if the upload was successful, the do_upload function returns extensive data about where the file is stored. you can use the 'full_path' index from the array to attach this file to the email. then send the mail. after that you may delete the file from your server. Here are some code fragments that might help.


if($_FILES['upload']['size'] > 0) { // upload is the name of the file field in the form

$aConfig['upload_path']      = '/someUploadDir/';
$aConfig['allowed_types']    = 'doc|docx|pdf|jpg|png';
$aConfig['max_size']     = '3000';
$aConfig['max_width']        = '1280';
$aConfig['max_height']       = '1024';


    $ret = $this->upload->data();
  } else {

  $pathToUploadedFile = $ret['full_path'];

Hope this helped...

8/12/2013 7:23:09 PM


Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:



Codeigniter Email Class

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow