このライブラリはV2 APIをサポートしませんが、旧バージョンのライブラリを利用することでV2 APIを利用可能です。詳細については Continue Using V2 in PHPを参照してください。
SendGrid PHPライブラリの利用
123456789101112
// using SendGrid's PHP Library// https://github.com/sendgrid/sendgrid-phprequire'vendor/autoload.php';$sendgrid=newSendGrid("SENDGRID_APIKEY");$email=newSendGrid\Email();$email->addTo("test@sendgrid.com")->setFrom("you@youremail.com")->setSubject("Sending with SendGrid is Fun")->setHtml("and easy to do anywhere, even with PHP");$sendgrid->send($email);
<?phprequire'vendor/autoload.php';Dotenv::load(__DIR__);$sendgrid_apikey=getenv('YOUR_SENDGRID_APIKEY');$sendgrid=newSendGrid($sendgrid_apikey);$url='https://api.sendgrid.com/';$pass=$sendgrid_apikey;$template_id='<your_template_id>';$js=array('sub'=>array(':name'=>array('Elmer')),'filters'=>array('templates'=>array('settings'=>array('enable'=>1,'template_id'=>$template_id))));$params=array('to'=>"test@example.com",'toname'=>"Example User",'from'=>"you@youremail.com",'fromname'=>"Your Name",'subject'=>"PHP Test",'text'=>"I'm text!",'html'=>"<strong>I'm HTML!</strong>",'x-smtpapi'=>json_encode($js),);$request=$url.'api/mail.send.json';// Generate curl request$session=curl_init($request);// Tell PHP not to use SSLv3 (instead opting for TLS)curl_setopt($session,CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1_2);curl_setopt($session,CURLOPT_HTTPHEADER,array('Authorization: Bearer '.$sendgrid_apikey));// Tell curl to use HTTP POSTcurl_setopt($session,CURLOPT_POST,true);// Tell curl that this is the body of the POSTcurl_setopt($session,CURLOPT_POSTFIELDS,$params);// Tell curl not to return headers, but do return the responsecurl_setopt($session,CURLOPT_HEADER,false);curl_setopt($session,CURLOPT_RETURNTRANSFER,true);// obtain response$response=curl_exec($session);curl_close($session);// print everything outprint_r($response);?>
<?php$url='https://api.sendgrid.com/';$user='apikey';$pass='your_api_key';$json_string=array('to'=>array('example1@sendgrid.com','example2@sendgrid.com'),'category'=>'test_category');$params=array('api_user'=>$user,'api_key'=>$pass,'x-smtpapi'=>json_encode($json_string),'to'=>'example3@sendgrid.com','subject'=>'testing from curl','html'=>'testing body','text'=>'testing body','from'=>'example@sendgrid.com',);$request=$url.'api/mail.send.json';// Generate curl request$session=curl_init($request);// Tell curl to use HTTP POSTcurl_setopt($session,CURLOPT_POST,true);// Tell curl that this is the body of the POSTcurl_setopt($session,CURLOPT_POSTFIELDS,$params);// Tell curl not to return headers, but do return the responsecurl_setopt($session,CURLOPT_HEADER,false);// Tell PHP not to use SSLv3 (instead opting for TLS)curl_setopt($session,CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1_2);curl_setopt($session,CURLOPT_RETURNTRANSFER,true);// obtain response$response=curl_exec($session);curl_close($session);// print everything outprint_r($response);?>
<?php$url='https://api.sendgrid.com/';$user='apikey';$pass='your_api_key';$fileName='myfile';$filePath=dirname(__FILE__);$params=array('api_user'=>$user,'api_key'=>$pass,'to'=>'example@sendgrid.com','subject'=>'test of file sends','html'=>'<p> the HTML </p>','text'=>'the plain text','from'=>'example@sendgrid.com','files['.$fileName.']'=>'@'.$filePath.'/'.$fileName);print_r($params);$request=$url.'api/mail.send.json';// Generate curl request$session=curl_init($request);// Tell curl to use HTTP POSTcurl_setopt($session,CURLOPT_POST,true);// Tell curl that this is the body of the POSTcurl_setopt($session,CURLOPT_POSTFIELDS,$params);// Tell curl not to return headers, but do return the responsecurl_setopt($session,CURLOPT_HEADER,false);// Tell PHP not to use SSLv3 (instead opting for TLS)curl_setopt($session,CURLOPT_SSLVERSION,CURL_SSLVERSION_TLSv1_2);curl_setopt($session,CURLOPT_RETURNTRANSFER,true);// obtain response$response=curl_exec($session);curl_close($session);// print everything outprint_r($response);?>