<?php
class MailSender{
//require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
 //$pathRoot = $_SERVER['DOCUMENT_ROOT'];

//include_once $pathRoot ."/saia/api/v1/objects/appUser.php";
 
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases

public $fromEmail;
public $fromName;
public $subject;
public $toEmail;
public $toName;
public $contentPlainText;
public $contentHtml;
public $apiKey;
public $pathToLib;

function sendMail(){
    require($this->pathToLib);
	$email = new \SendGrid\Mail\Mail(); 
	$email->setFrom($this->fromEmail, $this->fromName);
	$email->setSubject($this->subject);
	$email->addTo($this->toEmail, $this->toName);
	$noData = "";
	if(strcmp($this->contentPlainText,$noData)!=0){
		$email->addContent("text/plain", $this->contentPlainText);
	}
	
	if(strcmp($this->contentHtml,$noData)!=0){
			$email->addContent("text/html", $this->contentHtml);
	}

	$sendgrid = new \SendGrid($this->apiKey);
	try {
	//	$sendResponse = $sendgrid->send($email);
	$response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
   // print_r($response->headers());
    print $response->body() . "\n";	
        
	//	$response["statusCode"]= $sendResponse->statusCode();
	//	$response["header"]= print_r($sendResponse->headers(),true);
	//	$response["body"]= $sendResponse->body();

		return json_encode($response);
	} catch (Exception $e) {
		$response["statusCode"]= "502";
		$response["header"]= "";
		$response["body"]= 'Caught exception: '. $e->getMessage();
		return json_encode($response);
	}
	
}



}