问题描述
你好,我一直只是想在laravel中发送电子邮件,我阅读了文档,它们看起来非常简单,但是每当我尝试一次又一次地出错时,我尝试sendgrid无效,现在我尝试使用mailgun,但我也遇到了问题.
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.
这是我的代码:
$data = array();
Mail::send('emails.auth.activate', $data, function($message)
{
$message->to('[email protected]', 'John Doe')->subject('This is a demo!');
});
这是我得到的错误:
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
邮件配置:
<?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,
);
推荐答案
执行以下步骤
- 首先,通过在
composer.json
内添加 - 使用
composer update
更新作曲家 - 通过 http://www.mailgun.com/在mailgun上创建帐户.创建帐户后,请注意创建的邮件枪子域(如
sandboxXXXXXXXXXXXXXXXXXXXXXXXXXXXX.mailgun.org
)和API密钥(如key-65c33f1xxxxxxxxxxxxxxxxxxxx
) -
转到
config/services.php
文件并替换
"guzzlehttp/guzzle": "~4.0"
行来安装guzzle软件包- 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' => '',
),
使用
'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)
像这样配置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
请注意,您无需为此提供用户名和密码. Mailgun将处理此问题.
Note that you do not need to supply username and password for this. Mailgun will handle this.
尝试立即发送电子邮件.希望这可以帮助.祝你好运!
Try sending email now. Hope this helps. Good luck!
这篇关于在laravel中使用mailgun发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!