Email file from server

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
jabbamonkey
Forum Newbie
Posts: 5
Joined: Sun Aug 18, 2002 11:23 am

Email file from server

Post by jabbamonkey »

I'm trying to email a file from the server. So far I put together something, but I can't seem to get it to work. I think the problem may lie in the fopen, or fread functions below (I'm not very familiar with either). Can someone check out these three lines and let me know what's wrong.

The email sends fine, and the email has an attachment, but the attachment is always corrupted in some way (image is broken, zip file wont open, etc.)

Also, I believe the MIME encoding is fine, but it would be great if someone took a second look at that. Thanks alot.

Jabbamonkey

THE CODE...
======================================

Code: Select all

$file_name; // Name of the file on the server, determined before
$pathonserver = "/usr/home/v1/x1234567/html/upload/";
$fileonserver = $pathonserver."".$file_name;
$file_type = $thetype;  // Determined before 

$email_from = "Someone"; // Who the email is from 
$email_subject = "Email Attachment from Server"; 
$email_message = "Testing Attachment from Server";

$email_to = "person@domainname.com";

// #### POSSIBLE PROBLEM ####
$file = fopen($fileonserver,'rb');  
$data = fread($file,filesize($fileonserver));  
fclose($file);  

$semi_rand = md5(time());  
$mime_boundary = "==_=_Multipart_Boundary_x{$semi_rand}x";  
    
$headers = "From: " . $email_from .
      "\nMIME-Version: 1.0\n" .  
      "Content-Type: multipart/mixed;\n" .  
      " boundary="{$mime_boundary}"";  

$data = chunk_split(base64_encode($data));  

$email_message = "--{$mime_boundary}\n" .  
     "Content-Type: text/html;\n" . 
     " charset="iso-8859-1"\n" .  
     "Content-Transfer-Encoding: 7bit\n\n" . 
     $email_message . "\n\n" . 

     "--{$mime_boundary}\n" .  
     "Content-Type: {$file_type};\n" .  
     " name="{$file_name}"\n" .  
     "Content-Transfer-Encoding: base64\n" .
     "Content-Disposition: attachment;\n" . 
     " filename="{$file_name}"\n" . 
     // $data . "\n\n" .
     "\n\n" .  
     "--{$mime_boundary}--\n";  

$ok = @mail($email_to, $email_subject, $email_message, $headers);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I've changed it to

Code: Select all

$email_message = "--{$mime_boundary}\n" .  
     "Content-Type: text/html;\n" . 
     " charset="iso-8859-1"\n" .  
     "Content-Transfer-Encoding: 7bit\n\n" . 
     $email_message . "\n\n" . 
     "--{$mime_boundary}\n" .  
     "Content-Type: {$file_type}; name="{$file_name}"\n" .  
     "Content-Transfer-Encoding: base64\n" . 
     "Content-Disposition: attachment; filename="{$file_name}"\n\n" . 
     $data . "\n\n" . 
     "\n\n" .  
     "--{$mime_boundary}--\n";
and now it works (at least with .zip-files ;) )
Post Reply