How to send emails with Laravel using PHPMailer.

As you know Laravel is one of the most used backend frameworks in the world. It has lots of built-in tools which help you in different situations but today we will see how we can use a third-party library in Laravel to send emails.

We will learn how we can implement one of the most famous PHP mail library “PHPMailer” to send emails with Laravel. The email sending feature can be used for many purposes. It is very easy and simple to send emails with PHPMailer. Let’s start.

SEND EMAILS WITH LARAVEL :

send emails with laravel

STEP 1 :

Create a new Laravel project and Install the PHPMailer library using composer. If you do not have composer on your system please visit: https://getcomposer.org/

//Install Laravel Globally
composer global require laravel/installer

//Create new Laravel project
laravel new mail-app
cd mail-app
php artisan serve

//Install PHP Mailer
composer require phpmailer/phpmailer

STEP 2 :

Define required details in .env file. You will find this file inside the root folder of your Laravel project.

SMTP_PORT=465 #your smtp server port number
SMTP_HOST=smtp.example.com #your smtp server host name
[email protected] #your smtp username/email
SMTP_PASS=12345 #your smtp server password

STEP 3

Create a controller using the PHP artisan command.

php artisan make:controller MailController

STEP 4

Create a new route. Goto routes/web.php.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MailController;

Route::get("/mail", [MailController::class, "index"]);

?>

STEP 5

Goto app/Http/Controllers/MailController.php and create a public “index” function inside MailController class. Also, write this line after use App\Http\Controllers\Controller;” :

use PHPMailer\PHPMailer\PHPMailer;
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use PHPMailer\PHPMailer\PHPMailer;

class MailController extends Controller
{
    public function index(){
     
     
    }
    
}

?>

STEP 6

Now inside “index” function, write the following code to use PHPMailer for sending emails.

            //Required Data
            $sendTo = "[email protected]";
            $subject = 'New EMAIL !';
            $body = "Hello World ! My Email.";
            $isHTML = false;

            //Creating PHPMailer instance
            $mail = new PHPMailer();

            // Settings
            $mail->IsSMTP();
            $mail->CharSet = 'UTF-8';
            $mail->Host       = env('SMTP_HOST', "");
            $mail->SMTPDebug  = 0;
            $mail->SMTPAuth   = true;
            $mail->Port       = env('SMTP_PORT', "");
            $mail->Username   = env('SMTP_USERNAME', "");
            $mail->Password   = env('SMTP_PASS', "");
            $mail->setFrom(env('SMTP_USERNAME', ""), "MY EMAIL");
            $mail->addAddress($sendTo);
            $mail->SMTPSecure = 'ssl';
    
            // Content
            $mail->isHTML($isHTML);                   
            $mail->Subject = $subject;
            $mail->Body    = $body;
             
            //send
            $mail->send();  

            return "Mail sent"; 

The above code is easy to understand. First, I defined three variables.

  • $sendTo : (String) Send email to this address.
  • $subject : (String) Email subject.
  • $body : (String) Email body.
  • $isHTML : (Boolean) Is email body HTML?

After that, I have created a new instance of PHPMailer class and set all the required details. You also need to do the same.

Just for your info, you can use the env() function to access the .env files variable. The first parameter in the env() function is the name of the variable and the second parameter is a default value (in case variable is not defined in the .env file).

//SYNTAX
env("VARIABLE_NAME", "DEFAULT_VALUE");

So the final code of our MailController will look like this :

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use PHPMailer\PHPMailer\PHPMailer;

class MailController extends Controller
{
    public function index(){
     
            //Required Data
            $sendTo = "[email protected]";
            $subject = 'New EMAIL !';
            $body = "Hello World ! My Email.";
            $isHTML = false;

            //Creating PHPMailer instance
            $mail = new PHPMailer();

            // Settings
            $mail->IsSMTP();
            $mail->CharSet = 'UTF-8';
            $mail->Host       = env('SMTP_HOST', "");
            $mail->SMTPDebug  = 0;
            $mail->SMTPAuth   = true;
            $mail->Port       = env('SMTP_PORT', "");
            $mail->Username   = env('SMTP_USERNAME', "");
            $mail->Password   = env('SMTP_PASS', "");
            $mail->setFrom(env('SMTP_USERNAME', ""), "MY EMAIL");
            $mail->addAddress($sendTo);
            $mail->SMTPSecure = 'ssl';
    
            // Content
            $mail->isHTML($isHTML);                   
            $mail->Subject = $subject;
            $mail->Body    = $body;
             
            //send
            $mail->send();  

            return "Mail sent"; 
     
    }
    
}

?>

If you hit the “/mail” route email will be sent. In most cases, if you are using localhost email will not be sent until you add an extension in your php.ini file.

Simply search the “php.ini” file and add this line anywhere :

extension=php_openssl.dll

php.ini file is read-only in Linux-based OS. First, find the folder where php.ini is located and edit it by running this command.

sudo gedit php.ini

In this way, you can send emails with Laravel using the PHPMailer library.

Related Posts