【问题标题】:I got Unsupported mail transport [sendgrid] error in laravel我在 laravel 中遇到不支持的邮件传输 [sendgrid] 错误
【发布时间】:2021-06-24 14:18:04
【问题描述】:

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

 Unsupported mail transport [sendgrid]."}

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

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

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

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

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

    '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 中的值。 谢谢!

【问题讨论】:

    标签: laravel sendgrid


    【解决方案1】:

    根据 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

    更新 邮件.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'),
            ],
        ],
    
    ];
    

    【讨论】:

    • 只有你需要改变的是 MAIL_PASSWORD,MAIL_FROM_NAME,MAIL_FROM_ADDRESS。还要确保从你必须添加到 sendgrid 的地址
    • 我也发布了mail.php文件
    • 不是,应该是 MAIL_USERNAME=apikey
    • 关注此视频并在此处添加地址电子邮件docs.sendgrid.com/ui/sending-email/senders
    • 我不认为会有区别
    【解决方案2】:

    我的 Laravel 9 和 AWS 邮件设置也面临同样的问题。

    请记住,Laravel 文档中的任何地方都没有说明您必须添加

    'transport' => 'ses',
    

    在你的 laravel mail.php 配置中,但这是最重要的一点。 因为没有它你总是会收到上述错误。

    所以总体上你的配置应该是这样的:

    'ses' => [
                'transport' => 'ses',
                'key' => env('AWS_ACCESS_KEY_ID'),
                'secret' => env('AWS_SECRET_ACCESS_KEY'),
                'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
                'version' => '2010-12-01',
            ],
    

    不要为get运行:

    php artisan config:cache
    

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 2021-12-31
      • 1970-01-01
      • 2019-02-04
      • 2022-12-13
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多