Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

  • Change ssl / tls

  • Change the ports

  • Add "guzzlehttp/guzzle": "~5.3|~6.0" in composer.json

  • Add a new line in StreamBuffer.php

    $options = array_merge($options, array('ssl' => array('verify_peer' => false,'verify_peer_name' => false,'allow_self_signed' => true )));

Please help .

Thank you.

like image 539
JIJOMON K.A Avatar asked Jan 28 '26 09:01

JIJOMON K.A


1 Answers

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.

like image 176
Saravanan Nandhan Avatar answered Jan 30 '26 23:01

Saravanan Nandhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!