<?php $apiUrl = "https://sendgrid.com/api/mail.send.json"; $dateTime = date('Y/m/d h:i:s'); $sendGridParams = array( 'api_user' => 'sendGridUserNameGoesHere' ,'api_key' => 'sendGridPasswordGoesHere' ,'to' => 'CustomerEmail@gmail.com' ,'toname' => 'Customer Name' ,'subject' => 'Test - ' . $dateTime ,'html' => 'This is a <B>test!</B> <BR> Sent: '. $dateTime ,'from' => 'DoNotRepl@YourCompanyWebsite.com' ,'fromname' => 'Your Company' //optional add'l email headers //,'headers' => json_encode(array('X-Accept-Language'=>'en')) ); $query = http_build_query($sendGridParams); $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => $apiUrl . '?' . $query ,CURLOPT_RETURNTRANSFER => true ,CURLOPT_SSL_VERIFYPEER => true ,CURLOPT_SSL_VERIFYHOST => 2 //download latest CA bundle from http://curl.haxx.se/docs/caextract.html ,CURLOPT_CAINFO => dirname(__FILE__) . '\cacert.pem' )); if(FALSE === $curlResponse = curl_exec($curl)){ die("API call failed! cURL error " . curl_errno($curl) . " " . curl_error($curl)); } curl_close($curl); if(NULL === $decodedResponse = json_decode($curlResponse,true)){ die("Error decoding API response, raw text: " . $curlResponse); } if( $decodedResponse['message'] === "success"){ echo "E-Mail Sent!"; }else{ echo "API Returned errors! <BR>"; if(is_array($decodedResponse['errors'])){ foreach($decodedResponse['errors'] as $error){ echo $error , "<BR>"; } } } ?> |
Comments
Leave a comment Trackback