Automation Testing Company in India
The world is heading towards automation, it has it’s strong footsteps in Software testing as well. We at Blazingcoders perform automation testing with various tools such as Junit, Selenium, QTP/
Read MoreSign Up Now and Get FREE CTO-level Consultation.
Today we are going to explain how to send email in Laravel using STMP. Laravel is PHP based framework that comes with a simple API to send emails via SMTP from localhost or Hosted online. You can easily use the 'Mailable' class to build an email function. You can also send customized data explicitly using the with() a method. Can Use SMTP Mails service providers like Google, Yahoo, Webmail and etc
Sending emails is a crucial feature in most web applications — from user registration confirmation to contact forms and invoice delivery. Laravel, a powerful PHP framework, makes it easy to send emails using SMTP.
In this step-by-step guide, we’ll show you how to send email in Laravel using SMTP, configure the .env file, use the built-in Mail facade, and test it efficiently.
SMTP (Simple Mail Transfer Protocol) ensures reliable, authenticated, and trackable email delivery. With SMTP, Laravel can connect with mail services like:
Gmail
Mailgun
SendGrid
Outlook
Zoho
Custom business email
Configure email setting in 'config\mail.php' Like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, etc.
Here are the steps to send mail using the SMTP server in laravel.
Step 1:
First, install the latest version of laravel. Below is the command to install Laravel
composer create-project laravel/laravel project_name --prefer-dist
Step 2:
open up the '.env' file and configure the email settings. Here we are using email client as Gmail
Gmail SMTP Settings:
Host Name: smtp.gmail.com
Smtp Port: 587(tls), 465(ssl)
Encryption: tls/ssl
Your '.env' file should look like given below
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=your_gmail_id
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=ssl
Step 3:
Then generate the mailable class. Fire up the below command on the command prompt.
php artisan make:mail SendeMail
This will create 'SendeMail.php' file inside 'App\Mail' folder.
SendMail.php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeUser extends Mailable
{
use Queueable, SerializesModels;
public $uname;
public function __construct($uname)
{
$this->uname = $uname;
}
public function build()
{
return $this->view('email.welcome');
}
}
?>
Setp 4:
Create a View file. This will contain the template of our email. Create 'email' folder inside 'views' and place 'welcome.blade.php' file. below are the simple view template
welcome.blade.php
Step 5:
Create a contoller file with comment give below
php artisan make:controller MailController
And Open the 'MailController.php' file inside 'App\Http\Controllers' folder and add send_mail() function
MailController.php
...
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeUser;
...
public function send_email()
{
$user_name = 'John Anderson';
$to = 'john.anderson@gmail.com'
Mail::to($to)->send(new WelcomeUser($user_name));
return 'Mail sent successfully';
}
?>
Step 6:
Add route for our MailController send_mail() function. router file will be in this path 'routes\web.php'
Route::get('/user/sendmail', 'HomeController@send_email');
Open your browser and Run the code
Check your .env credentials (especially Gmail or SMTP provider)
Verify that your MAIL_PORT and MAIL_ENCRYPTION are correct
Use logging: MAIL_MAILER=log to debug email content without sending
For production, use services like Mailgun, SendGrid, or Amazon SES
Use queues for performance: php artisan queue:work
Customize Blade views for transactional emails
1) How do I send an email in Laravel using SMTP?
In Laravel, configure .env for SMTP, create a Mailable class, and use the Mail::to()->send() function to send the email.
2) What is the best way to send email in Laravel?
The best way is to use SMTP with Laravel's Mail facade and create a dedicated Mailable class for reusable and structured email templates.
3) Can I use Gmail SMTP to send email in Laravel?
Yes. Set MAIL_HOST=smtp.gmail.com, MAIL_PORT=587, and MAIL_ENCRYPTION=tls in your .env, and use your Gmail credentials.
4) Why is Laravel mail not working with SMTP?
Check that your .env settings are correct, your email service allows SMTP, and the MAIL_MAILER is set to smtp.
5) How do I test email sending in Laravel?
You can create a route that sends a test mail and visit that route in the browser to check if it sends successfully.
6) Can I log emails instead of sending them in Laravel?
Yes. Set MAIL_MAILER=log in .env to log the email contents without actually sending them — useful for local testing.
7) Is it secure to use SMTP in Laravel?
Yes, if you use secure ports (587 with TLS or 465 with SSL) and avoid storing plain passwords — consider using Laravel’s secrets manager or environment variables.
Request a FREE Business Plan.
+91 ▼ The world is heading towards automation, it has it’s strong footsteps in Software testing as well. We at Blazingcoders perform automation testing with various tools such as Junit, Selenium, QTP/
Read MoreOffshore and Outsource jQuery Developers: Transform Your Front-End Development A website that is both interesting and easy to use is essential for success in the digital-first world of today. A cro
Read More
In this blog post, we are going to explain how to upload images in WordPress media. The steps to inserting an image into a blog post in WordPress are: Place the cursor where you want the image t
Read More