'cannot send mail using mailgun

I am using mailgun driver in laravel app and tried to send mail. But it gives me this error

Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required

config/mail.php

'driver' => env('mailgun', 'smtp'),
'from' => [
        'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

.env

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

services.php

'mailgun' => [
        'domain' => env('mydomain.com.pk'),
        'secret' => env('key-********************'),
    ],

What is wrong with my settings and is there anything else you need to check? Please help I have been trying to study laracast for past 2 days but it doesn't send mail.



Solution 1:[1]

Your config is all messed up. This part in mail.php:

'driver' => env('mailgun', 'smtp'),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

Should've stayed the way it initially was. Then you change the configuration in the .env file in the project root. So if you have in your mail.phpthe following line:

'driver' => env('MAIL_DRIVER', 'smtp'),

Then in your .env file you can change the value of the property MAIL_DRIVER to mailgun.

In laravel 5.4 the mail part of my .env file looks something like this:

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=verysecretstring
MAIL_ENCRYPTION=tls
MAILGUN_DOMAIN=somelongstring.mailgun.org
MAILGUN_SECRET=key-somelongstring

And I have not changed anything in the files in the config directory. The values for all of these variables you can find under your domain if you login to mailgun.

Also, read the documentation https://laravel.com/docs/5.3/mail or try to find a tutorial about setting up mailgun with laravel.

Solution 2:[2]

You should look at how to use environment configurations as described in the documentation:

When using the env() function, the first argument should be the name of the variable as defined in your .env file, while the second argument is the default.

Thus, in your config/mail.php and services.php you should be referring to variables which you have defined in your env() file as follows:

config/mail.php

'driver' => env('MAIL_DRIVER', 'smtp'),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS'),
    'name' => env('MAIL_FROM_NAME'),
],

services.php

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],

.env

MAIL_DRIVER=mailgun
[email protected]
MAIL_FROM_NAME=Example
MAILGUN_DOMAIN=mydomain.com.pk
MAILGUN_SECRET=key-********************

Note that I often do not specify default values for, for example, your MAIL_FROM_ADDRESS since this will mostly vary from installation to installation. Further, you should never store API secrets like MAILGUN_SECRET in files like services.php which are often version controlled.

Solution 3:[3]

use their lib

install?

php composer.phar require mailgun/mailgun-php php-http/guzzle6-adapter php-http/message

include?

require 'vendor/autoload.php';
use Mailgun\Mailgun;

new object?

$mgClient = new Mailgun('YOUR_API_KEY');

See Example https://documentation.mailgun.com/api-sending.html#examples

Solution 4:[4]

To me it was that I was using an incorrect value for MAILGUN_SECRET env variable, at first I was using a key that I received through email but the required value was the api secret key that is way longer. Api secret key can be found in: Settings -> API Keys -> Private API key at mailgun.org

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 René
Solution 2 Radical
Solution 3 Vin
Solution 4 Ekeuwei