Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send emails in CakePHP applications

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.60k
    Comment on it

    Sending email becomes a very common feature for web applications, Emails generally used for sending notifications, newsletters and invoices etc. Email can be send in simple text format or in HTML format. This tutorial demonstrate different methods for sending emails through CakePHP applications. In CakePHP (version 2) we have two basic methods available for sending emails.

     

    1.Using Email Component (Depreciated method and removed from CakePHP ver 3.X)

    2.Using CakeEmail class

     

    1. Using Email Component :

    This is the first method to send email using Email component which is removed from CakePHP version 3.x. Here is an example code for how to use Email Component.

    Inside your controller use this code snippet :

     

    <?php
    class EmailController extends AppController{
    
    //use email component
    public $components=array('Email');
    
    function send(){
    
    //array of values for using in template file
    $emailValues=array('name'=>'MyName','phone'=>'MyPhone');
    $this->set('emailValues',$emailValues);//pass to template
    
    $this->Email->reset(); //To clear previous messages
    $this->Email->to = 'receiver_email'; //receiver's email
    $this->Email->subject = 'Subject Title'; // set title for email subject
    $this->Email->replyTo = 'reply_email'; //email address fr replyTo
    $this->Email->from = 'Sender_email'; //sender's email
    $this->Email->template = 'template_name';//email template
    $this->Email->sendAs = 'html';// email type
    
    /*****smtp settings***********/
    $this->Email->smtpOptions = array(
        'port'=>'465',
        'timeout'=>'30',
        'host' => 'ssl://smtp.gmail.com',
        'username'=>'youremail@gmail.com',
        'password'=>'yourpassword',
        );
    $this->Email->delivery = 'smtp';
    
    if ($this->Email->send()) {
        return true;
    } else {
        echo $this->Email->smtpError;
    }
    
    }
    
    ?>

     

    2. Using CakeEmail class :

    CakeEmail is the class which allow sending emails from any place in your application. This class provide more options for sending emails.

    There are two ways to configure setting for email functionality to work. You can configure the setting using two ways :

     

    1. Central configuration :

    To create a central configuration create a file email.php under app/Config/ directory or copy paste email.php.default and rename to email.php. email.php.default file have default configuration settings provided by cakePHP.

    There are three already predefined options available in the file : default, smtp and fast. We can define any config options. For example : In given example we have set configuration under default option.

     

    public $default = array(
            'transport' => 'Smtp',
            'from' => array('sender_email' => 'Sender Name'),
            'host' => 'ssl://smtp.gmail.com',// for using gmail smtp
                    'port' => 465,
                    'timeout' => 30,
                    'username' => 'your gmail address',
                    'password' => 'password',
                    'charset' => 'utf-8',
                    'headerCharset' => 'utf-8',
        );


    2. Runtime Configuration

    You can also configure different options dynamically using CakeEmail Object.

    Example :

    $Email = new CakeEmail();
    $Email->from(array('sender_email' => 'Sender Name'));
    $Email->to('receiver email');
    $Email->subject('Title');
    $Email->send('Message Content');

     

    Steps to use CakeEmail Class :

    Step 1 : First step is to load the CakeEmail class :

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

     

    Step 2 : Load the config settings


    a. From config file :

    $Email = new CakeEmail();//creating object of CakeEmail
    $Email->config('default');// using default config method 

        OR

    $Email = new CakeEmail('default');//or passing config name as argument in constructor

    Note : From version 2.7 'default' is used as implicit configuration

     

    b. Runtime Configuration

    $Email = new CakeEmail();
    $Email->from(array('sender_email' => 'Site Name'));
    $Email->to('receiver_email');
    $Email->subject('Title');
    $Email->send('Message');

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: