Click on link below to get fully featured Elastic Email SMTP API Class
written in PHP

features supported yet
1. send/schedule email
2. create subscribers list
3. upload subscribers csv file to list
4. get tracking
5. get subscriber lists
6. upload attachments
7. upload merge fields

rename elastic.php.doc to elastic.php

elastic.php


/*
  @author:Tofeeq ur Rehman
  @class : elastic
*/

class Elastic
{
	public $config = array();
	
	public function __construct()
	{
		$file = APPLICATION_PATH . '/configs/elastic.json';
		$json = file_get_contents($file);
		$this->config = Zend_Json::decode($json);
	} 
	
	
	public function elastic()
	{
		return $this;
	}
	
	
	function sendEmail($to, $subject, $body_text, $body_html, $from, $fromName, $minutesDelay = null, $attachments = null, $csvFileName = null)
	{

		$data = "username=".urlencode($this->config['account_email']);
		$data .= "&api_key=".urlencode($this->config['apikey']);
		$data .= "&from=".urlencode($from);
		$data .= "&from_name=".urlencode($fromName);
		#$data .= "&to=".urlencode($to);
		$data .= "&subject=".urlencode($subject);
		if($body_html)
		  $data .= "&body_html=".urlencode($body_html);
		if($body_text)
		  $data .= "&body_text=".urlencode($body_text);
		
		if ($attachments && $csvFileName) {
			$data .= "&data_source=".urlencode($attachments);
		} else if ($attachments) {
			$data .= "&attachments=".urlencode($csvFileName);
		}

		if ($minutesDelay) {
			$data .= "&time_offset_minutes=" . urlencode($minutesDelay);	
		}
		
		
		return $this->request($data, '/mailer/send');
		
	}
	
	
	function uploadAttachment($filepath, $filename)
	{
	    $data = http_build_query(
	    			array(
	    				'username' => $this->config['account_email'],
	    				'api_key' => $this->config['apikey'],
	    				'file' => $filename
	    			)
	    		);
	    
	    $file = file_get_contents($filepath);
	    $result = ''; 
	
	    $fp = fsockopen('ssl://api.elasticemail.com', 443, $errno, $errstr, 30);   
	
	    if ($fp){
	        fputs($fp, "PUT /attachments/upload?".$data." HTTP/1.1rn");
	        fputs($fp, "Host: api.elasticemail.comrn");
	        fputs($fp, "Content-type: application/x-www-form-urlencodedrn");
	        fputs($fp, "Content-length: ". strlen($file) ."rn");
	        fputs($fp, "Connection: closernrn");
	        fputs($fp, $file);
	        while(!feof($fp)) {
	            $result .= fgets($fp, 128);
	        }
	    } else { 
	        return array(
	            'status'=>false,
	            'error'=>$errstr.'('.$errno.')',
	            'result'=>$result);
	    }
	    fclose($fp);
	    $result = explode("rnrn", $result, 2); 
	    return array(
	        'status' => true,
	        'attachId' => isset($result[1]) ? $result[1] : ''
	    );
	}
	
	/**
	 * 
	 * @param url encoded string $data
	 * @param string $action: /mailer/send
	 * @return string
	 */
	public function request(&$data, $action)
	{
		$res = "";
		
		$header = "POST $action HTTP/1.0rn";
		$header .= "Content-Type: application/x-www-form-urlencodedrn";
		$header .= "Content-Length: " . strlen($data) . "rnrn";
		
		$fp = fsockopen('ssl://api.elasticemail.com', 443, $errno, $errstr, 30);
		
		if(!$fp)
			return "ERROR. Could not open connection";
		else {
			fputs ($fp, $header . $data);
			while (!feof($fp)) {
				$res .= fread ($fp, 1024);
			}
			fclose($fp);
		}
		
		
		$res = explode("rnrn", $res, 2);
		return array(
				'status' => true,
				'data' => isset($res[1]) ? $res[1] : ''
		);
		
		return $res;
	}
	
	
	public function createSubscriberList($listName)
	{
		$data = http_build_query(
	    			array(
	    				'username' => $this->config['account_email'],
	    				'api_key' => $this->config['apikey'],
	    				'listname' => $listName
	    			)
	    		);
		
		 return $this->request($data, '/lists/create-list');  
		
	}
	
	
	public function addSubscriberToList($listName, $filename, $filepath)
	{
		$file_name_with_full_path = realpath($filepath);
		
		//echo $file_name_with_full_path; die;
		
		$post = array(
				'api_key' => $this->config['apikey'],
				'listname' => $listName,
				'file_contents'=>'@'.$file_name_with_full_path
		);
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://api.elasticemail.com/lists/upload-contacts');
		curl_setopt($ch, CURLOPT_POST,1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		//curl_setopt($ch, CURLOPT_UPLOAD, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		$result = curl_exec ($ch);
		curl_close ($ch);
		
		return $result;
	
	}
	
	
	public function getSubscribers($listName)
	{
		$data = "username=".urlencode($this->config['account_email']);
		$data .= "&api_key=".urlencode($this->config['apikey']);
		$data .= "&listname=".urlencode($listName);
		
		$result = $this->request($data, '/lists/get-contacts');
		return $result['data'];
	}
	
	public function getLog()
	{
		$data = "username=".urlencode($this->config['account_email']);
		$data .= "&api_key=".urlencode($this->config['apikey']);
		$data .= "&listname=".urlencode($listName);
		
		$result = $this->request($data, '/mailer/status/log');
		//echo '
' . __FILE__ . ':' . __LINE__; var_export($result);
		return $result['data'];
	}
}

Advertisements