PHP Code:
<?php
class TSUESMTP
{
public $smtpSocket = NULL;
public $smtpReturn = 0;
public $secure = "";
public $toemail = "";
public $toname = "";
public $subject = "";
public $message = "";
public $headers = "";
public $fromemail = "";
public $delimiter = "\r\n";
public $Settings = array( );
public function TSUESMTP()
{
global $TSUE;
$this->secure = ($TSUE["TSUE_Settings"]->settings["global_settings"]["smtp_secure"] == 1 ? "tls" : "none");
}
public function start($toemail, $toname = "", $subject, $message, $from, $fromname)
{
global $TSUE;
$toemail = $this->fetch_first_line($toemail);
if( empty($toemail) )
{
return false;
}
$delimiter =& $this->delimiter;
$toemail = $this->dounhtmlspecialchars($toemail);
$subject = $this->fetch_first_line($subject);
$message = preg_replace("#(\r\n|\r|\n)#s", $delimiter, trim($message));
if( (strtolower($TSUE["TSUE_Language"]->charset) == "iso-8859-1" || $TSUE["TSUE_Language"]->charset == "") && preg_match("/&[a-z0-9#]+;/i", $message) )
{
$message = utf8_encode($message);
$subject = utf8_encode($subject);
$encoding = "UTF-8";
$unicode_decode = true;
}
else
{
$encoding = $TSUE["TSUE_Language"]->charset;
$unicode_decode = false;
}
$message = $this->dounhtmlspecialchars($message, $unicode_decode);
$subject = $this->encode_email_header($this->dounhtmlspecialchars($subject, $unicode_decode), $encoding, false, false);
$from = $this->fetch_first_line($from);
$mailfromname = ($fromname ? $this->fetch_first_line($fromname) : $from);
if( $unicode_decode == true )
{
$mailfromname = utf8_encode($mailfromname);
}
$mailfromname = $this->encode_email_header($this->dounhtmlspecialchars($mailfromname, $unicode_decode), $encoding);
if( !isset($headers) )
{
$headers = "";
}
$headers .= "From: " . $mailfromname . " <" . $from . ">" . $delimiter;
$headers .= "Return-Path: " . $from . $delimiter;
$headers .= "Message-ID: <" . gmdate("YmdHis") . "." . substr(md5($message . microtime()), 0, 12) . "@" . $TSUE["TSUE_Settings"]->settings["global_settings"]["website_url"] . ">" . $delimiter;
$headers .= "MIME-Version: 1.0" . $delimiter;
$headers .= "Content-Type: text/html" . (($encoding ? "; charset=\"" . $encoding . "\"" : "")) . $delimiter;
$headers .= "Content-Transfer-Encoding: 8bit" . $delimiter;
$headers .= "X-Priority: 3" . $delimiter;
$headers .= "X-Mailer: TSUE Mail via PHP" . $delimiter;
$headers .= "X-Sender: TSUE PHP-Mailer" . $delimiter;
$headers .= "Date: " . date("r") . $delimiter;
$this->toemail = $toemail;
$this->toname = $toname;
$this->subject = $subject;
$this->message = $message;
$this->headers = $headers;
$this->fromemail = $from;
}
public function send()
{
global $TSUE;
if( !$this->toemail )
{
return false;
}
$apiKey = 'api-4B9753D6AC604B78B67E0A200476488F';
$url = 'https://api.smtp2go.com/v3/email/send';
$data = [
'sender' => $this->fromemail,
'to' => explode(',', $this->toemail),
'subject' => $this->subject,
'text_body' => strip_tags($this->message),
'html_body' => $this->message
];
$options = [
'http' => [
'header' => "Content-Type: application/json\r\n" .
"X-Smtp2go-Api-Key: $apiKey\r\n",
'method' => 'POST',
'content' => json_encode($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
return $this->errorMessage("Unable to send email via API");
}
return true;
}
public function fetch_first_line($text)
{
$text = preg_replace("/(\r\n|\r|\n)/s", "\r\n", trim($text));
$pos = strpos($text, "\r\n");
if( $pos !== false )
{
return substr($text, 0, $pos);
}
return $text;
}
public function dounhtmlspecialchars($text, $doUniCode = false)
{
if( $doUniCode )
{
$text = preg_replace("/&#([0-9]+);/esiU", "convert_int_to_utf8('\\1')", $text);
}
return str_replace(array( "<", ">", """, "&" ), array( "<", ">", "\"", "&" ), $text);
}
public function encode_email_header($text, $charset = "utf-8", $force_encode = false, $quoted_string = true)
{
$text = trim($text);
if( !$charset )
{
return $text;
}
if( $force_encode == true )
{
$qp_encode = true;
}
else
{
$qp_encode = false;
for( $i = 0; $i < strlen($text); $i++ )
{
if( 127 < ord($text[$i]) )
{
$qp_encode = true;
break;
}
}
}
if( $qp_encode == true )
{
$outtext = preg_replace("#([^a-zA-Z0-9!*+\\-/ ])#e", "'=' . strtoupper(dechex(ord(str_replace('\\\"', '\"', '\\1'))))", $text);
$outtext = str_replace(" ", "_", $outtext);
$outtext = "=?" . $charset . "?q?" . $outtext . "?=";
return $outtext;
}
if( $quoted_string )
{
$text = str_replace(array( "\"", "(", ")" ), array( "\\\"", "\\(", "\\)" ), $text);
return "\"" . $text . "\"";
}
return preg_replace("#(\\r\\n|\\n|\\r)+#", " ", $text);
}
public function errorMessage($msg)
{
global $TSUE;
if( $TSUE["TSUE_Settings"]->settings["global_settings"]["smtp_debug"] == "1" )
{
trigger_error($msg, 512);
}
return false;
}
}