To send email using SendGrid in Laravel, you’ll need to do the following:
- Install the SendGrid PHP Library: You can install the SendGrid PHP library using Composer by running the following command:
composer require sendgrid/sendgrid
- Configure your SendGrid API Key: You’ll need to obtain an API key from SendGrid and add it to your Laravel application as an environment variable. You can find instructions for obtaining an API key in the SendGrid documentation.
- Set up the Mail driver: In your Laravel application’s
.env
file, set theMAIL_DRIVER
variable tosendgrid
. You’ll also need to set theSENDGRID_API_KEY
variable to your SendGrid API key. - Use the Mail facade to send email: Once you’ve set up the Mail driver and configured your SendGrid API key, you can use the Laravel Mail facade to send email. Here’s an example of how you might use the Mail facade to send an email using SendGrid:
use Illuminate\Support\Facades\Mail;
$to = '[email protected]';
$subject = 'Hello from SendGrid';
$body = 'This is a test email sent using SendGrid and Laravel.';
Mail::send([], [], function ($message) use ($to, $subject, $body) {
$message->to($to)
->subject($subject)
->setBody($body);
});