acymailingElasticemail

The MailPoet Newsletters acymailingElasticemail class.

Defined (1)

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

/inc/mailer/class.elasticemail.php  
  1. class acymailingElasticemail { 
  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. /** Upload Function which uploads the file selected and return a part of the response. 
  15. * The return value is the file's ID on ElasticEmail server. 
  16. */ 
  17. function uploadAttachment($filepath, $filename) { 
  18. if (!empty ($this->attachment[$filepath])) 
  19. return $this->attachment[$filepath]; 
  20.  
  21. $data = file_get_contents($filepath); 
  22. $header = "PUT /attachments/upload?username=" . urlencode($this->Username) . "&api_key=" . urlencode($this->Password) . "&file=" . urlencode($filename) . " HTTP/1.0\r\n"; 
  23. $header .= "Host: api.elasticemail.com\r\n"; 
  24. $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
  25. $header .= "Connection: Keep-alive\r\n"; 
  26. $header .= "Content-Length: " . strlen($data) . "\r\n\r\n"; 
  27. $info = $header . $data; 
  28. $result = $this->sendinfo($info); 
  29. //We take the last value of the server's response which correspond of the file's ID. 
  30. $res = end(explode("\r\n", $result)); 
  31. //If the ID is correct and we have no Errors 
  32. if(preg_match('#[^a-z0-9\-]#i', $res) || strpos($result, '200 OK') === false) { 
  33. $this->error = "Error while uploading file : ".$res; 
  34. return false; 
  35. } else { 
  36. $this->attachment[$filepath] = $res; 
  37. return $res; 
  38.  
  39. /** Function which permit to send an email based on the object's values. 
  40. * First, we do the test if we have enough credit to send emails. 
  41. */ 
  42. function sendMail(& $object) { 
  43. if (!$this->connect()) 
  44. return false; 
  45. $data = "username=" . urlencode($this->Username); 
  46. $data .= "&api_key=" . urlencode($this->Password); 
  47. $data .= "&referral=" . urlencode('2f0447bb-173a-459d-ab1a-ab8cbebb9aab'); 
  48. //if you want to encode the message in quoted printable 
  49. //$data .= "&encodingtype=3"; 
  50. if (!empty ($object->From)) $data .= "&from=" . urlencode($object->From); 
  51. if (!empty ($object->FromName)) $data .= "&from_name=" . urlencode($object->FromName); 
  52.  
  53. $to = array_merge($object->to, $object->cc, $object->bcc); 
  54. $data .="&to="; 
  55. foreach($to as $oneRecipient) { 
  56. $data .= urlencode($object->AddrFormat($oneRecipient).";"); 
  57. $data = trim($data, ';'); 
  58.  
  59. if (!empty ($object->Subject)) $data .= "&subject=" . urlencode($object->Subject); 
  60.  
  61. if (!empty($object->ReplyTo)) { 
  62. $replyToKey = key($object->ReplyTo); 
  63. $data .="&reply_to=". urlencode($object->ReplyTo[$replyToKey][0]); 
  64. if(!empty($object->ReplyTo[$replyToKey][1])) { 
  65. $data .="&reply_to_name=". urlencode($object->ReplyTo[$replyToKey][1]); 
  66.  
  67. if (!empty($object->Sender)) $data .="&sender=".urlencode($object->Sender); 
  68.  
  69. if (!empty ($object->sendHTML) || !empty($object->AltBody)) { 
  70. $data .= "&body_html=" . urlencode($object->Body); 
  71. if (!empty ($object->AltBody)) $data .= "&body_text=" . urlencode($object->AltBody); 
  72. }else{ 
  73. $data .= "&body_text=" . urlencode($object->Body); 
  74.  
  75. if ($object->attachment) { 
  76. $ArrayID = array (); 
  77. foreach ($object->attachment as $oneAttachment) { 
  78. $oneID = $this->uploadAttachment($oneAttachment[0], $oneAttachment[2]); 
  79. if (!$oneID) 
  80. return false; 
  81. $ArrayID[]=$oneID; 
  82. $data .= "&attachments=".urlencode(implode(";", $ArrayID)); 
  83.  
  84. if (!empty($object->mailid)) $data .= "&channel=".urlencode($object->mailid); 
  85.  
  86. $header = "POST /mailer/send HTTP/1.0\r\n"; 
  87. $header .= "Host: api.elasticemail.com\r\n"; 
  88. $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
  89. $header .= "Connection: Keep-Alive\r\n"; 
  90. $header .= "Content-Length: " . strlen($data) . "\r\n"; 
  91. $header .= "Message-ID: " . $object->MessageID . "\r\n\r\n"; 
  92.  
  93. $info = $header . $data; 
  94. $result = $this->sendinfo($info); 
  95.  
  96. //We take the last value of the server's response which correspond of the file's ID. 
  97. $explodedVar = explode("\r\n", $result); 
  98. $res = end($explodedVar); 
  99.  
  100. if(!$object->SMTPKeepAlive) { 
  101. $this->close(); 
  102.  
  103. //If the ID is correct and we have no Errors 
  104. if(strpos($result, '200 OK') === false || preg_match('#[^a-z0-9\-]#i', $res)) { 
  105. $this->error = $res; 
  106. return false; 
  107. } else { 
  108. return true; 
  109.  
  110. function getCredits($object) { 
  111. $header = "GET /mailer/account-details?username=" . urlencode($this->Username) . "&api_key=" . urlencode($this->Password) . " HTTP/1.0\r\n"; 
  112. $header .= "Host: api.elasticemail.com\r\n"; 
  113. $header .= "Connection: Close\r\n\r\n"; 
  114. $result = $this->sendinfo($header); 
  115. if (!$result) 
  116. return false; 
  117. if (preg_match('#<credit>(.*)</credit>#Ui', $result, $explodedResults)) { 
  118. return $explodedResults[1]; 
  119. } else { 
  120. $this->error = $result; 
  121. return false; 
  122.  
  123. function connect() { 
  124. if (is_resource($this->conn)) 
  125. return true; 
  126. $this->conn = fsockopen('ssl://api.elasticemail.com', 443, $errno, $errstr, 20); 
  127. if (!$this->conn) { 
  128. $this->error = "Could not open connection " . $errstr; 
  129. return false; 
  130. return true; 
  131.  
  132. function sendinfo(& $info) { 
  133. //Check if the connection is Ok... and if not we return false. 
  134. if (!$this->connect()) { 
  135. return false; 
  136. $res = ''; 
  137. $length = 0; 
  138. fwrite($this->conn, $info); 
  139. while (!feof($this->conn)) { 
  140. $res .= fread($this->conn, 1024); 
  141. if (substr($res, 0, 4) == "HTTP") { 
  142. $length = 0; 
  143. if ($length == 0) { 
  144. $pos = strpos(strtolower($res), 'content-length:'); 
  145. if ($pos !== false) { 
  146. $lng = substr($res, $pos +16, 6); 
  147. if (strpos($lng, "\r") !== false) { 
  148. $length = (int) $lng; 
  149. $length += $pos; 
  150. if ($length > 0 && strlen($res) >= $length) 
  151. break; 
  152. return $res; 
  153.  
  154. function close() { 
  155. if (is_resource($this->conn)) fclose($this->conn);