dongqing4774 2016-07-05 05:47
浏览 157
已采纳

使用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"
},
  • 写回答

2条回答 默认 最新

  • dongzhe6287 2016-07-05 05:52
    关注

    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) !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器