【发布时间】: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 中的值。 谢谢!
【问题讨论】: