phpmailer.sourceforge.net - a mailing class for php that does everything
you need, there are other alternatives - definitely beats fiddling with
all the required headers yourself.
save yourself a big headache. :-)
Robin Wilson wrote:
> Hello
>
> I'm trying to do a function that emails users and sends an attachment.
> But I think something is in the wrong order or something as when I send
> it Exchange discards it.
>
> This is the function which I have put together from various sources:
>
> I would really appreciate any help as I have been up half the night and
> got nowhere!
> Thanks
> Robin
>
> function sendMail($to, $toName, $from, $fromName, $subject, $message,
> $uploadfile, $fileName, $fileType, $fileSize) {
> $eol="\r\n";
> $mime_boundary=md5(time());
>
> # Common Headers
> $headers .= 'From: '. $fromName .' <'. $from .'>'.$eol;
> $headers .= 'Reply-To: '. $toName .' <'. $to .'>'.$eol;
> $headers .= 'Return-Path: '. $fromName.' <'. $from .'>'.$eol; //
> these two to set reply address
> $headers .= "Message-ID:
> <".$mime_boundary."@".$_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/related;
> boundary=\"".$mime_boundary."\"".$eol;
>
> $msg = " ";
>
> if ($uploadfile != "") {
> # File for Attachment
> $file_name = substr($uploadfile, (strrpos($uploadfile, "/")+1));
>
> $handle=fopen($uploadfile, 'rb');
> $f_contents=fread($handle, $fileSize);
> $f_contents=chunk_split(base64_encode($f_contents)); //Encode
> The Data For Transition using base64_encode();
> fclose($handle);
>
> # Attachment
> $msg .= "--".$mime_boundary.$eol;
> $msg .= "Content-Type: ".$fileType."; name=\"".$file_name."\"".$eol;
> $msg .= "Content-Transfer-Encoding: base64".$eol;
> $msg .= "Content-Disposition: attachment;
> filename=\"".$file_name."\"".$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;
>
> # Text Version
> $msg .= "--".$mime_boundary.$eol;
> $msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
> $msg .= "Content-Transfer-Encoding: 8bit".$eol;
> $msg .= strip_tags(str_replace("<br>", "\n", $message)).$eol.$eol;
>
> # HTML Version
> $msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
> $msg .= "Content-Transfer-Encoding: 8bit".$eol;
> $msg .= $message.$eol.$eol;
>
> # Finished
> $msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's
> for better security. see Injection.
>
>
> // Mail it
> mail($to, $subject, $msg, $headers);
> }
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php