How to send emails with Laravel

First of all, Make sure you are using one of Laravel’s email drivers from the Laravel website.

In this example we will setup a basic Cpanel mail account:

Add these details in your .ENV file

MAIL_DRIVER=smtp
MAIL_HOST=mail.mywebsite.com
MAIL_PORT=587
MAIL_USERNAME=do-not-reply@mywebsite.com
MAIL_PASSWORD=123passwordhey
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=do-not-reply@mywebsite.com
MAIL_FROM_NAME=mywebsite.com

These environment files will be then pass to your config/mail.php file.

Remember that I have added manually 2 new environment variables which are:

MAIL_FROM_ADDRESS=do-not-reply@mywebsite.com
MAIL_FROM_NAME=mywebsite.com

So make sure you re-add them inside your config/mail.php file as:

If you wish to add more env variables, simply add them in the .ENV file and then create a copy in the config/mail file. example.

If you add

MAIL_TITLE=TitleHere

You will then need to add this in your config/mail.php file:

'custom' => [
'mail_title' => env('MAIL_TITLE'),
],

Then to use this variable in any of your controllers, write this:

config('custom.mail_title');

Now run this command(This example is to create a registration activation link email):

php artisan make:mail RegistrationEmail

When this command runs, it creates a mail file in:

Http/mail/RegistrationEmail.php

Open the file, you should see something like that:

view('view.name');
}
}

Now this check this out, if you want to send your controller data to the maillable template, you will need to add this in your controller:

$user = User::findOrFail($user->id);
Mail::to($user->email)->send(new RegistrationEmail($user));

What you are doing here is pass the information from the $user array/collection to the “RegistrationEmail” Maillable.

But here there is a problem, you will need to move these variables back to the view, how to do this?

Create a new view file called:

views/emails/registrationVerifyLink.blade.php

And add this code inside, MAKE SURE YOU DO NOT HAVE A SINGLE BLANK SPACE ON THE LEFT SIDE OF YOUR IDE/FILE OR THE MAILLABLE WILL FAIL TO READ YOUR CODE!!! I HAVE SPENT AGES LOOKING FOR A PROBLEM IN THE PAST!!

@component('mail::message')

Welcome to {{ config(‘app.name’) }}!

@component(‘mail::panel’)

Hey {{ ucfirst($user->name) }}!

To activate your account, click on the link!

@endcomponent

@component(‘mail::button’, [‘url’ => ”.config(“global.activation_url”).’/’.$user->email.’/’.$user->activationtoken.”])
Click to activate
@endcomponent

Big Thanks From
{{ config(‘app.name’) }}

@endcomponent

Now return to your “RegistrationEmail” Maillable and fill it up with this new code:

user = $user;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from(config('mail.from.address'))
->subject(config('mail.from.name'). ' account verification')
->markdown('emails.registrationVerifyLink');
}
}

As you can see, we are passing the $user variable as a construct and send it to our view.

That’s it, job done! Please leave me a comment below!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Disclaimer:

As an Amazon Associate I earn from qualifying purchases. This post may contain affiliate links which means I may receive a commission for purchases made through links.