gpt4 book ai didi

php - Mailgun API 在 Laravel 8.54 中默默失败

转载 作者:行者123 更新时间:2023-12-03 08:14:25 26 4
gpt4 key购买 nike

我花了一整天的时间试图理解为什么我尝试使用 Mailgun API 发送邮件在我的 Laravel 8 项目中失败了。我知道这里还有其他问题也有类似的投诉,但到目前为止,这些其他问题的答案都没有帮助。

一个潜在的线索是,当我在 .env 文件中注释掉 mailgun 域和 secret 时,我没有收到任何错误。但我无法确定原因。

我正在使用美国位置的免费沙箱域。

这是我的环境变量:

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

服务.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'),
],

];

邮件.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"
|
*/

...

示例邮件.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('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82ecedaff0e7f2eefbc2e7fae3eff2eee7ace1edef" rel="noreferrer noopener nofollow">[email protected]</a>')
->markdown('emails.example');
}
}

在我的 Controller 中,我正在执行以下操作:

Mail::to('[my hard-coded email]')->send(new ExampleMail());

谁能帮我找出问题所在吗?我安装了 guzzle: "guzzlehttp/guzzle": "^7.4",所以这不应该是问题,即使是问题,至少也会出现某种错误。

帮助将不胜感激。我对 Laravel 还很陌生,所以目前我有点不知所措。

最佳答案

我刚刚遇到了同样的问题...您也可以按 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 行的注释

关于php - Mailgun API 在 Laravel 8.54 中默默失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69888631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com