Email Attachment Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
arach2k5
Forum Newbie
Posts: 2
Joined: Mon Oct 25, 2004 5:17 pm

Email Attachment Problem

Post by arach2k5 »

Hello all - quick question:

I have a PHP script which processes a standard HTML form, one of which is a file upload field. In my test environment, Win2003/IIS6/PHP5.02, the form works beautifuly - it uploads files to a directory and properly attaches files (image, doc, etc.) to the message.

However, on the "live" environment, RHL7.3/Apache1.3/PHP4.3.3, the script malfunctions as follows: it still properly uploads files, but attachments seem to get sent as text, like this:

--==Multipart_Boundary_x0e1804ee554c0b0fd6b3eff6ad50ad93x
Content-Type: multipart/mixed;
File Name="1098735514-CanonCalculator.jpg"
Content-Transfer-Encoding: base64

/9j/4AAQSkZJRgABAQEBLAEsAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYF
BgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoK...

...and so on.

Is this simply a problem with the earlier version of PHP vs the later one? (Which works perfectly, as I described above.) Can anybody shed light on this? If it will help, here is the relevant code where the parts of the message are assembled:

[syntax=php]// Create email message headers
$Headers = "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" Boundary=\"{$MIMEBoundary}\"" .
"\nFrom: $Name < $From >\r\n" . "X-Mailer: PHP/" . phpversion();

// Create the Non-MIME part of the message body...
$Body = "This is a multi-part message in MIME format.\n\n" .
"--{$MIMEBoundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$NonMIME . "\n\n"; // Here is the Non-MIME part of the message

// ...and add the file attachment to the message
$Body .= "--{$MIMEBoundary}\n" .
"Content-Type: {$AttachmentType};\n" .
"Name=\"{$AttachmentName}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$AttachmentData . "\n\n" .
"--{$MIMEBoundary}--\n";[/syntax]

And the basic encoding process which happens earlier (of course):

[syntax=php]/* Process file for encoding first, otherwise if uploading is processed first, the encoding process will fail */
$AttachmentAttribute = $FileTempName;
$BoundarySemiRandom = md5(time());
$MIMEBoundary = "==Multipart_Boundary_x{$BoundarySemiRandom}x";
$AttachmentType = "multipart/mixed";

// Open the file and read it into $AttachmentData
$File = fopen($AttachmentAttribute,'rb');
$AttachmentData = fread($File,filesize($AttachmentAttribute));
fclose($File);

// Encode the file into base64 in preparation for sending
$AttachmentData = chunk_split(base64_encode($AttachmentData));[/syntax]

Thanks very much in advance.

Allen
Post Reply