CakePHP

Send email from localhost in CakePHP using CakeEmail

Sometimes it’s very essential to test email transaction from localhost in raw PHP or in CakePHP. Today I will show you how to send email from localhost in CakePHP using CakeEmail Component.

You must follow the following steps to make it done..

  • Step 1: Open your localhost php.ini file. It is located inside your server’s PHP directory.
  • Step 2: Inside php.ini please add?the following code and save your modified php.ini file.
    extension=php_openssl.dll
  • Step 3: Now need to configure your CakePHP email component by adding the following lines of code in /app/Config/email.php it will override your CakeEmail’s email component.
<?php
/*Gmail component setup in cakephp by Shaharia Azam (shaharia.azam@gmail.com)*/
class EmailConfig {
    public $gmail = array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => 465,
        'username' => 'yourUsername@gmail.com',
        'password' => 'YourPassword',
        'transport' => 'Smtp'
    );
}
?>

Now this above code will use your Gmail’s SMTP authentication details and it will send mail from CakePHP.

  • Step 4: Now you need to make your own function in /app/Controllers/YourController.php file like the below
<?php
public function send_mail($receiver = null, $name = null, $pass = null) {
        $confirmation_link = "http://" . $_SERVER['HTTP_HOST'] . $this->webroot . "users/login/";
        $message = 'Hi,' . $name . ', Your Password is: ' . $pass;
        App::uses('CakeEmail', 'Network/Email');
        $email = new CakeEmail('gmail');
        $email->from('yourUsername@gmail.com');
        $email->to($receiver);
        $email->subject('Mail Confirmation');
        $email->send($message . " " . $confirmation_link);
    }
?>

now you can send email through the above process.

If you face any problem please let me know and I will try to solve the problem. It helped me a lot that’s why I wrote this tutorial for you which will tell you about How to send email from localhost in CakePHP by using CakeEmail.

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.

Need PHP Expert?

I can be a great asset to your project
Request For Hire
close-link

Sharing is Awesome, Do It!

Share this post with your friends
close-link
Looking for a PHP expert? I can be a great help for your project.
Hire Me!
close-image