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