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

时间:2016-07-05 05:47:13

标签: php laravel mailgun

作为测试,当我访问我的'/'路线时,我让相关的控制器发送测试邮件。但是,我收到了这个错误:

客户端错误: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('my_test_email@gmail.com')->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' => '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',

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

2 个答案:

答案 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
],

注意:邮件端口 587 (对我来说很好)!

答案 1 :(得分:0)

我的API密钥错误