Hi Nerd, today on Send Email using PHPMailer. Laravel sends emails by default using the SwiftMailer package. SwiftMailer is Laravel’s built-in library. To send emails in the PHP-based web application, we mostly utilized the default mail() method. Now, we’ll use the Laravel package “PHPMailer” to send emails. PHPMailer is an open-source tool that is simple to install and use in a Laravel application.
Many capabilities are included in this bundle, such as the ability to send numerous emails with To, CC, BCC, and Reply-to addresses. In terms of security, it guards against header injection attacks. As a result, it is exceedingly safe to use. Attachments, including inline attachments, can be included in emails. If you have not installed laravel you can do it here How to install laravel 9 – Laravel Installation Guide.
Send Email using PHPMailer in Laravel
STEP 1 : Install PHPMailer in Laravel
I’ll be using the composer to install the PHPMailer package in my Laravel application. You may also use the Laravel installer. Launch the command prompt and type the following command.
composer require phpmailer/phpmailer
After installing the package, check the composer.json file to see if the PHPMailer package is installed. The composer.json file is located at the root of the project directory.
STEP 2 : Setup .env file
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
SMTP_USERNAME=sender@gmail.com #your smtp username/email
SMTP_PASS=12345 #your smtp server password
STEP 3 : Create PHPMailerController.php file
To create a controller file with the name PHPMailerController.php, Follow the below command.
php artisan make:controller PHPMailerController
STEP 4 : Create Routes For PHPMailer
Now, add the following code to the web.php file to create routes.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MailController;
Route::get("/mail", [PHPMailerController::class, "index"]);
STEP 5 : Use PHPMailer and Exceptional classes
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
Simply open the PHPMailerController.php file and insert the code snippet below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class MailerController extends Controller {
public function index(Request $request) {
//Required Data
$sendTo = "myuser@example.com";
$subject = 'New EMAIL !';
$body = "Hello World ! My Email.";
$isHTML = false;
try {
//Creating PHPMailer instance
$mail = new PHPMailer(true);// Passing `true` enables exceptions
// 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;
// $mail->AltBody = plain text version of email body;
if( !$mail->send() ) {
return back()->with("failed", "Email not sent.")->withErrors($mail->ErrorInfo);
} else {
return back()->with("success", "Email has been sent.");
}
} catch (Exception $e) {
return back()->with('error','Message could not be sent.');
}
}
}
Don’t forget to edit the sender data in the above code, such as:
- $sendTo : (String) Send email to this address.
- $subject : (String) Email subject.
- $body : (String) Email body.
- $isHTML : (Boolean) Is email body HTML?
Email will be sent if you use the “/mail” route. Most of the time, if you use localhost, email will not be sent unless you add an extension to your php.ini file.
Simply find the “php.ini” file and insert the following line anywhere:
extension=php_openssl.dll
In Linux-based operating systems, the php.ini file is read-only. First, navigate to the folder containing php.ini and modify it using this command.
sudo gedit php.ini
All the best.