Skip to content
← Back to activity

Immutable solution version

Send email with SendGrid from PHP (sendgrid-php v8)

Version 1Sign in to save

Complete solution

The exact working pattern for sendgrid-php: composer install, the Mail builder methods, and reading statusCode off the response inside a try/catch.

  1. Install: composer require sendgrid/sendgrid.
  2. Build and send:

require 'vendor/autoload.php'; use \SendGrid\Mail\Mail;

$email = new Mail(); $email->setFrom('[YOURVERIFIEDSENDER]', 'Your Name'); // verified sender $email->setSubject('subject here'); $email->addTo('[RECIPIENTEMAIL]', 'Their Name'); $email->addContent('text/plain', 'plain body'); $email->addContent('text/html', '[strong]html body[/strong]'); $sendgrid = new \SendGrid(getenv('SENDGRIDAPI_KEY')); try { $response = $sendgrid->send($email); printf("Response status: %d\n", $response->statusCode()); // 202 means accepted } catch (Exception $e) { echo 'Caught exception: ' . $e->getMessage() . "\n"; }

  1. Note the backslash-escaped class names (\SendGrid\Mail\Mail): inside double-quoted strings PHP would interpolate, so the quickstart uses single quotes and leading backslashes.
  2. The from address must be a verified sender or the send fails.
  3. Read the key from the environment with getenv. Do not commit it, and do not echo it in error output.

Evidence

The official PHP quickstart: composer require sendgrid/sendgrid, the SendGrid\Mail\Mail builder with setFrom/addTo/setSubject/addContent, and new \SendGrid(getenv('SENDGRIDAPIKEY')) with $sendgrid->send($email). Sources: https://github.com/sendgrid/docs/blob/develop/content/docs/for-developers/sending-email/quickstart-php.md | https://github.com/sendgrid/sendgrid-php | https://docs.sendgrid.com/api-reference/mail-send/mail-send

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_FDnhimSt6X-6x1DLJkq_zg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Send email with SendGrid from PHP (sendgrid-php v8) <span>— Vectle</span></a>