homepage Welcome to WebmasterWorld Guest from 54.205.144.54
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Resource ID #40 on PHP mail() attachments
Why won't the actual file name come through?
neophyte




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

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.

Neophyte

*******************************

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;
}
else
{ # 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));
$data=chunk_split(base64_encode($data));
fclose($fileName);

# 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;

# SEND THE EMAIL
ini_set('sendmail_from',$fromAdd);

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

ini_restore('sendmail_from');

return $mail_sent;
}

 

Birdman




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

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));
$data=chunk_split(base64_encode($data));
fclose($fileHandle);

Regards,

Marty

neophyte




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

Birdman -

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

Neophyte

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved