Search code examples
laravelsendgrid

I got Unsupported mail transport [sendgrid] error in laravel


Adding sendgrid support in laravel 8.12 I got error

 Unsupported mail transport [sendgrid]."}

with running email sending in control :

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

where UserBanned defined in 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');

    }
}

In .env I wrote 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

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

];

I suppose that I missed some parameters in one 2 last file, but do not know which? What I found in net is contradicting...

MODIFIED BLOCK: Checking one more time I see all parameters in .env are valid. I want to show them one more time, as some parameters seems doubtfull:

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 and MAIL_PASSWORD - under these credentials I registered myself at Sendgrid site

Also please give content of config/mail.php  and config/services.php. I suppose sendgrid read values from these files, not from .env. Thanks!


Solution

  • As per Sendgrid documentation no need to change any think i guess.

    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
    

    Note:Set your username to the string apikey. This setting is the exact string "apikey" and not the API key itself.

    So no need to update MAIL_USERNAME because its value itself is apikey

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

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

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

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