/ Published in: PHP
Sends mail to the e-mail address specified. Supports attaching files, multi-part message, and e-mail encodings. Works well with Asian cell phones.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Sends mail to the e-mail address given. * Supports attaching files and multiple encodings. * @param string The email address of the recipient * @param string The name to include in the from header * @param string The e-mail address to include in the from header * @param string the e-mail subject * @param string the e-mail body. * @param boolean true if the body is html or false if plain text. * @param string a path to a file on the server to attach to the e-mail. Null or an empty string indicates that there is no attachment. * @param string the encoding with which to encode the subject, from, and body. */ function SendMail($emailaddress, $from, $fromaddress, $emailsubject="", $body="", $html = true, $attachment="", $encoding="utf-8") {//{{{ # Is the OS Windows or Mac or Linux $eol=" "; $eol="\r"; } else { $eol="\n"; } //set subject encoding $emailsubject = encode_mail_string($emailsubject, $encoding); } $from = encode_mail_string($from, $encoding); } $msg = ""; # Common Headers $headers .= "From: ".$from." <".$fromaddress.">".$eol; $headers .= "Reply-To: ".$from." <".$fromaddress.">".$eol; $headers .= "Return-Path: ".$from." <".$fromaddress.">".$eol; // these two to set reply address $headers .= 'MIME-Version: 1.0'.$eol; //send multipart message # Boundry for marking the split & Multitype Headers $headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol; # File for Attachment $f_name = $attachment; $f_contents=chunk_split(base64_encode($f_contents));//Encode The Data For Transition using base64_encode(); # Attachment $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: application/jpeg; name=\"".$file."\"".$eol; $msg .= "Content-Transfer-Encoding: base64".$eol; $msg .= "Content-Disposition: attachment; filename=\"".basename($attachment)."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $f_contents.$eol.$eol; # Setup for text OR html $msg .= "Content-Type: multipart/alternative".$eol; $contentType = "text/plain"; if ($html) { $contentType = "text/html"; } # Body $msg .= "--".$mime_boundary.$eol; $msg .= "Content-Type: ".$contentType."; charset=\"".$encoding."\"".$eol; $msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $body.$eol.$eol; # Finished $msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security. see Injection. } else { $headers .= "Content-Type: text/plain; charset=\"".$encoding."\"".$eol; $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !! $msg .= $body.$eol.$eol; } // SEND THE EMAIL //LogMessage("Sending mail to: ".$emailaddress." => ".$emailsubject); //ini_set(sendmail_from, '[email protected]'); // the INI lines are to force the From Address to be used ! return $success; }//}}}