Files
2026-10-19 15:22:52 -07:00

177 lines
5.8 KiB
PHP

<?php
include "smtp.php";
//
// Function to send the email
//
function SendEmail($recipient, $email_from, $email_subject,$email_message,
$from_name="", $to_name="",$attachments="", $email_bounce="") {
// Get GLOBALS
$smtp = $GLOBALS["smtp"];
$timezone = $GLOBALS['timezone'];
if (isset($timezone) && strlen($timezone) > 0) date_default_timezone_set($timezone);
// Initialize the SMTP handler
if (!$smtp) {
$smtp = new smtp_class;
// $smtp->host_name = "rainforestwebservices.com";
// $smtp->localhost = "localhost";
}
// Preprocess parameters
$EmailSender = htmlentities($email_from);
$EmailSubject = stripslashes($email_subject);
// $EmailBody = stripslashes(htmlentities($email_message));
$EmailBody = stripslashes($email_message);
if($email_bounce == '') {
$EmailBounce = htmlentities($email_from);
} else {
$EmailBounce = htmlentities($email_bounce);
}
// Setup the email to clause
if ($to_name) {
if (strpos($recipient, ',') === false) {
$EmailTo = "\"$to_name\" <" . $recipient . ">";
}
else {
$recipients = explode(",",$recipient);
$EmailTo = "$to_name <" . $recipients[0] . ">";
for ($i = 1; $i < count($recipients); $i++) {
$EmailTo .= ", " . $recipients[$i];
}
}
} else {
if (strpos($recipient, ',') === false) {
$EmailTo = "$recipient";
}
else {
$recipients = explode(",",$recipient);
$EmailTo = $recipients[0];
for ($i = 1; $i < count($recipients); $i++) {
$EmailTo .= ", " . $recipients[$i];
}
}
}
// Setup the email from clause
if ($from_name) {
$EmailFrom = "\"$from_name\" <$EmailSender>";
} else {
$EmailFrom = htmlentities($email_from);
}
$EmailDate = date("D, d M Y H:i:s O",time());
// Prepare to send the email
$set = array("From: $EmailFrom",
"Reply-To: $EmailFrom",
"To: $EmailTo",
"Date: $EmailDate",
"Subject: $EmailSubject");
$bodyLength = strlen($EmailBody);
$strippedLength = strlen(strip_tags($EmailBody));
if ($bodyLength > $strippedLength) {
$isHtml = 1;
$EmailBody = preg_replace("#.*<html>#i","",$EmailBody);
$EmailBody = preg_replace("#</html>#i","",$EmailBody);
$TextMsg = preg_replace("#.*<body#i","<body",$EmailBody);
$TextMsg = preg_replace("#<br>#i","\n",$TextMsg);
$TextMsg = preg_replace("#<br>#i","\n",$TextMsg);
$TextMsg = preg_replace("#</p>#i","\n",$TextMsg);
$TextMsg = strip_tags($TextMsg);
$TextMsg = ltrim($TextMsg);
$TextMsg = htmlspecialchars_decode($TextMsg,ENT_QUOTES);
$TextMsg = str_replace("&nbsp;",' ',$TextMsg);
}
else {
$isHtml = 0;
}
// Now send the email
// $rec = array($recipient);
$rec = explode(",",$recipient);
$EmailBody = str_replace("&quot;",'"',$EmailBody);
// Add in any attachments
if (is_array($attachments) && count($attachments) > 0) {
$parts = array();
// Add message body
if ($isHtml) {
$htmlboundary = md5(uniqid(time())) . "_htmlalt";
$parts[] = "Content-Type: multipart/alternative; boundary=\"$htmlboundary\"\n\n" .
"--" . $htmlboundary . "\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Type: text/plain; charset=UTF-8\n\n" .
$TextMsg . "\n" .
"--" . $htmlboundary . "\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Type: text/html; charset=UTF-8\n\n" .
"<html>" . $EmailBody . "</html>\n" .
"--" . $htmlboundary . "--\n";
}
else {
$parts[] = "Content-Transfer-Encoding: binary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"\r\n" . $EmailBody;
}
// Add each attachment
foreach ($attachments as $attach_name => $attach_data) {
$parts[] = "Content-Transfer-Encoding: base64\r\n" .
"Content-Type: application/unknown; name=\"$attach_name\"\r\n" .
"Content-Disposition: attachment; filename=\"$attach_name\"\r\n" .
"\r\n" . trim(chunk_split(base64_encode($attach_data)));
}
$boundary = md5(uniqid(time()));
// Build the complete message
$EmailBody = join("\r\n\r\n--{$boundary}\r\n", $parts);
$EmailBody = "--{$boundary}\r\n{$EmailBody}\r\n\r\n--{$boundary}--\r\n";
$set[] = "Content-Type: multipart/mixed; boundary=\"$boundary\"";
$set[] = "MIME-Version: 1.0";
}
else {
if ($isHtml) {
$htmlboundary = md5(uniqid(time())) . "_htmlalt";
$set[] = "MIME-version: 1.0";
$set[] = "Content-Type: multipart/alternative; boundary=\"$htmlboundary\"";
$EmailBody = "--" . $htmlboundary . "\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Type: text/plain; charset=UTF-8\n\n" .
$TextMsg . "\n" .
"--" . $htmlboundary . "\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Type: text/html; charset=UTF-8\n\n" .
"<html>" . $EmailBody . "</html>\n" .
"--" . $htmlboundary . "--\n";
}
}
$result = $smtp->SendMessage($EmailBounce, $rec, $set, $EmailBody);
if (!$result) {
sleep(1);
$result = $smtp->SendMessage($EmailBounce, $rec, $set, $EmailBody);
if (!$result) {
sleep(2);
$result = $smtp->SendMessage($EmailBounce, $rec, $set, $EmailBody);
}
}
if (!$result) $GLOBALS['error_message'] = $smtp->error . " - $recipient";
return($result);
}
?>