使用 Mailgun 在 Laravel 5.4 中发送邮件获取错误代码“401 UNAUTHORIZED` response: Forbidden”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43415563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:47:30  来源:igfitidea点击:

sending mail in Laravel 5.4 using Mailgun get error code " 401 UNAUTHORIZED` response: Forbidden "

phplaravelemailmailgun

提问by White Nuzzle

I'm trying to send mail in Laravel 5.4 project with Mailgun. I think I set the configuration correctly. But, I got this error message such as

我正在尝试使用 Mailgun 在 Laravel 5.4 项目中发送邮件。我想我正确设置了配置。但是,我收到此错误消息,例如

ClientException in RequestException.php line 111: Client error: POST https://api.mailgun.net/v3/sandboxfeb88d58f18841738b2fc81d7cbc7631.mailgun.org/messages.mime>resulted in a 401 UNAUTHORIZEDresponse: Forbidden

ClientException in RequestException.php line 111: Client error: POST https://api.mailgun.net/v3/sandboxfeb88d58f18841738b2fc81d7cbc7631.mailgun.org/messages.mime>resulted in a 401 UNAUTHORIZEDresponse: Forbidden

Here is my configuration:

这是我的配置:

in .env file

在 .env 文件中

MAIL_DRIVER=mailgun
MAILGUN_DOMAIN=sandboxfeb88d58f18841738b2fc81d7cbc7631.mailgun.org
MAILGUN_SECRET=pubkey-1767e**********

in mail.php file

在 mail.php 文件中

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Richi Htoo'),
],

in services.php file

在 services.php 文件中

'mailgun' => [
    'domain' => env('MAILGUN_DOMAIN'),
    'secret' => env('MAILGUN_SECRET'),
],

and I wrote mail sending code in default route such as

我在默认路由中编写了邮件发送代码,例如

Route::get('/', function () {
//return view('welcome');

    $data = [
        'title' => 'Hi student I hope you like the course',
        'content' => 'This laravel course was created with a lot of love and dedication for you'
    ];

    Mail::send('emails.test', $data, function($message){
        $message->to('[email protected]', 'White Nuzzle')->subject('Hello student how are you?');
    });
});

And I also installed Laravel Package "guzzlehttp/guzzle" version 6.2 to send mail.

而且我还安装了 Laravel Package "guzzlehttp/guzzle" 6.2 版来发送邮件。

But when I call that default home route, I got an error message as I mention above.

但是当我调用该默认主路由时,我收到了上面提到的错误消息。

I can't find any solution for my error in any where including this forum "stackoverflow.com".

我在任何地方都找不到任何解决我的错误的方法,包括这个论坛“stackoverflow.com”。

Can anyone help me please?

有人可以帮我吗?

回答by Mohammed Atif Sami

Frankly it was quite an ordeal, I made the sandbox worked as following

坦率地说,这是一场考验,我让沙箱工作如下

  1. Added authorized recipient - Apparently sandbox can't send email to anyone, you need to add them as authorized recipient.
  2. Use proper credentials in .env File added MAILGUN_DOMAINand MAILGUN_SECRETtoo as services.php uses them. Remember MAILGUN_SECRETis the private key and starts with key-, don't use public key here
  3. Put MAIL_DRIVER=mailgun MAIL_HOST=smtp.mailgun.org MAIL_PORT=587 MAIL_USERNAME=postmaster@sandboxcc*****************.mailgun.org MAIL_PASSWORD=****************** MAIL_ENCRYPTION=tls
  4. !!!MOST IMPORTANT!!!! RESTART YOUR SERVER to load the new .env file.
  1. 添加授权收件人 - 显然沙箱无法向任何人发送电子邮件,您需要将他们添加为授权收件人。
  2. 使用正确的凭据在加入.ENV文件MAILGUN_DOMAINMAILGUN_SECRET太为services.php使用它们。记住MAILGUN_SECRET是私钥,以 开头key-,不要在这里使用公钥
  3. MAIL_DRIVER=mailgun MAIL_HOST=smtp.mailgun.org MAIL_PORT=587 MAIL_USERNAME=postmaster@sandboxcc*****************.mailgun.org MAIL_PASSWORD=****************** MAIL_ENCRYPTION=tls
  4. !!!最重要的!!!!重新启动您的服务器以加载新的 .env 文件

回答by Rohit Jadhav

Make sure to check your credentials for the mailgun, make sure that those are correct.

确保检查您的 mailgun 凭据,确保这些凭据正确无误。

Dont copy the Public Validation Key. please copy the Private API Key

不要复制公共验证密钥。请复制私有 API 密钥

回答by Thomas

Default Mail Driver: Mailgun

默认邮件驱动程序:Mailgun

MAILGUN_DOMAIN=*your-domain*.mailgun.org

MAILGUN_SECRET=pubkey-*your-public-key*

Default Mail Driver: SMTP

默认邮件驱动程序:SMTP

MAIL_USERNAME=postmaster@*your-domain*.mailgun.org

MAIL_PASSWORD=*your-password*

Don't forget to php artisan config:clear

不要忘记 php artisan config:clear

回答by Fastmover

Personally I never got the sandbox account to work. I just did this a little over a month ago. Sandbox never worked, but the live account I created did. Try switching to the live account and let me know if that works for you.

就我个人而言,我从未让沙箱帐户正常工作。一个多月前我刚刚做了这个。沙盒从未奏效,但我创建的真实账户却奏效了。尝试切换到真实账户,让我知道这是否适合您。

回答by Kashif Saleem

Just go to the config folder. and made some changes..

只需转到配置文件夹。并做了一些改变..

'mailgun' => [
    'domain' => env('your_domainxxxxxxx.mailgun.org'),
    'secret' => env('key-xxxxxxxx_private_API_keyxxxx'),
],

error will be resolved.

错误将得到解决。