How to send Email in Lumen using Mailgun.
Now we are sending Emails using the mailgun.
Having difficulties setting it up?

In this tutorial, we will discuss the configuration of the mail system using mailgun in lumen 5.8
To get started we follow simple steps,
1) Require illuminate/mail
composer require illuminate/mail “5.8.*”
Just make sure that you are using the same version if not please enter your version of the lumen framework.
2) You just need to set up the bootstrap/app.php
In bootstrap, you need to uncomment these few lines of codes.
$app->withFacades();$app->register(App\Providers\AppServiceProvider::class);
Then we need to add more lines of code in bootstrap/app.php
$app->configure('services');
$app->configure('mail');
And also You need to add some aliases in your bootstrap/app.php
$app->alias('mail.manager', Illuminate\Mail\MailManager::class);
$app->alias('mail.manager', Illuminate\Contracts\Mail\Factory::class);
$app->alias('mailer', Illuminate\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\Mailer::class);
$app->alias('mailer', Illuminate\Contracts\Mail\MailQueue::class);
Then we need to make two files which are services.php and mail.php in our config folder. You asked why we will need to make these files?
This is to map the env file with our services.
You just need to add these codes in services.php
<?php
return [
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
'endpoint' => env('MAILGUN_ENDPOINT')
],
In you mail.php
Add these codes
<?php
return [
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', ''),
'name' => env('MAIL_FROM_NAME', ''),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => env('MAIL_PRETEND', false),
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
3) Add this Configuration in your env
customize your configuration according to your variables, If you’re not using Mailgun, you can always use the other mail providers Mail comes with; have a look at the docs if you plan on using a different provider, they are all easy to set up once you’re at this point.
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=YourAddress
MAIL_FROM_NAME="Your from name"
MAILGUN_DOMAIN=yourmailgundomain
MAILGUN_SECRET=mailgunsecretkey
MAILGUN_ENDPOINT=api.mailgun.net
4) Make Templates and Make Email files same as the Laravel
Make a folder Email in Your App Folder and make a PHP class file DemoMail.In DemoMail you need to add the lines of codes.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class DemoMail extends Mailable
{
use Queueable,SerializesModels;
public $data;
public function __construct($data){
$this->data = $data;
}
public function build()
{
return $this->subject('Email Sent Successfull')->view('emails.registration.welcome_email')
->with([
'user' => $this->data
]);
}
}
Then You need to make a template in Views/Emails file Demo.blade.php
Paste the code
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h2>Hello {{$user->name}}</h2>
<h4>Welcome to the Demo Mail</h4>
<p>Your demo is completed.</p>
</body>
</html>use Illuminate\Support\Facades\Mail;
use App\Mail\UserMail;
5) Make a call in Controller
use App\Mail\UserMail;use Illuminate\Support\Facades\Mail;
And whenever You want to send mail You can call this instance.
Mail::to($request->emailAddress)->send(new UserMail($request));
Finally, don’t forget to read the Laravel Mail docs for more info on how to use this great library.
Thanks,
Tech Sagar Verma!