acymailingSendgrid

The MailPoet Newsletters acymailingSendgrid class.

Defined (1)

The class is defined in the following location(s).

/inc/mailer/class.sendgrid.php  
  1. class acymailingSendgrid { 
  2. /** 
  3. * Ressources : Connection to the elasticemail server 
  4. */ 
  5. var $conn; 
  6.  
  7. /** 
  8. * String : Last error... 
  9. */ 
  10. var $error; 
  11. var $Username = ''; 
  12. var $Password = ''; 
  13.  
  14. /** Function which permit to send an email based on the object's values. 
  15. * First, we do the test if we have enough credit to send emails. 
  16. */ 
  17. function sendMail(& $object) { 
  18. $url = 'https://api.sendgrid.com'; 
  19.  
  20. $to = array_merge(array($object->to[0][0]), $object->cc, $object->bcc); 
  21. /**foreach($to as $oneRecipient) { 
  22. $data .= '&to[]='.urlencode($object->AddrFormat($oneRecipient).";"); 
  23. }*/ 
  24. $replyToKey = key($object->ReplyTo); 
  25.  
  26. $params = array( 
  27. 'api_user' => $this->Username,  
  28. 'api_key' => $this->Password,  
  29. 'to' => array_filter($to),  
  30. 'replyto'=> $object->ReplyTo[$replyToKey][0],  
  31. 'from' => $object->From,  
  32. 'fromname' => $object->FromName,  
  33. ); 
  34.  
  35. if(!empty($object->ReplyTo[$replyToKey][1])) { 
  36. $params['replytoname']= $object->ReplyTo[$replyToKey][1]; 
  37.  
  38. if (!empty ($object->Subject)) $params['subject']= $object->Subject; 
  39.  
  40. if (!empty($object->Sender)) $params['sender']=$object->Sender; 
  41.  
  42. if (!empty ($object->sendHTML) || !empty($object->AltBody)) { 
  43. $params['html']= $object->Body; 
  44. if (!empty ($object->AltBody)) $params['text']=$object->AltBody; 
  45. }else{ 
  46. $params['text']=$object->Body; 
  47.  
  48.  
  49. if ($object->attachment) { 
  50. $ArrayID = array (); 
  51. foreach ($object->attachment as $oneAttachment) { 
  52. $params['files'][$oneAttachment[2]]=$oneAttachment[0]; 
  53.  
  54. $header=array(); 
  55. $header['Content-Type']='application/x-www-form-urlencoded'; 
  56. $header['Connection']='Keep-Alive'; 
  57. $header['Message-ID']=$object->MessageID; 
  58.  
  59.  
  60. $params['headers']=json_encode($header); 
  61. $request = $url.'/api/mail.send.json'; 
  62.  
  63. $this->error=''; 
  64.  
  65. // Generate curl request 
  66. $session = curl_init($request); 
  67.  
  68. // Tell curl to use HTTP POST 
  69. curl_setopt ($session, CURLOPT_POST, true); 
  70.  
  71. // Tell curl that this is the body of the POST 
  72. curl_setopt ($session, CURLOPT_POSTFIELDS, http_build_query($params)); 
  73.  
  74. // Tell curl not to return headers, but do return the response 
  75. curl_setopt($session, CURLOPT_HEADER, false); 
  76. curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 
  77.  
  78. // Disable verification for misconfigured hosts :( 
  79. curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false); 
  80. curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false); 
  81.  
  82. // obtain response 
  83. $result = curl_exec($session); 
  84.  
  85. if (!$result) { 
  86. $this->error .= curl_error($session); 
  87.  
  88. curl_close($session); 
  89.  
  90. //We take the last value of the server's response which correspond of the file's ID. 
  91. $result=json_decode($result); 
  92.  
  93. //If the ID is correct and we have no Errors 
  94. if(isset($result->message) && $result->message=='success') { 
  95. return true; 
  96. } else { 
  97. if(isset($result->message) && $result->message=='error') { 
  98. foreach($result->errors as $msgError) 
  99. $this->error .= $msgError."\n\r"; 
  100. return false; 
  101.