使用Laravel 5.2连接mailgun API时发送邮件

As a test, when i access my '/' route I have the relevant controller sending a test email out. However, I am getting this error:

Client error: POST https://api.mailgun.net/v3/My domain name/messages.mime resulted in a 401 UNAUTHORIZED response: Forbidden

Here is my controller.

public function index()
{
    $data = [];

    \Mail::send('emails.test', $data, function($message)
   {
        $message->to('my_test_email@gmail.com')->subject('test email');
   });

}

I think the issue lies in my setting up of the mailgun API. Here is my .env file where I have set-up my mail.I am unsure if I am using my API key and domain name correctly here.

MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=port number (which is different to that in the mail.php file)
MAIL_USERNAME=My domain name
MAIL_PASSWORD=My API Key
MAIL_ENCRYPTION=tls

Here is my mail.php file

'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'test@gmail.com', 'name' => 'Test'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('My domain name'),
'password' => env('My API Key'),
'sendmail' => '/usr/sbin/sendmail -bs',

I also have this in my services.php file

  'mailgun' => [
    'domain' => 'My domain name',
    'secret' => 'My API key',
],

My composer.json file

   "require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~4.0",
    "symfony/css-selector": "2.8.*|3.0.*",
    "symfony/dom-crawler": "2.8.*|3.0.*",
    "guzzlehttp/guzzle": "~5.3|~6.0"
},

In your mail.php you need to change username and password to:

'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),

In order to read values from .env.

Additionally, MAILGUN_SECRET and MAIL_PASSWORD are not the same:

'mailgun' => [
    'domain' => 'My domain name',
    'secret' => 'My API key', //<-- Here is not the same as MAIL_PASSWORD
],

Note: The Mail port is 587 (works fine for me) !

It was an error with my API key