JIJOMON K.A
JIJOMON K.A

Reputation: 1280

Send Mail Using Lumen

Mail.php

return [
 'driver' =>'smtp',
 'host' => 'smtp.gmail.com',
 //'port' => 587,
 'port' =>465,
 //'encryption' =>'tls',
 'encryption' =>'ssl',
 'username' => '[email protected]',
 'password' => 'xxxxxxx',
 // 'sendmail' => '/usr/sbin/sendmail -bs',
 'sendmail' => '/usr/sbin/sendmail -t',
 'markdown' => [
        'theme' => 'default',
        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
];

Controller

$data = []; // Empty array

        Mail::send('email.credentials', $data, function($message)
        {
            $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
        });

Error

Swift_TransportException Connection could not be established with host smtp.gmail.com [A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

I Tried...

Please help .

Thank you.

Upvotes: 3

Views: 10736

Answers (2)

DragonHeart26
DragonHeart26

Reputation: 66

Have you turned on 2 layers security in your Google account (email address you config in .env file) which uses to send email.

Upvotes: 0

Saravanan Nandhan
Saravanan Nandhan

Reputation: 612

1. Require illuminate/mail

Make sure you’re using the same version as your underlying framework (i.e. if you’re on Lumen v. 5.3, use composer require illuminate/mail "5.3.*").

composer require illuminate/mail "5.5.*"

2. Set up Lumen bootstrap/app.php

First, open your bootstrap.php and uncomment the following lines:

$app->withFacades();
$app->register(App\Providers\AppServiceProvider::class);

Also, add the following line below the last line you uncommented:

$app->configure('services');

This will allow you to define a ‘services’ config file and setup your mail service. Now I know that normally configuration is done in the .env file with Lumen, and we’ll use that shortly, but first we’ll need to write a small config file to map to the .env file.

3. Create your configuration files

Create a new folder at the root level of your install called config(if it doesn’t already exist). In the config folder, create two new files, one named services.php and another named **mail.php**.

In the services.php file paste the following:

<?php
return [
    'mailgun' => [
        'domain' => env('MAILGUN_DOMAIN'),
        'secret' => env('MAILGUN_SECRET'),
    ],
];

Lastly, add the following to your .env file:

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=<your-mailgun-domain>
MAILGUN_SECRET=<your-mailgun-api-key>

Make sure you replace those sneaky placeholders with your actual key and domain. 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.

4. Send Email!

To send an email, use one of the following in your classes (depending on your preference):

use Illuminate\Support\Facades\Mail;

    $data = []; // Empty array

    Mail::send('email.credentials', $data, function($message)
    {
        $message->to('[email protected]', 'Jon Doe')->subject('Welcome!');
    });

Finally, don’t forget to read the Laravel Mail docs for more info on how to use this great library.

Upvotes: 4

Related Questions