首页 > 解决方案 > 我在 laravel 中遇到不支持的邮件传输 [sendgrid] 错误

问题描述

在 laravel 8.12 中添加 sendgrid 支持出现错误

 Unsupported mail transport [sendgrid]."}

在控制中运行电子邮件发送:

Mail::to($bannedUser)->send(new UserBanned($subject, $additiveVars));

其中 UserBanned 在 app/Mail/UserBanned.php 中定义:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class UserBanned extends Mailable
{
    use Queueable, SerializesModels;

    public $subject;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(string $subject)
    {
        $this->subject = $subject;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('view.emails.user_banned');

    }
}

在 .env 中,我写了 sendgrid credenatils:

SENDGRID_API_KEY = 'MY_API_KEY'
MAIL_DRIVER=smtp

MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587

MAIL_FROM_ADDRESS=""
MAIL_FROM_NAME="COMPANY_NAME"

MAIL_USERNAME=MY_SENDGRID_ACCOUNT
MAIL_PASSWORD=MY_SENDGRID_PASSWORD

MAIL_ENCRYPTION=tls
MAIL_MAILER=smtp

在 config/mail.php 中:

<?php
return [
    'driver' => 'sendgrid',


    'mailers' => [
        'sendgrid' => [
            'transport' => 'sendgrid',
        ],
    ],

    /*
   |--------------------------------------------------------------------------
   | SMTP Host Address
   |--------------------------------------------------------------------------
   |
   | Here you may provide the host address of the SMTP server used by your
   | applications. A default option is provided that is compatible with
   | the Mailgun mail service which will provide reliable deliveries.
   |
   */

    'host' => env('MAIL_HOST', 'smtp.sendgrid.net'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Host Port
    |--------------------------------------------------------------------------
    |
    | This is the SMTP port used by your application to deliver e-mails to
    | users of the application. Like the host we have set this value to
    | stay compatible with the Mailgun e-mail application by default.
    |
    */

    'port' => env('MAIL_PORT', 587),

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */




    'from' => [
        'address' => env('MAIL_FROM_ADDRESS'),
        'name' => env('MAIL_FROM_NAME'),
    ],

    /*
    |--------------------------------------------------------------------------
    | E-Mail Encryption Protocol
    |--------------------------------------------------------------------------
    |
    | Here you may specify the encryption protocol that should be used when
    | the application send e-mail messages. A sensible default using the
    | transport layer security protocol should provide great security.
    |
    */

    'encryption' => env('MAIL_ENCRYPTION', 'tls'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Username
    |--------------------------------------------------------------------------
    |
    | If your SMTP server requires a username for authentication, you should
    | set it here. This will get used to authenticate with your server on
    | connection. You may also set the "password" value below this one.
    |
    */

    'username' => env('MAIL_USERNAME'),

    'password' => env('MAIL_PASSWORD'),

    /*
    |--------------------------------------------------------------------------
    | Sendmail System Path
    |--------------------------------------------------------------------------
    |
    | When using the "sendmail" driver to send e-mails, we will need to know
    | the path to where Sendmail lives on this server. A default path has
    | been provided here, which will work well on most of your systems.
    |
    */

    'sendmail' => '/usr/sbin/sendmail -bs',

    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Log Channel
    |--------------------------------------------------------------------------
    |
    | If you are using the "log" driver, you may specify the logging channel
    | if you prefer to keep mail messages separate from other log entries
    | for simpler reading. Otherwise, the default channel will be used.
    |
    */

    'log_channel' => env('MAIL_LOG_CHANNEL'),
];

配置/服务.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.
    |
    */

    'sendgrid' => [
        'api_key' => env('SENDGRID_API_KEY'),
    ],

//    'mailgun' => [
//        'domain' => env('MAILGUN_DOMAIN'),
//        'secret' => env('MAILGUN_SECRET'),
//        'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'),
//    ],

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

];

我想我错过了最后一个文件中的一些参数,但不知道是哪个?我在网上发现的东西是矛盾的......

修改块: 再检查一次,我发现 .env 中的所有参数都有效。我想再给他们看一次,因为有些参数似乎很可疑:

MAIL_MAILER=smtp
SENDGRID_API_KEY = "SG.XXXXXXXX.XXXXXXXXXXXXXX"

MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587

MAIL_FROM_ADDRESS=myname@mycompany.com
MAIL_FROM_NAME="client company support"

MAIL_USERNAME=myname@mycompany.com
MAIL_PASSWORD=paswordIFilledEegisteingAtSendgridSite

MAIL_ENCRYPTION=tls

MAIL_USERNAME 和 MAIL_PASSWORD - 根据这些凭据,我在 Sendgrid 网站上注册了自己

另外请给出config/mail.php 和config/services.php 的内容。我想 sendgrid 从这些文件中读取值,而不是从 .env 中读取值。谢谢!

标签: laravelsendgrid

解决方案


根据 Sendgrid 文档,我认为无需更改任何内容。

MAIL_MAILER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="John Smith"
MAIL_FROM_ADDRESS=from@example.com

注意:将您的用户名设置为字符串apikey。此设置是确切的字符串“apikey”,而不是 API 密钥本身。

所以不需要更新MAIL_USERNAME,因为它的价值本身就是apikey

参考:https ://docs.sendgrid.com/for-developers/sending-email/laravel

参考:https ://docs.sendgrid.com/for-developers/sending-email/integrating-with-the-smtp-api

参考:https ://docs.sendgrid.com/ui/sending-email/senders

更新 了 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', 'smtp'),

    /*
    |--------------------------------------------------------------------------
    | 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"
    |
    */

    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
        ],

        'ses' => [
            'transport' => 'ses',
        ],

        'mailgun' => [
            'transport' => 'mailgun',
        ],

        'postmark' => [
            'transport' => 'postmark',
        ],

        'sendmail' => [
            'transport' => 'sendmail',
            'path' => '/usr/sbin/sendmail -bs',
        ],

        'log' => [
            'transport' => 'log',
            'channel' => env('MAIL_LOG_CHANNEL'),
        ],

        'array' => [
            'transport' => 'array',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Global "From" Address
    |--------------------------------------------------------------------------
    |
    | You may wish for all e-mails sent by your application to be sent from
    | the same address. Here, you may specify a name and address that is
    | used globally for all e-mails that are sent by your application.
    |
    */

    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
        'name' => env('MAIL_FROM_NAME', 'Example'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Markdown Mail Settings
    |--------------------------------------------------------------------------
    |
    | If you are using Markdown based email rendering, you may configure your
    | theme and component paths here, allowing you to customize the design
    | of the emails. Or, you may simply stick with the Laravel defaults!
    |
    */

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],

];

推荐阅读