Integrate SendGrid into your application for seamless email communication using the API method.
There are two ways to integrate:
- SMTP
- API (this documentation focuses on API integration)
- Visit the official SendGrid website.
- Navigate to Sender Authentication.
- Click "Create new Sender," complete all requirements, and ensure you provide a valid email for verification. Save the API key generated during this process.
- Delete any configuration for email if exists.
- Set the following variables in your
.env
file:MAIL_FROM_ADDRESS
MAIL_FROM_NAME
SENDGRID_API_KEY
Execute the following command to install the SendGrid package using Composer:
$ composer require sendgrid/sendgrid
- (new SendGridService())->sendMail("Reject Email", 'abanoubtalaat555@gmail.com',$data,'mail.reject_driver');
-
- Must Provide
-
-
- Email subject
-
-
-
- Email Address
-
-
-
- Data will pass it to view
-
-
-
- Email View
-
class SendGridService
{
public $email;
public function __construct()
{
$this->email = new \SendGrid\Mail\Mail();
}
public function sendMail($subject, $to, $data, $viewPath)
{
$this->email->setFrom(env("MAIL_FROM_ADDRESS"), env("MAIL_FROM_NAME"));
$this->email->setSubject($subject);
$this->email->addTo($to);
$this->email->addContent("text/html", view($viewPath, compact('data'))->render());
$sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
$sendgrid->send($this->email);
}
}