function send_mail($fromname, $emailaddress, $fromaddress, $emailsubject, $body, $fileattach = false) {
# Is the OS Windows or Mac or Linux if (strtoupper(substr(PHP_OS,0,3)=='WIN')) $eol="rn"; elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) $eol="r"; else $eol="n";
$mime_boundary_1 = md5(time()); $mime_boundary_2 = "1_".$mime_boundary_1; $mail_sent = false; # Common Headers $headers = ""; $headers .= 'From: '.$fromname.'<'.$fromaddress.'>'.$eol; $headers .= 'Reply-To: '.$fromname.'<'.$fromaddress.'>'.$eol; $headers .= 'Return-Path: '.$fromname.'<'.$fromaddress.'>'.$eol; // these two to set reply address $headers .= "Message-ID: <".$now."webmaster@".$_SERVER['SERVER_NAME'].">".$eol; $headers .= "X-Mailer: PHP v".phpversion().$eol; // These two to help avoid spam-filters
# Boundry for marking the split & Multitype Headers $headers .= 'MIME-Version: 1.0'.$eol; $headers .= "Content-Type: multipart/mixed;".$eol; $headers .= " boundary=\"".$mime_boundary_1."\"".$eol; $msg = ""; # Building Message Body $msg .= "--".$mime_boundary_1.$eol; $msg .= "Content-Type: multipart/alternative;".$eol; $msg .= " boundary=\"".$mime_boundary_2."\"".$eol.$eol; # Text Version $msg .= "--".$mime_boundary_2.$eol; $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$eol; $msg .= strip_tags(str_replace("<br>", $eol, $body)).$eol.$eol; # HTML Version $msg .= "--".$mime_boundary_2.$eol; $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$eol; $msg .= str_replace($eol, "<br>", $body).$eol.$eol; # Finished Message Body $msg .= "--".$mime_boundary_2."--".$eol.$eol; # Begin Adding Attachments if ($fileattach) { for($i=0; $i < count($fileattach); $i++) { if (is_file($fileattach[$i]["file"])) { # File for Attachment $file_name = substr($fileattach[$i]["file"], (strrpos($fileattach[$i]["file"], "/")+1)); $handle=fopen($fileattach[$i]["file"], 'rb'); $f_contents=fread($handle, filesize($fileattach[$i]["file"])); $f_contents=chunk_split(base64_encode($f_contents)); //Encode The Data For Transition using base64_encode(); //Remove file unlink($fileattach[$i]["file"]); # Attachment $msg .= "--".$mime_boundary_1.$eol; $msg .= "Content-Type: ".$fileattach[$i]["content_type"].";".$eol; $msg .= " name=\"".$file_name."\"".$eol; $msg .= "Content-Transfer-Encoding: base64".$eol; $msg .= "Content-Disposition: attachment;".$eol; $msg .= " filename=\"".$file_name."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $f_contents.$eol.$eol; } } } # Finished $msg .= "--".$mime_boundary_1."--".$eol.$eol; // finish with two eol's for better security. see Injection. # SEND THE EMAIL ini_set(sendmail_from, $fromaddress); // the INI lines are to force the From Address to be used ! if (mail($emailaddress, $emailsubject, $msg, $headers)) $mail_sent = true; ini_restore(sendmail_from); return $mail_sent; }
|