PASSWORD RESET

Your destination for complete Tech news

PHP

How to send an email using Sendgrid in Laravel?

502 0
< 1 min read

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 the MAIL_DRIVER variable to sendgrid. You’ll also need to set the SENDGRID_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);
});

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.