Jump to content

PHP mail() with ataced file is corrupt.


Bengt7jqb

Recommended Posts

 

Hi, what am I doing wrong?

 

I can sen that mail in both text and html format but the atached file is

corrupt. I can't understand whay (so far)

I use my Debian Linux and Apache 2 PHP 5

 

Here is the mail.php file

 

<html>
<head>
<title> Sending Email </title>
</head>

<body>
<?php
// Read POST request params into global vars
$to      = $_POST['to'];
$from    = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];

// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$fileatt_size = $_FILES['fileatt']['size'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {
   // Read the file to be attached ('rb' = read binary)
   $file = fopen($fileatt,'rb');

   // Base64 encode the file data
   $attachment = fread($file, $fileatt_size);
   $attachment = base64_encode($attachment);
   $attachment = chunk_split($attachment);
   fclose($file);
   }

// Generate a boundary string
$random_hash = md5(date('r', time()));

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0"
      ."\nContent-Type: multipart/mixed;"
      ."\n boundary=\"PHP-mixed-".$random_hash."\"";

ob_start(); //Turn on output buffering
?>

This is a multi-part message in MIME format.

--PHP-mixed-<?php echo $random_hash; ?>

Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; 
?>"

--PHP-alt-<?php echo $random_hash; ?>

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


<?php echo $message; ?>

<?php echo $fileatt_type; ?>

<?php echo $fileatt_name; ?>

$_FILES = <?php print_r($_FILES); ?>

--PHP-alt-<?php echo $random_hash; ?>

Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit


HTML - <b><?php echo $message; ?><br><?php echo $fileatt_type; ?><br><?php echo 
$fileatt_name; ?><br></b> -
<br> $_FILES = <?php print_r($_FILES); ?><br>


--PHP-alt-<?php echo $random_hash; ?>--

--PHP-mixed-<?php echo $random_hash; ?>

Content-Type: <?php echo $fileatt_type; ?>;
name="<?php echo $fileatt_name; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="<?php echo $fileatt_name; ?>"

<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--

<?php
   //copy current buffer contents into $message variable and delete current 
output buffer
$message = ob_get_clean();

// Send the message
if ( @mail($to, $subject, $message, $headers) ) {
   echo "<p>Mail sent! Yay PHP! $fileatt_type $fileatt_name </p>";
   echo "<p> \$_FILES = ".nl2br(print_r($_FILES,1))."</p>";
   }
else
   echo "<p>Mail could not be sent. Sorry!</p>";

?>
</body>
</html>

 

You can try it here

  http://radiomuseum-se.data-doc.se/Downloads/phpemail/

The atached picture is this one

  http://radiomuseum-se.data-doc.se/bild/close.gif

and the mail from it. The file part only wtih corrupt file.

 

--PHP-mixed-68dab7302103fa4fe524499c8d626b2d
Content-Type: image/gif;
name="close.gif"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="close.gif"

R0lGODlhFVwwFVww91wwXDBCGBhSISGEjKWUnK2cpb2lOUKlp b2tOVK9vc7GUlrGxtbOUlrOWlrO
hIzWWlrWWmPeY2Petb3e3ufnY2Pna2Pna2vnc2vnc3Pvc3Pve 3Pve3vvhHvvlIzvnIzvtZz3hHP3
hHv3jHv3jIT3lIT3nIz3rZT3tZz3vZz/nIz/vaX/3rX/77X/9/f/////////XDD/////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////IfkEAVwwXDAvXDAs
XDBcMFwwXDAVXDAVXDBcMAjzXDBfvChAsKDBgwQFDkSRQsWKh xAjrlCRAkWBgSFEiPDAsaPHjhpD
NCiQYYMIEiU+eixBQsSGDCMzaDAJXDBcMIcOHG7mrOlSA8wCFi4IbdECgIajRwEQFXrBwsgKFqIS
LRrVglKiVSs8hSqVKFwwq1Ozai1cMKGCWbNTr7Y4axbCSAgU2 FaYSlQuBbdky7JVC0AuBLwP/gpW
61Xw3wcjAw+eCiGtYcQFHkiGoFawY8mQGTB4EGAqZslTAzzQPFLzZqKfMaM2XXrBAtOwYzNwzWBk
ggSvZctecHvkgdvAgwsHfqDBwAPIkytfjvyiwAjQo0ufDl1gQ FwwXDA7
--PHP-mixed-68dab7302103fa4fe524499c8d626b2d--

 

And the werry same file but mail is sent by Thunderbird and file is OK

--------------050003010301080804010002
Content-Type: image/gif;
name="close.gif"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
filename="close.gif"

R0lGODlhFQAVAPcAAEIYGFIhIYSMpZScrZylvaU5QqWlva05U r29zsZSWsbG1s5SWs5aWs6E
jNZaWtZaY95jY961vd7e5+djY+drY+dra+dza+dzc+9zc+97c+97e++Ee++UjO+cjO+1nPeE
c/eEe/eMe/eMhPeUhPecjPetlPe1nPe9nP+cjP+9pf/etf/vtf/39/////////8A////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////////////////////////yH5BAEAAC8ALAAAAAAVABUAAAjzAF+8KECwoMGD
BAUORJFCxYqHECOuUJECRYGBIUSI8MCxo8eOGkM0KJBhgwgSJ T56LEFCxIYMIzNoMAkAAIcO
HG7mrOlSA8wCFi4IbdECgIajRwEQFXrBwsgKFqISLRrVglKiV Ss8hSqVKACrU7NqLQChglmz
U6+2OGsWwkgIFNhWmEpULgW3ZMuyVQtALgS8D/4KVutV8N8HIwMPngohrWHEBR5IhqBWsGPJ
kBkweBBgKmbJUwM80DxS82ainzGjNl16wQLTsGMzcM1gZIIEr 2XLXnB75IHbwIMLB36gwcAD
yJMrX478osAI0KNLnw5dYEAAADs=
--------------050003010301080804010002--

 

Any idea what is wrong ??

 

73 dé Bengt

Link to comment
https://forums.phpfreaks.com/topic/186732-php-mail-with-ataced-file-is-corrupt/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.