简体   繁体   中英

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('xyz@gmail.com');
          $email->to('abc@gmail.com');
          $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' => 'XXX@gmail.com',
    //'charset' => 'utf-8',
    //'headerCharset' => 'utf-8',
);

public $smtp = array(
    'transport' => 'Smtp',
    'from' => array('XXX@gmail.com' => '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',
);

}

$this->data['resume'] is an array that contains information about the file upload.

The array looks something like this:

array(
   'name' => '(filename)',
   'type' => '(filetype)',
   'tmp_name' => '(file location)',
   'error' => (int) 0,
   'size' => (int) 1
)

Try setting the attachment using:

$email->attachments(array($this->data['resume']['name'] => $this->data['resume']['tmp_name']));
$Path = WWW_ROOT."img";
$fileName = 'image.png';    
$this->Email->to = $to;
$this->Email->subject = $subject;
$this->Email->attachments = array($Path.$fileName);
$this->Email->from = $from;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM