Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1040377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:12:59+00:00 2026-05-16T15:12:59+00:00

How do you use the email->attach function? I can’t figure what is happen, cos

  • 0

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)

<?php

 class Sendmail extends Controller {

      function __construct() {
           parent::Controller();
           $this->load->library('email');
           $this->load->helper('url');
           $this->load->helper('form');
           $this->load->library('validation');
      }

      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->load->library('email');
           $this->email->set_newline('\r\n');

           $this->email->clear();
           $this->email->from($info['mail'], $info['nome']);
           $this->email->to('example@mai.com');
     /* $this->email->cc(''); # não é preciso */
           $this->email->subject($info['motivo']);
           $this->email->message($info['mensagem']);
           $this->email->attach($info['anexo']);

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

           else {
            $this->load->view('formulario');
    # show_error( $this->email->print_debugger() );
           }

      }

 }
?>

my view (formulario.php)

    <?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>
          </select>

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

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

     </form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:12:59+00:00Added an answer on May 16, 2026 at 3:12 pm

    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.

    $this->load->library('upload');
    
    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';
    
    $this->upload->initialize($aConfig);
    
      if($this->upload->do_upload('upload'))
      {
        $ret = $this->upload->data();
      } else {
        ...
      }
    
      $pathToUploadedFile = $ret['full_path'];
      $this->email->attach($pathToUploadedFile);
      ...
      $this->email->send();
      ...
    }
    ...
    

    Hope this helped…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
I have used the smtp class to send emails through code. Can I use
What do you use to validate an email address on a ASP.NET form. I
Is there a way to use JQuery to cloak or encrypt email addresses on
I'm working on an email solution in SQL Server ONLY that will use Database
I'm wanting to use jQuery to wrap a mailto: anchor around an email address,
I would like to use Exchange e-mail groups as an authentication scheme for an
My company requires me to use Outlook for my E-mail. Outlook does virtually nothing
Is it a bad idea to use an email address as the salt for
I am looking to use quartz to schedule emails, but I'm not sure which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.