Skip to content

Instantly share code, notes, and snippets.

@umanghome
Created April 1, 2018 17:54
Show Gist options
  • Save umanghome/a0bcfa822e5ab78effad4dd5aeb38212 to your computer and use it in GitHub Desktop.
Save umanghome/a0bcfa822e5ab78effad4dd5aeb38212 to your computer and use it in GitHub Desktop.
Sending Elastic Email emails using PHP
<?php
define('ELASTIC_MAIL_API_KEY', '');
/**
* Sends an email using Elastic Mail.
* @param {String} $to Receiver's email address.
* @param {String} $subject Email submit.
* @param {String} $body_text Text body.
* @param {String} $body_html HTML body.
* @param {String} $from Sender's email address.
* @param {String} $fromName Name of sender.
* @return {Boolean}
*/
function sendElasticEmail($to, $subject, $body_text, $body_html, $from, $fromName) {
$res = "";
$data = "username=".urlencode("[email protected]");
$data .= "&api_key=".urlencode(ELASTIC_MAIL_API_KEY);
$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);
$header = "POST /mailer/send HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($data) . "\r\n\r\n";
$fp = fsockopen('ssl://api.elasticemail.com', 443, $errno, $errstr, 30);
if(!$fp)
return false;
else {
fputs ($fp, $header.$data);
while (!feof($fp)) {
$res .= fread ($fp, 1024);
}
fclose($fp);
}
return true;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment