2016-07-05 44 views
0

作爲測試,當我訪問我的'/'路由時,我有相關的控制器發送測試郵件。不過,我收到此錯誤:使用Laravel 5.2連接Maligun API時發送郵件

客戶端錯誤:POST https://api.mailgun.net/v3/My domain name/messages.mime導致了401 UNAUTHORIZED響應: 故宮

這裏是我的控制器。

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

    \Mail::send('emails.test', $data, function($message) 
    { 
     $message->to('[email protected]')->subject('test email'); 
    }); 

} 

我認爲問題在於我設置的mailgun API。這裏是我的.env文件,我設置了我的郵件。我不確定我是否正確使用我的API密鑰和域名。

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 

這裏是我的mail.php文件

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

我也有這個在我的services.php文件

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

我composer.json文件

"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" 
}, 

回答

0

在您的mail.php中,您需要更改usernamepassword到:

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

爲了從.env讀取值。

此外,MAILGUN_SECRETMAIL_PASSWORD是不一樣的:

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

注:郵件端口是(對我來說工作正常)!

+0

感謝您的建議。但是,我仍然遇到同樣的錯誤。 – DanielPahor

+0

MAILGUN_SECRET和MAIL_PASSWORD不一樣。請檢查更新! –