Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send email in cakephp2.x?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 408
    Comment on it

    Sometimes we are required to send mails in our cakephp application.

     

    To setup in cakephp lets follow these steps :

     


    1. In your EmailConfig class at /app/Config/email.php set the email configuration. Use this class to configure email transports of CakePHP.

     

    transport => The name of a supported transport;

     

    valid options are as follows:

     

    Mail - Send using PHP mail function
    Smtp - Send using SMTP
    Debug - Do not send the email, just return the result

     

    The transports can be added or override by adding the appropriate file at app/Network/Email

    from => The origin email.

     

    Now in the class set the default array as :

     

    public $default = array(
            'transport' => 'Smtp',
            'from' => array('noreply@gmail.com' => 'Ishake'),
            'host' => 'smtpout.secureserver.net',
            'port' => 25,
            'timeout' => 30,
            'username' => 'yourtestmail@gmail.com.com',
            'password' => 'yourpassword',
            'client' => null,
            'log' => false,
            'emailFormat' => 'html',
            'charset' => 'utf-8',
            'headerCharset' => 'utf-8',
        );
    
    

     


    2. After setting up the email configuration in email.php file next step is to set AppController at app/Controller/AppController.php.

     

    This is a  controller file for the application-wide. We can add all application wide controller related methods here and add all our application wide methods in this class. Your controllers will inherit them. Like wise we will create our send_mail() function as :

     

    Before that proceeding, make sure the EmailComponent class is loaded using
     App::uses('CakeEmail', 'Network/Email');

     

    then in the class create the function :

     

    public function send_mail($email_data = null)
        {    
    
            $email         = new CakeEmail('default');
            $email_to      = $email_data['to'];
            $email_msg     = $email_data['body'];
            $email_subject = $email_data['subject'];
            
            $email->to($email_to);
            $email->subject($email_subject);
            $mail_status = @$email->send($email_msg);
         
            if (!$mail_status) {
                return FALSE;
            }
            return TRUE;
        }

     


    3. After setting up the AppController in yourcontroller using $this->send_mail() to set the data array with the (body, subject and to) in your action as :

     

    $message = '';  
              $message .= '<html>';
              $message.='<table style="width:800px;margin:auto;border-collapse:collapse;border:1px solid #5A5A5A;">';
              $message.='<thead style="background:#5A5A5A;">';
              $message.='<tr>';
              $message.='<td width="50%" style="padding:14px 20px;text-align:right;font-size:25px;color:#fff;"></td>';
              $message.='</tr>';
              $message.='</thead>';
              $message.='<tbody>';
              $message.='<tr>';
              $message.='<td style="padding:5px 20px;" colspan="2">';
              $message .= "<h3>Test Mail</h3></br>";
              $message.='</td>';
              $message.='</tr>';
              $message.='</tbody>';
              $message.='</table>';
              $message .= '<html>';
    
                $data_send['body'] = $message;
                $data_send['subject'] = "Test Ckaephp Mail";
                $data_send['to'] = "testToMail@gail.com";
    
        $this->send_mail($data_send);

     

    In this way you can setup the cakephp mail functionality in your application and make use of it anywhere in the application.

 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: