How to upload a file and attach it to email using cakephp 2.x?
NickName:KingBowen Ask DateTime:2012-10-25T15:54:46

How to upload a file and attach it to email using cakephp 2.x?

I am a new learner of cakephp 2.x. I want to build a simple form for user to upload their resume files, and then send them as email attachments. Somehow I can't find a way to attach uploaded file to email. Any help will welcome! Here is my send.ctp:

<?php 
   echo $this -> Form -> create(null, array('enctype' =>'multipart/form-data'));
   echo $this -> Form -> input('first_name', array('type'=>'text')); 
   echo $this -> Form ->input('last_name', array('type'=>'text'));
   echo $this -> Form ->input('contact_number',array('type'=>'text')); 
   echo $this -> Form ->input('email',array('type'=>'text'));
   echo $this -> Form ->input('resume', array('type' => 'file',));
   echo $this -> Form ->end('Submit');      
?>

Here is my HomesController.php

<?php 
   class HomesController extends AppController {
public $name = 'Homes';
public $uses = null;
public $helpers = array('Html', 'Form');

    public function index() {
    }
public function send(){
   if (!empty($this->data)) {
      echo $this->data['last_name'];
      echo $this->data['contact_number'];
      echo $this->data['email'];
          echo $this->data['resume'];

      App::uses('CakeEmail', 'Network/Email');
      $email = new CakeEmail();
      $email->from('[email protected]');
          $email->to('[email protected]');
          $email->subject('About');
          $email->attachments = array($this->data['resume']);
      $email->send($this->data['last_name']);
      $this->redirect(array('action' => 'index'));
   }
     }
   }?>

The email can actually send and be able to receive, but without attachment. When I try "echo $this->data['resume']" it only display a string---'Array'. But other fields like "echo $this->data['last_name']" works properly.

http://cakebaker.42dh.com/2006/04/15/file-upload-with-cakephp/ Above one use database, I don't wanna use database to store files. And it use cakephp 1.x which cannot run in 2.x.

How to do form-based file uploads in CakePHP? This one doesn't say how to attach files to email.

This is my Config/email.php, I use gmail smtp: class EmailConfig {

public $default = array(
    'transport' => 'Smtp',
    'from' => '[email protected]',
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

public $smtp = array(
    'transport' => 'Smtp',
    'from' => array('[email protected]' => 'Sender'),
    'host' => 'smtp.gmail.com',
    'port' => 465,
    'timeout' => 30,
    'username' => 'XXX',
    'password' => '@password',
    'client' => null,
    'log' => false,
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

public $fast = array(
    'from' => 'you@localhost',
    'sender' => null,
    'to' => null,
    'cc' => null,
    'bcc' => null,
    'replyTo' => null,
    'readReceipt' => null,
    'returnPath' => null,
    'messageId' => true,
    'subject' => null,
    'message' => null,
    'headers' => null,
    'viewRender' => null,
    'template' => false,
    'layout' => false,
    'viewVars' => null,
    'attachments' => null,
    'emailFormat' => null,
    'transport' => 'Smtp',
    'host' => 'localhost',
    'port' => 25,
    'timeout' => 30,
    'username' => 'user',
    'password' => 'secret',
    'client' => null,
    'log' => true,
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

}

Copyright Notice:Content Author:「KingBowen」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/13064051/how-to-upload-a-file-and-attach-it-to-email-using-cakephp-2-x

Answers
penguin egg 2012-10-25T08:58:55

$this->data['resume'] is an array that contains information about the file upload.\n\nThe array looks something like this:\n\narray(\n 'name' => '(filename)',\n 'type' => '(filetype)',\n 'tmp_name' => '(file location)',\n 'error' => (int) 0,\n 'size' => (int) 1\n)\n\n\nTry setting the attachment using:\n\n$email->attachments(array($this->data['resume']['name'] => $this->data['resume']['tmp_name']));\n",


More about “How to upload a file and attach it to email using cakephp 2.x?” related questions

How to upload a file and attach it to email using cakephp 2.x?

I am a new learner of cakephp 2.x. I want to build a simple form for user to upload their resume files, and then send them as email attachments. Somehow I can't find a way to attach uploaded file to

Show Detail

attach file to email using php

I have an input on my form to allow a user to browse to a file location. The idea being that they can attach a resume to to the application they are getting ready to submit. &lt;label class="

Show Detail

Multiple files upload using CakePHP Uploader

Is it possible to upload multiple files (images in particular) using CakePHP Uploader and HTML5 multiple attribute? And of course save those images with hasMany relation, eg Project hasMany Image. ...

Show Detail

Razor form - upload and attach to email

I am implementing a razor contact form on Umbraco CMS, which includes inline C# script to create and send an email using .Net's MailMessage. The end user now needs to be able to upload a file, which

Show Detail

How can create pdf file in cakephp 2.x

I am using "tcpdf" vendor file in cakephp 2.X but it is not looking good view. I want every html tag convert properly in pdf file. Please help.

Show Detail

How to attach file to email in Node.js using File Upload control

My Angular app sends a FormGroup to a Node.js server &amp; then sends that form content via an Email. At the moment, I can populate the email body like so: &lt;tr&gt; &lt;th&gt;Town&lt;/th&gt...

Show Detail

CakePHP 2.x using PHP in CSS

Could someone please advise me on the current methods available for using PHP in a CSS file in CakePHP 2.x I have currently separated my stylesheet into a php file and wish to parse my data via th...

Show Detail

file upload in cakephp

How can I upload a file using cakephp? is there any framework support for file uploading or should I write my own code for this ?

Show Detail

how to upload a zip file using curl to a https site using cakephp

how to upload a zip file using curl to a https site using cakephp? I want to upload a zip file to a url using cakephp. $data1 = array('file' =&gt; "@/". realpath($file)."type=application/zip"...

Show Detail

CakePHP passing variables to send email with CakeEmail

In CakePHP 2.x how can I create variables in a form in order to pass it to the CakeEmail function? I currently have a php file 'email.php' that has a form with inputs 'to', 'subject' and 'message'...

Show Detail