Erhan is typing…

WebDevelopment , PHP, Javascript, CakePHP, ExtJS

Make CakePHP Email Component Reusable

| Comments

I know the title of this post a little bit confusing but let me explain what I want to tell.

You are using CakePHP’s email component and you should set the same information in each time before you send your email like server address, username, password etc.

Just create a new file mailer.php with the following content and drop it into your CakePHP application components folder (I like convention over configuration! ;).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
App::import('Component', 'Email');

class MailerComponent extends EmailComponent
{
  var $from     = 'ME <[email protected]>';
  var $replyTo  = '[email protected]';
  var $sendAs   = 'both';
  var $delivery = 'smtp';
  var $xMailer  = 'Postman';
  var $smtpOptions = array(
    'port'     => 25,
    'host'     => 'serveradress',
    'timeout'  => 30,
    'username' => 'username',
    'password' => 'password'
  );
}

And right now you have a new component with the name Mailer and its server configuration is predefined. You can reuse it without being affected by any kind of mail server change.

You can define a new function inside your controller (_sendEmail() in our case) and make the email sending process more painless.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class AnyController extends AppController
{
  function contact()
  {
    if ($this->_sendEmail('Name', '[email protected]', 'Grate site!')) {
      $this->Session->setFlash(__("Thank you", true));
    } else {
      $this->Session->setFlash('Damn it!');
    }
  }

  function _sendEmail($name, $email, $message)
  {
    $this->Mailer->to = '[email protected]';
    $this->Mailer->subject = __("Site Contact", true);
    $this->Mailer->template = 'contact';

    $this->set('name', $name);
    $this->set('email', $email);
    $this->set('message', $message);
    $this->Mailer->send();

    $this->log( $this->Mailer->subject . ' -> Name:'. $name .' | E-posta: '. $email .' | Message: '. $message .' | smtp error: '. serialize($this->Mailer->smtpError) );
      
    return $this->Mailer->smtpError ? false : true;
  }
}

That’s all! Check Bakery for other cakes :)

Comments