Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Created February 6, 2018 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesobreira/a0630343afc60a9a4b231655a0a761e0 to your computer and use it in GitHub Desktop.
Save jesobreira/a0630343afc60a9a4b231655a0a761e0 to your computer and use it in GitHub Desktop.
Send email using Sendgrid (no lib or curl needed)
<?php
define('SENDGRID_KEY', 'SG.your api key here');
function sendgrid($from, $to, $subject, $message) {
$postdata = json_encode(
array(
'personalizations' => [
[
'to' => [
['email' => $to]
]
]
],
'from' => [
'email' => $from,
],
'subject' => $subject,
'content' => [ [
'type' => 'text/html',
'value' => $message
] ]
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/json'."\r\n"
.'Authorization: Bearer '.SENDGRID_KEY."\r\n",
'content' => $postdata
)
);
$context = stream_context_create($opts);
return file_get_contents('https://api.sendgrid.com/v3/mail/send', false, $context);
}
sendgrid('noreply@yoursite.com', 'yourclient@email.com', 'Hello', 'Hello from us!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment