Actually, the easiest way to send a file using mail() is to simply include a link in the $message variable. It might look like this. $subject = "Here is an email with a link to a file"; $message = "Thank you for visiting our site. Please see the <A href="http://www.yourDomain.com/yourFileName.xls">attached file</a> to review the document that you requested."; $headers = 'From: ' . "\r\n" . 'Reply-To: ' . "\r\n" . 'Content-Type: text/html; charset="utf-8"' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); $returnpath = "-fYourName@yourDomain.com"; mail($to, $subject, $message,$headers,$returnpath); In this way you can avoid having to email the file. If you actually need to attach the file then you might want to use the sample code for sending an email with attachment that is posted at webcheatsheet dot com. It shows how to set the content-type and pass in the file.
Below is the code for file attachment in mail. <?php function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) { $file = $path.$filename; $file_size = filesize($file); $handle = fopen($file, "r"); $content = fread($handle, $file_size); fclose($handle); $content = chunk_split(base64_encode($content)); $uid = md5(uniqid(time())); $name = basename($file); $header = "From: ".$from_name." <".$from_mail.">\r\n"; $header .= "Reply-To: ".$replyto."\r\n"; $header .= "MIME-Version: 1.0\r\n"; $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $header .= "This is a multi-part message in MIME format.\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-type:text/plain; charset=iso-8859-1\r\n"; $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $header .= $message."\r\n\r\n"; $header .= "--".$uid."\r\n"; $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here $header .= "Content-Transfer-Encoding: base64\r\n"; $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"; $header .= $content."\r\n\r\n"; $header .= "--".$uid."--"; if (mail($mailto, $subject, "", $header)) { echo "mail send ... OK"; // or use booleans here } else { echo "mail send ... ERROR!"; } } $my_file = "somefile.zip"; $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/"; $my_name = "Olaf Lederer"; $my_mail = "my@mail.com"; $my_replyto = "my_reply_to@mail.net"; $my_subject = "This is a mail with attachment."; $my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf"; mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message); ?>
I would agree with Red Swordfish use the link as it will speed up delivery and most modern day spam filters will block emails sent using PHP mailer as they are mostly deemed spammy let alone with attachments. Check out SMTP email class if you want to send these items securely using attachment.