src.dualinventive.com/mtinfo/dist/webroot/rc-4.05/include/mail.php

196 lines
6.5 KiB
PHP

<?php
/** \file scripts\page\mail.php
* \brief DI webinterface mail script.
* \author Rob Schalken, Core|Vision
* \version 1.0
* \date 17-10-2008
*
* This file contains the mail scripts.
*/
/*
* Required page
*/
require_once("utilities.php");
/**
* Send mail
*
* Inputs:
* - to Receiver
* - cc Carbon copy receiver
* - bcc Blind carbon copy receiver
* - from Sender
* - message_html Message for html email
* - attachment string with file
* - filename Filename
* - importance High/low importance
*
* Return: 1(OK)/0(Error)
*/
function send_mail($to, $cc, $bcc, $from, $subject, $message_html = "", $attachment = "", $filename = "", $importance=0) {
// Initial values
$result = FALSE;
// replace ; separator
$to = trim(str_replace(";", ",", $to));
$from = trim(str_replace(";", ",", $from));
$cc = trim(str_replace(";", ",", $cc));
$bcc = trim(str_replace(";", ",", $bcc));
// Check for valid or empty email address
if (valid_email($to) && valid_email($from) && valid_email($cc) && valid_email($bcc)) {
// Create a boundary string. It must be unique
// so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
// Define the headers we want pass. Note that they are separated with \r\n
$headers = "From: " . $from . "\r\nReply-To: " . $from . "\r\n";
// high importance?
if ($importance) {
$headers .= "X-priority: 1\r\npriority: Urgent\r\nImportance: high\r\n";
}
// Carbon Copy?
if (strlen($cc)) {
$headers .= "Cc: " . $cc . "\r\n";
}
// Blind carbon Copy?
if (strlen($bcc)) {
$headers .= "Bcc: " . $bcc . "\r\n";
}
// Add boundary string and mime type specification
$headers .= "Content-Type: multipart/mixed; boundary=\"PHP-alt-" . $random_hash . "\"\r\n";
// Turn on output buffering
ob_start();
if (strlen($message_html)) {
echo "\n--PHP-alt-" . $random_hash . "\n";;
echo "Content-Type: text/html; charset=\"UTF-8\"\n";
echo "Content-Transfer-Encoding: base64\n\n";
echo chunk_split(base64_encode($message_html)) . "\n";
}
if( is_array($attachment) && isset($attachment['document']) ) {
// data returned by 'upload_document'
echo "\n--PHP-alt-" . $random_hash . "\n";
echo "Content-Disposition: attachment\n";
echo "Content-Type: " . $attachment['mimetype'] . "; name=\"" . $attachment['filename'] . "\"\n";
echo "Content-Transfer-Encoding: base64\n\n";
echo chunk_split(base64_encode($attachment['document'])) . "\n";
}
else if (strlen($attachment)) {
$base64_encode = TRUE; // default
echo "\n--PHP-alt-" . $random_hash . "\n";
echo "Content-Disposition: attachment\n";
// Add requested attachment
switch(strtoupper(GetExtension($filename))) {
case ".PDF":
echo "Content-Type: application/pdf; name=\"" . $filename . "\"\n";
break;
case ".JPG":
echo "Content-Type: image/jpeg; name=\"" . $filename . "\"\n";
break;
case ".GIF":
echo "Content-Type: image/gif; name=\"" . $filename . "\"\n";
break;
case ".PNG":
echo "Content-Type: image/png; name=\"" . $filename . "\"\n";
break;
case ".TIF":
case ".TIFF":
echo "Content-Type: image/tiff; name=\"" . $filename . "\"\n";
break;
case ".CSV":
echo "Content-Type: text/csv; name=\"" . $filename . "\"\n";
break;
case ".DOC":
case ".DOCX":
echo "Content-Type: application/msword; name=\"" . $filename . "\"\n";
break;
case ".XLS":
case ".XLSX":
echo "Content-Type: application/vnd.ms-excel; name=\"" . $filename . "\"\n";
break;
case ".PPT":
case ".PPTX":
echo "Content-Type: application/vnd.ms-powerpoint; name=\"" . $filename . "\"\n";
break;
default:
echo "Content-Type: text/plain; name=\"" . $filename . "\"\n";
break;
}
{ // no other encodings defined // if( $base64_encode ) {
echo "Content-Transfer-Encoding: base64\n\n";
echo chunk_split(base64_encode($attachment)) . "\n";
}
}
// End boundary
echo "\n--PHP-alt-" . $random_hash . "--\n";
// Copy current buffer contents into $message variable and delete current output buffer
$catch_message = ob_get_clean();
// Send mail
$result = mail($to, $subject, $catch_message, $headers);
}
// Write result to log!
if ($result === FALSE) {
DBG("mail error, To: " . ($to) . ", Cc: " . ($cc) . ", Bcc: " . ($bcc) . ", Subject: " . $subject . ", Headers: " . str_replace("\r\n", ",", $headers));
}
else {
DBG("mail ok, To: " . ($to) . ", Cc: " . ($cc) . ", Bcc: " . ($bcc) . ", Subject: " . $subject . ", Headers: " . str_replace("\r\n", ",", $headers));
}
// Send result
return ($result === TRUE) ? 1 : 0;
}
/**
*
* Default mail "style sheet"
*/
function mail_default_stylesheet()
{
$stylesheet = "<style type=\"text/css\">\n";
$stylesheet .= "* {\n";
$stylesheet .= "font-family:Verdana;\n";
$stylesheet .= "}\n\n";
$stylesheet .= "table {\n";
$stylesheet .= "font-family:Verdana;\n";
$stylesheet .= "border-width:1px;\n";
$stylesheet .= "border-style:inset;\n";
$stylesheet .= "font-size:11px;\n";
$stylesheet .= "width:800px;\n";
$stylesheet .= "}\n\n";
$stylesheet .= "th {\n";
$stylesheet .= "background:#C0C0C0;\n";
$stylesheet .= "}\n\n";
$stylesheet .= "h1 {\n";
$stylesheet .= "font-family:Verdana;\n";
$stylesheet .= "font-size:16px;\n";
$stylesheet .= "color:" . $_PAGE_INFO['ini']['report']['color'] . ";\n";
$stylesheet .= "margin-top:3px;\n";
$stylesheet .= "margin-bottom:3px;\n";
$stylesheet .= "}\n\n";
$stylesheet .= "h2{\n";
$stylesheet .= "font-family:Verdana;\n";
$stylesheet .= "font-size:12px;\n";
$stylesheet .= "color:" . $_PAGE_INFO['ini']['report']['color'] . ";\n";
$stylesheet .= "margin-top:3px;\n";
$stylesheet .= "margin-bottom:3px;\n";
$stylesheet .= "}\n\n";
$stylesheet .= "</style>\n";
return $stylesheet;
}
?>