Created
February 6, 2018 20:14
-
-
Save jesobreira/a0630343afc60a9a4b231655a0a761e0 to your computer and use it in GitHub Desktop.
Send email using Sendgrid (no lib or curl needed)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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('[email protected]', '[email protected]', 'Hello', 'Hello from us!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment