微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在 Laravel 中收到不支持的邮件传输 [sendgrid] 错误

如何解决我在 Laravel 中收到不支持的邮件传输 [sendgrid] 错误

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

config/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。 谢谢!

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。