Nasrul Hazim Bin Mohamad

Reset Your Password from API

Jan
05

UPDATE 27/4/2018 – New blog post about reset password from an API – LARAVEL: RESET PASSWORD FROM AN API. Much more simpler than this one.

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:

<?php

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.

UPDATE 27/4/2018 – New blog post about reset password from an API – LARAVEL: RESET PASSWORD FROM AN API. Much more simpler than this one.

CakePHP: PHPMailer Component – Using GMail SMTP

Apr
12

Requirements:

  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 
if(!empty($this-&gt;SMTPSecure))
{
    $secure = $this-&gt;SMTPSecure . '://';
}

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

&lt;?php

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;from_you@you.com&quot;; // this will be overwritten by GMail

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

    $mail = new PHPMailer();

    $mail-&gt;IsSMTP();
    $mail-&gt;Host = &quot;smtp.gmail.com&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]@gmail.com&quot;;  
    $mail-&gt;Password   = &quot;password&quot;;
    
    $mail-&gt;From = $sender;
    $mail-&gt;FromName = &quot;From Me&quot;;

    $mail-&gt;AddAddress($to);

    $mail-&gt;IsHTML(true);
    $mail-&gt;CreateHeader($header);

    $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;);
    }
  }
}

?&gt;

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

&lt;?php

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 = '[someone@you.com]';
		$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);
		$this-&gt;set('mail',$mail);
		$this-&gt;render(false);
	}
}

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

References:

  1. http://phpmailer.worxware.com/?pg=examplebgmail
  2. http://blog.teamtreehouse.com/sending-email-with-phpmailer-and-smtp
  3. http://book.cakephp.org/2.0/en/controllers/components.html#creating-a-component