2

我是 cakephp 2.x 的新手。我想为用户构建一个简单的表单来上传他们的简历文件,然后将它们作为电子邮件附件发送。不知何故,我找不到将上传的文件附加到电子邮件的方法。欢迎任何帮助!这是我的 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');      
?>

这是我的 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'));
   }
     }
   }?>

电子邮件实际上可以发送和接收,但没有附件。当我尝试“echo $this->data['resume']”时,它只显示一个字符串---'Array'。但是像“echo $this->data['last_name']”这样的其他字段可以正常工作。

http://cakebaker.42dh.com/2006/04/15/file-upload-with-cakephp/ 以上一个使用数据库,我不想使用数据库来存储文件。它使用无法在 2.x 中运行的 cakephp 1.x。

如何在 CakePHP 中进行基于表单的文件上传? 这个没有说明如何将文件附加到电子邮件。

这是我的 Config/email.php,我使用 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',
);

}

4

2 回答 2

3

$this->data['resume']是一个包含文件上传信息的数组。

数组看起来像这样:

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

尝试使用以下方法设置附件:

$email->attachments(array($this->data['resume']['name'] => $this->data['resume']['tmp_name']));
于 2012-10-25T08:58:55.163 回答
1
$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;
于 2014-05-06T10:59:50.963 回答