Welcome to the Cookbook

loading...

5.4.2.3 Controller

In your controller you need to add the component to your $components array or add a $components array to your controller like:

<?php
var $components = array('Email');
?>
  1. <?php
  2. var $components = array('Email');
  3. ?>

In this example we will set up a private method to handle sending the email messages to a user identified by an $id. In our controller (let's use the User controller in this example)

 
<?php
function _sendNewUserMail($id) {
    $User = $this->User->read(null,$id);
    $this->Email->to = $User['User']['email'];
    $this->Email->bcc = array('secret@example.com');  
    $this->Email->subject = 'Welcome to our really cool thing';
    $this->Email->replyTo = 'support@example.com';
    $this->Email->from = 'Cool Web App <app@example.com>';
    $this->Email->template = 'simple_message'; // note no '.ctp'
    //Send as 'html', 'text' or 'both' (default is 'text')
    $this->Email->sendAs = 'both'; // because we like to send pretty mail
    //Set view variables as normal
    $this->set('User', $User);
    //Do not pass any args to send()
    $this->Email->send();
 }
?>
  1. <?php
  2. function _sendNewUserMail($id) {
  3. $User = $this->User->read(null,$id);
  4. $this->Email->to = $User['User']['email'];
  5. $this->Email->bcc = array('secret@example.com');
  6. $this->Email->subject = 'Welcome to our really cool thing';
  7. $this->Email->replyTo = 'support@example.com';
  8. $this->Email->from = 'Cool Web App <app@example.com>';
  9. $this->Email->template = 'simple_message'; // note no '.ctp'
  10. //Send as 'html', 'text' or 'both' (default is 'text')
  11. $this->Email->sendAs = 'both'; // because we like to send pretty mail
  12. //Set view variables as normal
  13. $this->set('User', $User);
  14. //Do not pass any args to send()
  15. $this->Email->send();
  16. }
  17. ?>
You have sent a message, you could call this from another method like
 
$this->_sendNewUserMail( $this->User->id );
  1. $this->_sendNewUserMail( $this->User->id );