0

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

我的代码在这里:

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()  
4

2 回答 2

0

For more info please visit: 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);
?>

For SMTP:

save this below code with "email.php" file in app/Config folder and put your smtp details in $smtp array. and assign $smtp variable in new 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',
    );

}
?>
于 2013-09-19T11:51:25.370 回答
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");
        }
    }
}
于 2013-09-19T06:50:07.473 回答