Nasrul Hazim Bin Mohamad

Reset Your Password from API


As you may know, Laravel provide a scaffold auth, which include reset/forgot password on web applications side. But what if you have a mobile application that need to reset password from the mobile application?

In this post, I’ll show you how to reset user’s password from an API in Laravel.

UPDATE 02/07/2017:

In case you’re looking for App\Transformers\Json, you basically can create a class called Json in app/Transformers directory.

In the Json class:


namespace App\Transformers;

class Json
    public static function response($data = null, $message = null)
        return [
            'data'    => $data,
            'message' => $message,

Please make sure to composer dumpautoload -o afterward.

CakePHP: PHPMailer Component – Using GMail SMTP



  1. CakePHP 2.x
  2. PHPMailer

Download PHPMailer & extract it to APP/vendors.

Add following lines in APP/vendors/phpmailer/class.phpmail.php line 542, in ELSE statement. This to ensure we have a secured domain through SSL / TLS.

// add a variable named SMTPSecure - SSL / TLS
var $SMTPSecure

// line 542 
    $secure = $this-&gt;SMTPSecure . '://';

Create a component called EmailComponent in APP/app/Controller/Component


App::uses('Component', 'Controller');
App::import('Vendor', 'phpmailer', array('file' =&gt; 'phpmailer'.DS.'class.phpmailer.php'));

class EmailComponent extends Component {

  public function send($to, $subject, $message) {
    $sender = &quot;;; // this will be overwritten by GMail

    $header = &quot;X-Mailer: PHP/&quot;.phpversion() . &quot;Return-Path: $sender&quot;;

    $mail = new PHPMailer();

    $mail-&gt;Host = &quot;;; 
    $mail-&gt;SMTPAuth = true;
    $mail-&gt;SMTPSecure = &quot;ssl&quot;;
    $mail-&gt;Port = 465;
    $mail-&gt;SMTPDebug  = 2; // turn it off in production
    $mail-&gt;Username   = &quot;[yourGMailAccount];;  
    $mail-&gt;Password   = &quot;password&quot;;
    $mail-&gt;From = $sender;
    $mail-&gt;FromName = &quot;From Me&quot;;



    $mail-&gt;Subject = $subject;
    $mail-&gt;Body    = nl2br($message);
    $mail-&gt;AltBody = nl2br($message);

    // return an array with two keys: error &amp; message
    if(!$mail-&gt;Send()) {
      return array('error' =&gt; true, 'message' =&gt; 'Mailer Error: ' . $mail-&gt;ErrorInfo);
    } else {
      return array('error' =&gt; false, 'message' =&gt;  &quot;Message sent!&quot;);


Create a controller, ExampleController.php to test out our EmailComponent


App::uses('AppController', 'Controller');

class ExampleController extends AppController {
	public $components = array('Email');
	public function index() {
		// just to test out the sending email using SMTP is OK, create a method that will be able to access from public
		$to = '[]';
		$subject = 'Hi buddy, i got a message for you.';
		$message = 'Nothing much. Just test out my Email Component using PHPMailer.'
		$mail = $this-&gt;Email-&gt;send($to, $subject, $message);

When you navigate to http://localhost/APP/Example, the email should send to []. Please do check the mailbox. 🙂