调试电子邮件不能在cakephp中工作

时间:2012-05-03 01:47:13

标签: php email cakephp

我正在尝试在cakephp中调试我的电子邮件,因为我无法发送测试邮件。但是,我所做的一切只给我一个空白的网页,甚至调试。我已经将调试模式设置为2.以下是我的代码:

C:\ xampp \ htdocs \ NewFolder \ app \ webroot \ email \ app \ controllersmailer_controller.php

<?php  
class MailerController extends AppController { 

    var $name = 'Mailer'; 
    //Not using a model 
    var $uses = ''; 
    //The built in Cake Mailer 
    var $components = array('Email'); 

    $this->Email->delivery = 'debug';
    /** 
     * Send a text string as email body 
     */ 
    function sendSimpleMail() { 
        //$this->Email->to = 'yourlogin@localhost'; 
        $this->Email->to = 'csorila17@gmail.com'; 
        $this->Email->subject = 'Cake test simple email'; 
        $this->Email->replyTo = 'noreply@example.com'; 
        $this->Email->from = 'Cake Test Account <noreply@example.com>'; 
        //Set the body of the mail as we send it. 
        //Note: the text can be an array, each element will appear as a 
        //seperate line in the message body. 
        if ( $this->Email->send('Here is the body of the email') ) { 
            $this->Session->setFlash('Simple email sent'); 
        } else { 
            $this->Session->setFlash('Simple email not sent'); 
        } 
        //$this->redirect('/'); 
        $this->Email->delivery = 'debug';

    } 
} 
?> 

C:\ XAMPP \ htdocs中\ NewFolder \应用\根目录\电子邮件\应用\视图\布局\ default.thtml中

<?php echo $this->Session->flash(); ?>
<?php echo $this->Session->flash('email'); ?>

C:\ XAMPP \ htdocs中\ NewFolder \应用\根目录\电子邮件\应用\视图\布局\电子邮件\ HTML \ default.thtml中

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<body> 
<?php echo $content_for_layout; ?> 
<?php echo $this->Session->flash(); ?>
<?php echo $this->Session->flash('email'); ?>
</body> 
</html> 

C:\ XAMPP \ htdocs中\ NewFolder \应用\根目录\电子邮件\应用\视图\布局\电子邮件\文本\ default.thtml中

<?php echo $content_for_layout; ?> 

2 个答案:

答案 0 :(得分:0)

如果您在本地运行此应用程序,我相信您需要设置邮件服务器才能工作,否则您可以使用任何提供程序的smtp邮件选项。为此,您必须配置邮件设置。

app\config中,您可以创建文件email.php

<?php 
    class EmailConfig {

        public $default = array(
            'host' => 'ssl://smtp.gmail.com',
            'port' => 465,
            'username' => 'your email address', //example@gmail.com
            'password' => 'password',
            'transport' => 'Smtp',
            'from' => array('From Email Address' => 'Name to be displayed in the Email'),
            'log' => true
        );

    }

在控制器中执行此操作后,您可以设置使用以下代码发送电子邮件。

$email = new CakeEmail();
$email->to('example@gmail.com');
$email->subject('Email testing Subject');       
$email->send('Email testing content');

答案 1 :(得分:0)

除了需要可访问的SMTP服务器之外,您也不能将此语句放在函数之外......

$this->Email->delivery = 'debug';   // needs to be *inside* a function

另请注意,当您执行使用此命令时,它只会将电子邮件保存到会话变量中,并且 NOT 将其发送。