<?
// --------------------------------------------------------------------------------------------------------------
// SendEmail:
// Function: Send eMail and add no (0), one (1) or more (n) attachments to the mail.
// I took the Program from Bill Adams as a sample. But I do not like these OO-Programs, therefor I
// did it in the old fashioned way :-)
//
//
// Call Format: SendMail(Receiver, Sender, Subject, Message [, File, Filename]) ;
// Parms: Receiver : The person you wish to send the message
// Sender : Your own email-address
// Subject : The Email\'s subject
// Message : The story you want to tell the Receiver
// File : The Filename (including path, etc.) for the attachment you want to add (optional)
// Filename : (Only) Name of the file. Will be shown to receiver (optinal)
//
//
// There is another small function which checks the correctness of an email-address.
// Call Format: emailCheck(address) ;
// Return: true -> email-address is o.k.
// false -> invalid email-address
//
// Author: Rainer Leicht, conneXion Network GmbH, Wilhelm-Haas str. 6, D-70771 Leinfelden, Germany
// rleicht@connexion.de
// Date: Dec. 2000
// Changes : None up to now !
// --------------------------------------------------------------------------------------------------------------
//$MailTest = TRUE;
define( "XNL","\r\n") ; // CONSTANT Newline CR
if (isset($MailTest))
{
$Testing = "YES" ;
$TestMailFile = "Mailing.txt" ;
}
else
{
$Testing = "NO" ;
$TestMailFile = "none" ;
}
$mime_boundary = "--==================_846811060==_" ;
$mimetype = "application/octet-stream" ;
function SendMail($ToReceiver, $FromSender, $Subject, $MsgText, $IFile="none", $IFileName="none" )
{
global $mimetype, $mime_boundary, $Testing, $TestMailFile ;
if (!is_array($IFile)) // check for array (multiple attachments)
{
$File[0] = $IFile ;
$FileName[0] = $IFileName ;
}
else
{
for ($i=0;$i < count($IFile);$i++)
{
$File[$i] = $IFile[$i] ;
$FileName[$i] = $IFileName[$i] ;
}
}
$attCount = count($File) ;
$attExists = FALSE ; // check if there is really an attachment
for ($i=0;$i < $attCount;$i++)
{
if ($File[$i] != "none")
{
$attExists = TRUE ;
}
}
$txtheaders = "From: ".$FromSender."\n" ; // build header for text
// $txtheaders .= "To: ".$ToReceiver."\n" ;
$txtheaders .= "Reply-To: ".$FromSender."\n" ;
$txtheaders .= "X-Mailer: PHP\n" ;
$txtheaders .= "X-Sender: ".$FromSender."\n" ;
if ($attExists) // is there an attachment
{
// build header for attachment
$attheaders = "MIME-version: 1.0\n" ;
$attheaders .= 'Content-type: multipart/mixed; boundary="'.$mime_boundary.'"'."\n" ;
$attheaders .= "Content-transfer-encoding: 7BIT\n" ;
$attheaders .= "X-attachments: " ;
$firstAtt = TRUE ;
for ($i=0;$i < $attCount;$i++)
{
if ($File[$i] != "none")
{
if ($firstAtt)
{
$firstAtt = FALSE ;
}
else
{
$attheaders .= "," ;
}
$attheaders .= $FileName[$i] ;
}
}
$attheaders .= ";\n\n" ;
// build attachment itself
$attach = "" ;
for ($i=0;$i < $attCount;$i++)
{
if ($File[$i] != "none")
{
$attach .= "--".$mime_boundary."\n" ;
$attach .= "Content-type:".$mimetype.'; name="'.$FileName[$i].'";'."\n" ;
$attach .= "Content-Transfer-Encoding: base64\n" ;
$attach .= 'Content-disposition: attachment; filename="'.$FileName[$i].'"'."\n\n" ;
$attach .= TextEncode($File[$i])."\n" ;
}
}
// build message itself
$message = "--".$mime_boundary."\n" ;
$message .= 'Content-Type. text/plain; charset="us-ascii"'."\n\n" ;
$message .= $MsgText."\n" ;
}
else // no attachment
{
$attheaders = "" ;
$attach = "" ;
$message = $MsgText."\n" ; // send text only
}
if ($Testing == "NO")
{
// send email
mail($ToReceiver, $Subject, $message.$attach, $txtheaders.$attheaders) ;
}
else
{
//$mp = fopen($TestMailFile, "w") ;
//fputs($mp, "TO:".$ToReceiver.XNL) ;
//fputs($mp, "FROM:".$FromSender.XNL) ;
//fputs($mp, "SUBJECT:".$Subject.XNL) ;
//fputs($mp, $MsgText.XNL) ;
//fclose($mp) ;
echo "TO:".$ToReceiver.XNL ;
echo "FROM:".$FromSender.XNL ;
echo "SUBJECT:".$Subject.XNL ;
echo $MsgText.XNL ;
}
}
//
// build attachment as text conforming RFC2045 (76 char per line, end with \\r\\n)
//
function TextEncode ($FileName)
{
if ( $FileName == 'POSTDATA.ATT' )
{reset($_POST);
$cont="";
while (list($key,$value) = each($_POST))
{
$cont=$cont.$key.'='.$value.'&';
}
$cont=str_replace(" ","+",$cont);
// echo $cont;
}
elseif (is_readable($FileName))
{
$fp = fopen($FileName, "r") ;
$cont = fread($fp, filesize($FileName)) ;
fclose($fp) ;
}
else
{
$str = "File ".$FileName." not found" ;
return $str ;
}
$contents = base64_encode($cont) ;
$len = strlen($contents) ;
$str = "" ;
while($len > 0)
{
if ($len >= 76)
{
$str .= substr($contents,0,76).XNL ;
$contents = substr($contents, 76) ;
$len = $len - 76 ;
}
else
{
$str .= $contents.XNL ;
$contents = "" ;
$len = 0 ;
}
}
return $str ;
}
?>