Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Resource ID #40 on PHP mail() attachments

Why won't the actual file name come through?



3:03 pm on Jan 31, 2008 (gmt 0)

10+ Year Member

Hello All -

I'm wading into the deeper waters of PHP mail at the moment with trying my hand at attaching files (.doc and .txt for now) and... generally speaking... so far so good.

I can get the mail to send, I can get the attachments to attach, but when I open these test emails, the attachments (while they ARE there and they DO open and they DO HAVE the proper extension for the file type attached) the actual document names are ALWAYS changed to "Resource ID #40".

For example, in the test email the attachment should be named "test_document.doc", but instead, it's named Resource ID #40.doc.

I can even click on and open these attachments and all the text is there as well... but why won't the actual file name of the document come through? Humm...

I've checked and re-checked the document names and types (i.e. $_FILES['upload']['name'] / $_FILES['upload']['type']) and those are fine. The path is fine as well.

It is true that $_FILES['upload']['name'] does include the document extension as well (i.e. test_document.doc), but that's okay, right? That wouldn't be causing this... ah, er, would it?

The script I'm using is shown below. If someone can find the error of my way's I'd be grateful as I don't know what else to test for.



function send_mail($toName, $toAdd, $fromName, $fromAdd, $subject, $body, $emailType, $attachments)
$eol = "\r\n"; //End of line characters
$mime_boundary = md5(time());

# Common Headers
$headers = "To: " .$toName."<".$toAdd.">".$eol;
$headers .= "From: ".$fromName."<".$fromAdd.">".$eol;
$headers .= "Reply-To: ".$fromName."<".$fromAdd.">".$eol;
$headers .= "Return-Path: ".$fromName."<".$fromAdd.">".$eol;
$headers .= "Message-ID: <".time()."-".$fromAdd.">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$headers .= "X-Priority: 1".$eol;
$headers .= "X-MSmail-Priority: High".$eol;

# Boundry for marking the split & Multitype Headers
$headers .= 'MIME-Version: 1.0'.$eol.$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$mime_boundary."\"".$eol.$eol;

# Open the first part of the mail
$msg = "--".$mime_boundary.$eol;

$htmlalt_mime_boundary = $mime_boundary."_htmlalt";

# Setup for text OR html -
$msg .= "Content-Type: multipart/alternative; boundary=\"".$htmlalt_mime_boundary."\"".$eol.$eol;

if($emailType == 'Text')
{ # Text Version
$msg .= "--".$htmlalt_mime_boundary.$eol;
$msg .= "Content-Type: text/plain; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $body . $eol . $eol;
{ # HTML Version
$msg .= "--".$htmlalt_mime_boundary.$eol;
$msg .= "Content-Type: text/html; charset=iso-8859-1".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
$msg .= $body . $eol . $eol;

//close the html/plain text alternate portion
$msg .= "--".$htmlalt_mime_boundary."--".$eol.$eol;

if ($attachments)
$fileName = $_FILES['upload']['name'];

$fileType = $_FILES['upload']['type'];

$filePath = $_FILES['upload']['tmp_name'];

$fileName = fopen($filePath, 'rb');
$data=fread($fileName, filesize($filePath));

# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$fileType."; name=\"".$fileName."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64" . $eol;
$msg .= "Content-Description: " . $fileName . $eol;
$msg .= "Content-Disposition: attachment; filename=\"".$fileName."\"".$eol.$eol;
$msg .= $data.$eol.$eol;

# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol;


$mail_sent = mail($toAdd, $subject, $msg, $headers);


return $mail_sent;


6:20 pm on Feb 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

No problem, you just need to change the name of one variable... The one you used to fopen() the file. See, you set the $fileName correctly at first then you overwrite it by using it as the file handle, which will be a "Resource id".

Try this instead:

if ($attachments)
$fileName = $_FILES['upload']['name'];

$fileType = $_FILES['upload']['type'];

$filePath = $_FILES['upload']['tmp_name'];

$fileHandle = fopen($filePath, 'rb');
$data=fread($fileHandle, filesize($filePath));




2:18 am on Feb 3, 2008 (gmt 0)

10+ Year Member

Birdman -

That worked like a charm! Thanks so much for showing me the error of my ways!



Featured Threads

Hot Threads This Week

Hot Threads This Month