"mail@aaronreynolds.co.uk")
- $textMessage = if you want to send just a text-based message or you want to provide a specific text-based alternative
to the HTML-based message, you can specify it here. If you leave it blank, then the HTML message has its tags stripped to
create a text-based alternative.
- $htmlMessage = specify this if you want to send an HTML-based message
- $subject = email subject line
- $attachmentsArray = an array of attachments with each element being an array defined as follows:
"mime_type"=> the standard mime-type
""saveAs_name"=> a simple file-name for the attachment (no path or special characters)
"file_name"=> a local system path to the file
- $customHeaders = an array of any custom email headers to you want to add keyed by header:
e.g. "X-Custom-Header-name"=>"Customer Header Value"
- $ccArray = array of CC recipients to add in same format as $toArray
- $bccArray = array of BCC recipients to add in same format as $toArray
- $debug = show debugging information in the page
*/
function SendEmail($toArray, $textMessage=null, $htmlMessage=null, $subject, $attachmentsArray=null, $customHeaders=null, $ccArray=null, $bccArray=null, $debug=false)
{
/*
$emailSender = sending email address defined elsewhere e.g. in a config file
$emailSenderName = name to use for sender defined elsewhere e.g. in a config file
$email_template_html = optional html template defined elsewhere e.g. in a config file
$email_template_text = optional text-based template defined elsewhere e.g. in a config file
*/
global $emailSender, $emailSenderName, $email_template_html, $email_template_text;
if ($email_template_html == null) $email_template_html = "--CONTENT--";
if ($email_template_text == null) $email_template_text = "--CONTENT--";
// Boundaries
$mixedBoundary = getBoundary();
$altBoundary = getBoundary();
// Get main Recipient
$to = '';
if (is_array($toArray))
{
$recipientCount = 0;
foreach ($toArray as $key => $thisRecipient)
{
if ($recipientCount > 0) { $to .= ','; }
if (!is_numeric($key))
{
$to .= "\"" . $key . "\" <" . $thisRecipient . ">";
}
else
{
$to .= $thisRecipient;
}
$recipientCount++;
}
}
else
{
$to = $toArray;
}
// Define sender
$sender = "\"" . $emailSenderName . "\" <" . $emailSender . ">";
// Define main headers
$headersArray = array();
//$headersArray['To'] = $to;
$headersArray['From'] = $sender;
$headersArray['Reply-To'] = $emailSender;
$headersArray['MIME-Version'] = '1.0';
$headersArray['X-Mailer'] = 'PHP/' . phpversion();
// Determine Message Type
if ($attachmentsArray != null && is_array($attachmentsArray))
{
// A mixed message contains one or more attachments
$headersArray['Content-Type'] = "multipart/mixed; boundary=\"BS-mixed-$mixedBoundary\"";
}
else
{
// An alternate message contains several versions of the same content
// - an alternate message can be enveloped inside a mixed message
$headersArray['Content-Type'] = "multipart/alternative; boundary=BS-alt-$altBoundary\"";
}
// Attach custom headers
if ($customHeaders != null)
{
foreach ($customHeaders as $k => $v)
{
$headersArray[$k] = $v;
}
}
// If additional recipients
if ($ccArray != null && is_array($ccArray))
{
$cc = '';
$recipientCount = 0;
foreach ($ccArray as $key => $thisRecipient)
{
if ($recipientCount > 0) { $to .= ','; }
if (!is_numeric($key))
{
$cc .= "\"" . $key . "\" <" . $thisRecipient . ">";
}
else
{
$cc .= $thisRecipient;
}
$recipientCount++;
}
$headersArray['Cc'] = $cc;
}
if ($bccArray != null && is_array($bccArray))
{
$bcc = '';
$recipientCount = 0;
foreach ($bccArray as $key => $thisRecipient)
{
if ($recipientCount > 0) { $to .= ','; }
if (!is_numeric($key))
{
$bcc .= "\"" . $key . "\" <" . $thisRecipient . ">";
}
else
{
$bcc .= $thisRecipient;
}
$recipientCount++;
}
$headersArray['Bcc'] = $bcc;
}
// Define Message Body
$output = '';
// Message pre-amble
$output .=
"This is a Multipart Email message:
- your Email Client should ignore this statement if it is capable of displaying this email correctly.\n\n";
// If attachments
if ($attachmentsArray != null && is_array($attachmentsArray))
{
$output .= "--BS-mixed-$mixedBoundary\n";
}
// START ALT
if ($htmlMessage !== null)
{
// Alternate versions supplied
$output .= "Content-Type: multipart/alternative; boundary=\"BS-alt-$altBoundary\"\n\n";
}
if ($textMessage !== null)
{
$textVersion = $textMessage;
}
else // use HTML version
{
$textVersion = str_replace('--CONTENT--', $htmlMessage, $email_template_text);
$textVersion = str_replace('--EMAIL--', $to, $textVersion);
$textVersion = str_replace('
', '\n', $textVersion);
$textVersion = strip_tags($textVersion);
}
// ENCAPSULATE ALT VERSION
// Do Plain-Text first
$output .= "--BS-alt-$altBoundary\n";
$output .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$output .= "Content-Transfer-Encoding: 7bit\n\n";
$output .= wordwrap($textVersion, 70);
$output .= "\n";
// END of ENCAPSULATE ALT VERSION
// ENCAPSULATE ALT VERSION
if ($htmlMessage !== null)
{
// Do HTML version
$output .= "--BS-alt-$altBoundary\n";
$output .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$output .= "Content-Transfer-Encoding: 7bit\n\n";
$htmlVersion = str_replace('--SUBJECT--', $subject, $email_template_html);
$htmlVersion = str_replace('--CONTENT--', $htmlMessage, $htmlVersion);
$output .= $htmlVersion;
$output .= "\n";
}
// END of ENCAPSULATE ALT VERSION
// END ALT
if ($htmlMessage !== null)
{
// Alternate versions supplied
$output .= "--BS-alt-$altBoundary--\n\n";
}
// If attachments
if ($attachmentsArray != null && is_array($attachmentsArray))
{
foreach ($attachmentsArray as $thisAttachment)
{
$output .= "--BS-mixed-$mixedBoundary\n";
$output .= "Content-Type: " . $thisAttachment['mime_type'] . "; name=\"" . $thisAttachment['saveAs_name'] . "\"\n";
$fs = filesize($thisAttachment['file_name']);
$output .= "Content-Disposition: attachment; filename=\"" . $thisAttachment['saveAs_name'] . "\"; size=$fs\n";
$output .= "Content-Transfer-Encoding: base64\n\n";
$attachment = chunk_split(base64_encode(file_get_contents($thisAttachment['file_name'])));
$output .= $attachment . "\n";
}
$output .= "--BS-mixed-$mixedBoundary--\n";
}
// Now build header
foreach ($headersArray as $k => $v)
{
$headers .= $k . ": " . $v . "\n";
}
// OK we have everything ready to send now
ini_set("sendmail_from", $emailSender);
$runningSafe = (ini_get("safe_mode") == 'On' || ini_get("safe_mode") === "1") ? true : false;
if ($runningSafe)
{
$result = mail($to, $subject, $output, $headers);
}
else
{
$result = mail($to, $subject, $output, $headers, "-f".$emailSender);
}
// Show Passed vars:
if ($debug)
{
echo "
";
echo "** TO: **\r\n";
print_r($toArray);
echo "\r\n\r\n** ATTACHMENTS: **\r\n";
print_r($attachmentsArray);
echo "\r\n\r\n** CUSTOM HEADERS: **\r\n";
print_r($customHeaders);
echo "\r\n\r\n** CC: **\r\n";
print_r($ccArray);
echo "\r\n\r\n** BCC: **\r\n";
print_r($bccArray);
echo "\r\n\r\n** HEADERS: **\r\n";
print_r($headersArray);
echo "\r\n\r\n** MESSAGE: **\r\n";
echo showTags($output);
echo "\r\n\r\n** TO: [" . $to . "]**\r\n";
echo "\r\n\r\n** SUBJECT: [" . $subject . "]**\r\n";
echo "\r\n\r\n** HEADERS: [" . $headers . "]**\r\n";
echo "\r\n** SAFE MODE RESPONSE: " . ini_get("safe_mode") . " **\r\n";
echo "\r\n** SAFE MODE: " . ($runningSafe ? "Y" : "N") . " **\r\n";
echo "** " . ($result ? "Queued" : "Failed to Queue") . " **";
echo "";
}
return $result;
}
function showTags($html)
{
return str_replace('<', '<', str_replace('>', '>', $html));
}
function getBoundary()
{
// Get separator for Multipart message
return md5(date('r', time()));
}