繁体   English   中英

Mailgun API 在 Laravel 8.54 中静默失败

[英]Mailgun API failing silently in Laravel 8.54

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

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

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

这是我的环境变量:

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('no-reply@example.com')
            ->markdown('emails.example');
    }
}

在我的控制器中,我正在执行以下操作:

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

谁能帮我发现问题? 我已经安装了 guzzle:“guzzlehttp/guzzle”:“^7.4”,所以这不应该是问题,即使是,至少会有某种错误。

帮助将不胜感激。 我对 Laravel 还是很陌生,所以我现在不知所措。

我做了一些挖掘,终于意识到了这个问题。 这是一个严重的新手错误。 查看vendor/laravel/src/Illuminate/Mail/Transport/MailgunTransport.php我意识到MailgunTransport::send不需要整个 API 基本 url,这是 Mailgun 为您提供的。 它只需要包含sandbox[number].mailgun.org的 url 部分。 我已将整个 API url 放在我的 .env 文件中。 因此,如果有任何其他新手难以理解为什么您的电子邮件没有发送,这可能是您的问题。 进行此更改解决了我的问题。

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

我也有这个问题。

Swift_TransportException
Request to Mailgun API failed.
http://localhost:3000/form-process

就我而言,这似乎是因为MAILGUN_ENDPOINT在我的 .env 文件中设置不正确。

Laravel 默认设置为:

MAILGUN_ENDPOINT=api.eu.mailgun.net

将其更改为

MAILGUN_ENDPOINT=api.mailgun.net

为我解决了这个问题。 希望这可以帮助某人。 不要忘记运行php artisan config:cache来重新加载你的 .env。

MAILGUN_ENDPOINT=api.mailgun.net添加到您的.env文件中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM