CakePhp provides a lot of functionalities, sending an email is one of the most important, since all apps need such a functionality.

For example, an application could send a confrming email when a user regiters himself, or the application could send notifications to users.

Anyway, there are a lot reasons to send an email, if you are here your application needs to send emails!

cakephp

In this short tutorial I'm going to show to you how to send email using gmail as SMTP server.

With your favourite editor open php.ini file (mine is in /etc/php5/apache2/php.ini), search for Dynamic Extensions and add this text "extension=php_openssl.so". Your file now should look like the following piece:

...................
...................
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
extension=php_openssl.so
..................
..................

Save the file (you have to be sudo…)

Now, cd to /path/to/your/app/ (mine is /var/www/myapp/)

make a copy of the email.php.default configuration file:

$ cp Config/email.php.default Config/email.php

With your favourite editor, open Config/email.php, and then modify it to match the one shown below:

<?php
class EmailConfig {
        public $gmail = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => 'your_user@gmail.com',
        'password' => 'your_password',
        'transport' => 'Smtp',
           );
}

Naturally, you have to change your_user and your_password to match your account on gmail.

Now it's time to add some logic to your app, open the controller in which you want to add the email sending, at the begginning of the file add the "App::uses" as shown below:

<?php
App::uses('CakeEmail', 'Network/Email');

Then, in the body of the controller add your function to send emails:

 public function send_email($dest=null)
{
                $Email = new CakeEmail('gmail');
                $Email->to($dest);
                $Email->subject('Automagically generated email');
                $Email->replyTo('the_mail_you_want_to_receive_replies@yourdomain.com');
                $Email->from ('your_user@gmail.com');
                $Email->send();
        return $this->redirect(array('action' => 'index'));
}

 

Now you can call this method, passing to it the destination of the email.

 

Gg1