Code Igniter & # 8594; attach email

How do you use the email-> attach function?

I can’t understand what is happening, because when I put the code for email-> attach mesage, it is empty (message body) and there is no attached file.

If I delete this line of code, everything will return to normal operation.

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 opinion (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> 
+6
php email codeigniter
source share
5 answers

You cannot directly attach a file from the form upload field to an email. You can only attach files to your email from your server, so you need to upload the file from the form using the CI download library: $ this-> upload-> do_upload () to your server in some directory. you need to configure the download library, what types of files are allowed, etc. if the download was successful, the do_upload function returns extensive data about where the file is stored. you can use the index "full_path" from the array to attach this file to the message. then send mail. after that you can delete the file from your server. Here are some code snippets 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 helps ...

+18
source share

$ this-> email-> attach ()

Allows you to send an attachment. Put the path / file name in the first parameter. Note. Use the file path, not the URL. To use multiple applications, use the function several times. For example:

 $this->email->attach('/path/to/photo1.jpg'); $this->email->attach('/path/to/photo2.jpg'); $this->email->attach('/path/to/photo3.jpg'); $this->email->send(); 

Codeigniter Email Class

+2
source share

This is absolutely correct code. Try

 $config['upload_path'] = './uploads'; $config['allowed_types'] = 'gif|jpg|jpeg|png|txt|php|pdf'; $config['max_size'] = '9000'; $config['encrypt_name'] = true; $image_data = $this->upload->data(); $fname=$image_data[file_name]; $fpath=$image_data[file_path].$fname; $this->email->attach($fpath); 
+1
source share

Step 1: you cannot directly attach the file from the field to download the form to the email. You can only attach files to your email from your server, so you need to download the file from the form using the CI download library:

$this->upload->do_upload() to your server in some directory.

Step 2:

  $file=upload file; $file_path='uploaded directory on your server(eg:uploads/career)'.$file; 

Step 3: just turn it on

 $this->email->attach($file_path); $this->email->send(); 
+1
source share

This is the latest update, but may be useful.
It was said twice

"You cannot directly attach a file from the field to download your form by email"

. However, this works fine in Codeigniter 3.0

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

(Although, the letter is not sent and no errors are displayed if there are two files with the same name)

+1
source share

All Articles