如何使用cakephp发送邮件

时间:2013-09-19 06:32:30

标签: email cakephp

我有一些发送到邮件的代码,但它无法正常工作。

我的代码在这里:

class EmailsController extends AppController
{
    var $name="Email";

    var $uses = NULL;

        public function index()
        {
        App::import('Component', 'Email');
        $path=WWW_ROOT."img";
        $filename="Desert.jpg";
        $email->from = 'pal@gmail.com';
        $email->to='abc@gmail.com';
        $email->subject='test mail';
        $email->template = 'simple_message';
        $email->attachments = array($path.$filename);
        $email->SendAs='html';
        if($email->send())
            {
                $this->session->setFlash("Email Send Successfully");

            }
         else
            {
                $this->session->setFlash("Email is not send");
            }

    }

}

我收到的错误如下:

Call to undefined method stdClass::send()  

2 个答案:

答案 0 :(得分:0)

为什么不使用var $ components?以正确的方式包含电子邮件组件:

class EmailsController extends AppController
{
    var $name="Email";    
    var $components = array('Email');
    var $uses = NULL;

    public function index() {
        $path=WWW_ROOT."img";
        $filename="Desert.jpg";

        $this->Email->from = 'pal@gmail.com';
        $this->Email->to='abc@gmail.com';
        $this->Email->subject='test mail';
        $this->Email->template = 'simple_message';
        $this->Email->attachments = array($path.$filename);
        $this->Email->sendAs='html';
        if($this->Email->send()) {
            $this->Session->setFlash("Email Send Successfully");

        } else {
            $this->Session->setFlash("Email is not send");
        }
    }
}

答案 1 :(得分:0)

有关详细信息,请访问:http://book.cakephp.org/2.0/en/core-utility-libraries/email.html

<?php
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail( $smtp );
$email->to( 'test@example.com' );
$email->subject(__("Reset Password") );
$email->emailFormat('html');
$email->send($body);
?>

对于SMTP:

将此下面的代码保存在app / Config文件夹中的“email.php”文件中,并将您的smtp详细信息放在$ smtp数组中。并分配$ smtp变量 新的CakeEmail($ smtp);

<?php
class EmailConfig {

    public $default = array(
        'transport' => 'Mail',
        'from' => 'you@localhost',
        //'charset' => 'utf-8',
        //'headerCharset' => 'utf-8',
    );

    public $smtp = array(
        'transport' => 'Smtp',
        'from' => array('site@localhost' => 'My Site'),
        'host' => 'localhost',
        'port' => 25,
        'timeout' => 30,
        'username' => 'user',
        'password' => 'secret',
        'client' => null,
        'log' => false,
        //'charset' => 'utf-8',
        //'headerCharset' => 'utf-8',
    );

}
?>