我花了一整天的时间试图理解为什么我在Laravel 8项目中尝试使用Mailgun API发送邮件时会默默失败。我知道这里还有其他类似的问题,但到目前为止,这些问题的答案都没有帮助。
一个潜在的线索是,当我注解掉.env文件中的mailgun域和secret时,我没有得到任何错误,但我不能确定原因。
我正在使用免费的沙盒域与美国的位置。
下面是我的env变量:
MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_NAME="${APP_NAME}"
MAILGUN_DOMAIN=https://api.mailgun.net/v3/....mailgun.org
MAILGUN_SECRET=5...9
services.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Third Party Services
|--------------------------------------------------------------------------
|
| This file is for storing the credentials for third party services such
| as Mailgun, Postmark, AWS and more. This file provides the de facto
| location for this type of information, allowing packages to have
| a conventional file to locate the various service credentials.
|
*/
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
'postmark' => [
'token' => env('POSTMARK_TOKEN'),
],
'ses' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],
];
mail.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Mailer
|--------------------------------------------------------------------------
|
| This option controls the default mailer that is used to send any email
| messages sent by your application. Alternative mailers may be setup
| and used as needed; however, this mailer will be used by default.
|
*/
'default' => env('MAIL_MAILER', 'mailgun'),
/*
|--------------------------------------------------------------------------
| Mailer Configurations
|--------------------------------------------------------------------------
|
| Here you may configure all of the mailers used by your application plus
| their respective settings. Several examples have been configured for
| you and you are free to add your own as your application requires.
|
| Laravel supports a variety of mail "transport" drivers to be used while
| sending an e-mail. You will specify which one you are using for your
| mailers below. You are free to add additional mailers as required.
|
| Supported: "smtp", "sendmail", "mailgun", "ses",
| "postmark", "log", "array"
|
*/
...
ExampleMail.php:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class ExampleMail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this
->from('no-reply@example.com')
->markdown('emails.example');
}
}
在我的控制器中,我正在执行以下操作:
Mail::to('[my hard-coded email]')->send(new ExampleMail());
谁能帮我解决这个问题?我已经安装了Guzzle:“guzzlehttp/guzzle”:“^7.4”,所以这不应该是问题,即使是,至少会有某种错误。
我对Laravel还是个新手,所以我现在还有点不知所措。
5条答案
按热度按时间ux6nzvsh1#
我做了一些研究,终于意识到了这个问题。这是一个主要的新手错误。看看
vendor/laravel/src/Illuminate/Mail/Transport/MailgunTransport.php
,我意识到MailgunTransport::send
不需要整个API库URL,这就是Mailgun提供给你的。它只需要包含sandbox[number].mailgun.org
的部分URL。我已经把整个API URL放在了我的.env文件中。所以,如果有任何其他新手在那里挣扎着理解为什么你的电子邮件不发送,这可能是你的问题。x7rlezfr2#
我刚刚遇到了同样的问题...你也可以按
Ctrl+p
,然后键入MailgunTransport.php
,然后按Enter
,它将打开mailgun传输文件,或者只是导航vendor>laravel>framework>src>illumate>Mail>Transport
并打开MailgunTransport.php
文件。在第80行注解掉这个
// throw new Swift_TransportException('Request to Mailgun API failed.', $e->getCode(), $e);
并用dd($e)
替换它,然后再次运行脚本。这将显示您遇到的确切问题,并且用这种方式修复要容易得多,而不是像Request to Mailgun API failed.
这样模糊的错误消息。不要忘记在解决任何问题后取消第80行的评论
wmtdaxz33#
我也有这个问题。
在我的情况下,这似乎是因为
MAILGUN_ENDPOINT
在我的.env文件中设置不正确。Laravel默认设置为:
改成
我解决了这个问题。希望这能帮助到别人。不要忘记运行
php artisan config:cache
来重新加载你的. env。xtfmy6hx4#
将
MAILGUN_ENDPOINT=api.mailgun.net
添加到.env
文件ibps3vxo5#
我遇到了同样的问题,我无法解决它,我找到了一个临时的解决方案,直到我找到一个解决方案:在审批环境中,MAIL_DRIVER=mailgun正常工作,在生产环境中,我将驱动程序设置为MAIL_DRIVER=SMTP