Laravel

Send Email Using Smtp In Laravel With Example

Hello buddy, I hope you are doing well in this article we will learn about how to send email using SMTP in laravel. Well we know we need to configure SMTP if we want to send an email on a live server.

I will give you an example on how to send an email in laravel 6,laravel 7,laravel 8,laravel 9, and laravel 10, by using smtp server of godaddy and hostinger

I will show you the easiest way to send email in laravel via godaddy and hostinger shared hosting.

To send an email from your laravel please add the below code in your controller.First add the below namespace on the top of your controller.

use Illuminate\Support\Facades\Mail;

Then in your controller, inside the function add the below code.

$data = [ 
            'name' => 'Shaiv Roy',
            'email' => 'equiry@codehunger.in'
        ]
        Mail::send('mail', $data, function($message) {
            $message->to(env('MAIL_TO'), 'CodeHunger Enquiry')->subject
               ('Quick Apply Data');
         });

Now, I am going to explain the above code, here $data is the array that we are passing in our mail facade as a parameter, then mail inside the send function is the blade file name which must be present inside your resources/views.

Below is the mail.blade.php code, which we created inside resources/views.

<h3>Contact Form Data</h3>
<p>name: {{$name ?? ''}}</p>
<p>email: {{$email ?? ''}}</p>

Hostinger SMTP settings

go to your .env and add the below code

MAIL_FROM_ADDRESS=noreply@codehunger.in
MAIL_FROM_NAME='COdehunger'
MAIL_MAILER=smtp
MAIL_HOST=smtp.hostinger.com
MAIL_PORT=587
MAIL_USERNAME=noreply@codehunger.in
MAIL_PASSWORD=Noreply@123
MAIL_ENCRYPTION=
MAIL_TO=enquiry@codehunger.in

You have to replace the above data with your once, and then you are ready to send email via hostinger smtp server.

Godaddy SMTP settings

 MAIL_DRIVER=smtp
 MAIL_HOST=localhost
 MAIL_PORT=25
 MAIL_USERNAME=mail@domainname.in
 MAIL_PASSWORD=yourpassword
 MAIL_ENCRYPTION=
 MAIL_FROM_ADDRESS=mailname@domainname.in
 MAIL_FROM_NAME="mail name"

Note: If you are new and planning to purchase any hosting services go for hostinger, as hostinger smtp is seamlessly fast.You can also buy from this link as it gives you 35% off on purchase.

And don’t forget to add CODE007 to get an extra discount at checkout.

I hope now you understood how to send email in laravel via smtp in godaddy and in hostinger.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button