简体   繁体   中英

debugging email not working in cakephp

i am trying to debug my email in cakephp since i cannot send a test mail. However, everything i do gives me just a blank web page, even the debug. I already set the debug mode to 2. Below are my codes:

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\\app\\webroot\\email\\app\\views\\layouts\\default.ctp

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

C:\\xampp\\htdocs\\NewFolder\\app\\webroot\\email\\app\\views\\layouts\\email\\html\\default.ctp

<!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\\app\\webroot\\email\\app\\views\\layouts\\email\\text\\default.ctp

<?php echo $content_for_layout; ?> 

If you are running this application locally, I believe you need to setup a mail server to work, otherwise you can use smtp mail option with any of the provider. For that you have to configure your mail settings.

In your app\\config you can create a file 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
        );

    }

After this in your controller you can setup use the following code to send email.

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

Well other than needing an accessible SMTP server, you also cannot place this statement outside of a function...

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

Also note that when you do use this command, it will merely save the email into a session variable and NOT send it.

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