laravel 在laravel错误中使用mailgun发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28286445/
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
Sending Email with mailgun in laravel error
提问by user3718908
Hello i've been simply trying to send an email in laravel i read the documentation and they made it seem so easy but whenever i try i keep getting error after error, i tried sendgrid didn't work and now i'm trying to use mailgun but i'm having issues with it as well.
您好,我一直只是尝试在 laravel 中发送电子邮件,我阅读了文档,它们使它看起来很简单,但是每当我尝试时,我总是在错误后不断出错,我尝试过 sendgrid 不起作用,现在我正在尝试使用mailgun 但我也有问题。
This is my code::
这是我的代码::
$data = array();
Mail::send('emails.auth.activate', $data, function($message)
{
$message->to('[email protected]', 'John Doe')->subject('This is a demo!');
});
This is the error i get:
这是我得到的错误:
GuzzleHttp \ Exception \ ClientException (400)
Client error response [url] https://api.mailgun.net/v2/mail.xxxxxxx.com/messages.mime [status code] 400 [reason phrase] BAD REQUEST
Mail Config:
邮件配置:
<?php
return array(
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "log"
|
*/
'driver' => 'mailgun',
'host' => 'sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org',
'port' => 587,
'from' => array('address' => '[email protected]', 'name' => 'Xxxxxxxx'),
'encryption' => 'tls',
'username' => '[email protected]',
'password' => 'xxxxxxxxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => true,
);
回答by srbhattarai
Follow these steps
按着这些次序
- First of all, install guzzle package by adding
"guzzlehttp/guzzle": "~4.0"
line insidecomposer.json
- Update composer using
composer update
- Create your account on mailgun from http://www.mailgun.com/. After creating account, kindly note the mail gun subdomain created like
sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org
and API key created likekey-65c33f1xxxxxxxxxxxxxxxxxxxx
Go to
config/services.php
file and replace'mailgun' => array( 'domain' => '', 'secret' => '', ),
with
'mailgun' => array( 'domain' => 'sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org', 'secret' => 'key-65c33f1xxxxxxxxxxxxxxxxxxxx', ),
If you want to create your own sub-domain, you can create and assign to domain (as an alternative)
Configure
config/mail.php
like this'driver' => 'mailgun', 'host' => 'smtp.mailgun.org', 'port' => 587, 'from' => array('address' => '[email protected]', 'name' => 'Xxxxxxxx'), 'encryption' => 'tls', 'username' => null, 'password' => null, 'sendmail' => '/usr/sbin/sendmail -bs', 'pretend' => false
- 首先,通过
"guzzlehttp/guzzle": "~4.0"
在里面添加行来安装guzzle包composer.json
- 使用更新作曲家
composer update
- 从http://www.mailgun.com/在 mailgun 上创建您的帐户。创建帐户后,请注意创建的邮件枪子域
sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org
和创建的 API 密钥类似key-65c33f1xxxxxxxxxxxxxxxxxxxx
转到
config/services.php
文件并替换'mailgun' => array( 'domain' => '', 'secret' => '', ),
和
'mailgun' => array( 'domain' => 'sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org', 'secret' => 'key-65c33f1xxxxxxxxxxxxxxxxxxxx', ),
如果您想创建自己的子域,您可以创建并分配给域(作为替代)
config/mail.php
像这样配置'driver' => 'mailgun', 'host' => 'smtp.mailgun.org', 'port' => 587, 'from' => array('address' => '[email protected]', 'name' => 'Xxxxxxxx'), 'encryption' => 'tls', 'username' => null, 'password' => null, 'sendmail' => '/usr/sbin/sendmail -bs', 'pretend' => false
Note that you do not need to supply username and password for this. Mailgun will handle this.
请注意,您不需要为此提供用户名和密码。Mailgun 会处理这个问题。
Try sending email now. Hope this helps. Good luck!
立即尝试发送电子邮件。希望这可以帮助。祝你好运!
回答by MMMTroy
Just wanted to add one possible reason for the error. I received this error while using sandbox mode when I hadn't set the authorized recipient yet. When I logged into Mailgun and added the intended recipient to the "Authorized Recipient" list the error went away.
只是想为错误添加一个可能的原因。当我尚未设置授权收件人时,我在使用沙箱模式时收到此错误。当我登录 Mailgun 并将预期收件人添加到“授权收件人”列表时,错误消失了。
回答by max234435
I had the same issue and kept getting the following error: Client error response [url] https://api.mailgun.net/v3//messages.mime404 not found
我遇到了同样的问题并不断收到以下错误:客户端错误响应 [url] https://api.mailgun.net/v3//messages.mime404 not found
There isn't much written about this error written online for Laravel 5.1 which I'm using. It turns out that in the config->services the default Laravel 5.1 installation comes with :
关于我正在使用的 Laravel 5.1 在线编写的这个错误的文章并不多。事实证明,在 config->services 中,默认的 Laravel 5.1 安装带有:
'domain' => env('');
'domain' => env('');
'secret' => env('');
'secret' => env('');
for some reason if you keep your domain and secret wrapped in env as per default installation, the MailGunTransport doesnt pick it up. So just set it to the following:
出于某种原因,如果您按照默认安装将域和机密保存在 env 中,则 MailGunTransport 不会拾取它。所以只需将其设置为以下内容:
domain' =>'yourdomain';
domain' =>'yourdomain';
'secret' => 'yoursecret';
'secret' => 'yoursecret';
Hope this helps since I'm sure i'm not the only one who probably run into this issue.
希望这会有所帮助,因为我确定我不是唯一可能遇到此问题的人。
回答by Mark Hague
I had this problem as I hadn't activated my mailgun account. Once activated all worked fine
我遇到了这个问题,因为我没有激活我的 mailgun 帐户。激活后一切正常
回答by Akshay Singh
You will be able to send mail to the persons in the same domain unless you have added them in the authorised recipient category. If you add someone to authorised recipient category then he/she would have to approve the request and then only would be able to receive the emails.
您将能够向同一域中的人员发送邮件,除非您已将他们添加到授权收件人类别中。如果您将某人添加到授权收件人类别,则他/她必须批准请求,然后才能接收电子邮件。
回答by M.J
I also stucked once in configuring the Mailgun in Laravel 5.1 and what worked for me is the following process. Hope its helps someone:
我也曾在 Laravel 5.1 中配置过 Mailgun,对我有用的是以下过程。希望它可以帮助某人:
1) Install guzzle package by adding "guzzlehttp/guzzle": "5.0" line inside composer.json like this:
1) 通过在 composer.json 中添加 "guzzlehttp/guzzle": "5.0" 行来安装 guzzle 包,如下所示:
"require": {
"guzzlehttp/guzzle": "~5.0"
},
2) Update composer using sudo composer update
in the terminal.
2)sudo composer update
在终端中使用更新作曲家。
3) Restart Apache.
3) 重启阿帕奇。
4) Create the account in http://www.mailgun.com. It will create a Sub Domain
and API Key
.
4) 在http://www.mailgun.com 中创建帐户。它将创建一个Sub Domain
和API Key
。
5) Add the Sub Domain
and API Key
in the .env
like this:
5)添加Sub Domain
并API Key
在.env
这样的:
MAILGUN_DOMAIN=sandbox8...........3b.mailgun.org
MAILGUN_SECRET=key-9..................04
6) And in the services.php add these line:
6) 在 services.php 中添加这些行:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
7) Now the mail.php as follows:
7)现在mail.php如下:
'driver' => 'mailgun',
'host' => 'smtp.mailgun.org',
'port' => 587,
'from' => ['address' => null, 'name' => null],
'encryption' => 'tls',
'username' => null,
'password' => null,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
I hope this works for you all.
我希望这对你们所有人都有效。