Skip to content

Instantly share code, notes, and snippets.

@ImtiazEpu
Last active February 14, 2023 19:43
Show Gist options
  • Save ImtiazEpu/d7bb6408bfeb5615933163bc0bd30337 to your computer and use it in GitHub Desktop.
Save ImtiazEpu/d7bb6408bfeb5615933163bc0bd30337 to your computer and use it in GitHub Desktop.
How to setup sendgrid email in Laravel

Setup sendgrid email in Laravel

Here are the steps to set up SendGrid email in Laravel:

  1. Sign up for a SendGrid account and obtain an API key.

  2. Add the SendGrid API key to your Laravel environment file (.env) by adding the following line:

SENDGRID_API_KEY=YOUR_SENDGRID_API_KEY
  1. In your Laravel project, install the official SendGrid library by running the following command in your terminal:
composer require sendgrid/sendgrid
  1. Open the config/mail.php file and update the driver option to sendgrid.

  2. In the config/services.php file, add the following code to define the SendGrid API key:

'sendgrid' => [
    'api_key' => env('SENDGRID_API_KEY'),
],
  1. To test the setup, you can use the following code in a controller or route:
use Illuminate\Support\Facades\Mail;

$data = [
    'email' => 'test@example.com',
    'name' => 'Test User',
    'subject' => 'Test email from SendGrid',
    'body' => 'This is a test email sent from SendGrid.',
];

Mail::send('emails.test', $data, function ($message) use ($data) {
    $message->to($data['email'], $data['name'])->subject($data['subject']);
});

This should send an email using SendGrid. If everything is set up correctly, you should receive an email with the message "This is a test email sent from SendGrid."

Note: You might need to create an email view file (e.g., resources/views/emails/test.blade.php) to render the content of the email.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment