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

报告相同问题?

悬赏问题

  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1