Welcome to the Cookbook

loading...

5.4.2 Sending a basic message

There is no translation yet for this section. Please help out and translate this.. More information about translations

To send a message without using a template, simply pass the body of the message as a string (or an array of lines) to the send() method. For example:

$this->Email->from    = 'Somebody <somebody@example.com>';
$this->Email->to      = 'Somebody Else <somebody.else@example.com>';
$this->Email->subject = 'Test';
$this->Email->send('Hello message body!');
  1. $this->Email->from = 'Somebody <somebody@example.com>';
  2. $this->Email->to = 'Somebody Else <somebody.else@example.com>';
  3. $this->Email->subject = 'Test';
  4. $this->Email->send('Hello message body!');

5.4.2.1 Setting up the Layouts

There is no translation yet for this section. Please help out and translate this.. More information about translations

To use both text and html mailing message you need to create layout files for them, just like in setting up your default layouts for the display of your views in a browser, you need to set up default layouts for your email messages. In the app/views/layouts/ directory you need to set up (at a minimum) the following structure

	email/
		html/
			default.ctp
		text/
			default.ctp

  1. email/
  2. html/
  3. default.ctp
  4. text/
  5. default.ctp

These are the files that hold the layout templates for your default messages. Some example content is below

email/text/default.ctp
	<?php echo $content_for_layout; ?>
  1. <?php echo $content_for_layout; ?>
email/html/default.ctp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<body>
		<?php echo $content_for_layout; ?>
	</body>
</html>
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <body>
  4. <?php echo $content_for_layout; ?>
  5. </body>
  6. </html>

5.4.2.2 Setup an email element for the message body

There is no translation yet for this section. Please help out and translate this.. More information about translations

In the app/views/elements/email/ directory you need to set up folders for text and html unless you plan to just send one or the other. In each of these folders you need to create templates for both types of messages referring to the content that you send to the view either by using $this->set() or using the $contents parameter of the send() method. Some simple examples are shown below. It is worthwhile to note that $this->set() should be done before invoking Email's send(), a little break in mindset of the usual CakePHP view conventions. For this example we will call the templates simple_message.ctp

text
 Dear <?php echo $User['first']. ' ' . $User['last'] ?>,
   Thank you for your interest.
  1. Dear <?php echo $User['first']. ' ' . $User['last'] ?>,
  2. Thank you for your interest.
html
 <p>Dear <?php echo $User['first']. ' ' . $User['last'] ?>,<br />
 &nbsp;&nbsp;&nbsp;Thank you for your interest.</p>
  1. <p>Dear <?php echo $User['first']. ' ' . $User['last'] ?>,<br />
  2. &nbsp;&nbsp;&nbsp;Thank you for your interest.</p>

5.4.2.3 Controller

There is no translation yet for this section. Please help out and translate this.. More information about translations

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 );