GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Coders: How can I send multiple attachments via form to email with php 5 (https://gfy.com/showthread.php?t=866662)

eMonk 11-03-2008 11:24 PM

Coders: How can I send multiple attachments via form to email with php 5
 
i would like to send 10 image files from my html form to php but having some problems & i've been searching google all day with no solid leads & was hoping that someone here would know because i know this has been done many times before. here's my php code:

Code:

<?php

// Read POST request params into global vars
$name = $_POST['name'];
$to      = $_POST['to'];
$email  = $_POST['email'];
$subject = $_POST['subject'];

// Obtain file upload vars
$fileatt1      = $_FILES['fileatt1']['tmp_name'];
$fileatt1_type = $_FILES['fileatt1']['type'];
$fileatt1_name = $_FILES['fileatt1']['name'];

// THIS HAS BEEN REPEATED AND I HAVE CHANGED THE FILE NAME
$fileatt2      = $_FILES['fileatt2']['tmp_name'];
$fileatt2_type = $_FILES['fileatt2']['type'];
$fileatt2_name = $_FILES['fileatt2']['name'];

$headers = "From: $email";

if (is_uploaded_file($fileatt1)) {
// Read the file to be attached ('rb' = read binary)
$file1 = fopen($fileatt1,'rb');
$data1 = fread($file1,filesize($fileatt1));
fclose($file1);
}

if (is_uploaded_file($fileatt2))  {
// Read the file to be attached ('rb' = read binary)
$file2 = fopen($fileatt2,'rb');
$data2 = fread($file2,filesize($fileatt2));
fclose($file2);
}

// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
            "Content-Type: multipart/mixed;\n" .
            " boundary=\"{$mime_boundary}\"";
                       
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
"Name: " . $name . "\n\n" .
"Email: " . $email . "\n\n" ;


// Base64 encode the file data
$data1 = chunk_split(base64_encode($data1));
$data2 = chunk_split(base64_encode($data2));

// Add file attachment to the message
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt1_type};\n" .
" name=\"{$fileatt1_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt1_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data1 . "\n\n" .
"--{$mime_boundary}--\n";

// THIS HAS BEEN REPEATED
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt2_type};\n" .
" name=\"{$fileatt2_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt2_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data2 . "\n\n" .
"--{$mime_boundary}--\n";

// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Thank you <b>$name</b></p>
<p>We have received your application and we will be in touch shortly</p>";
} else {
echo "<p>Sorry, but there as an error. If the problem persists please email us at </p>";
}

?>

this code only sends the first file and not the second, any ideas?

Varius 11-03-2008 11:41 PM

You should start using PEAR, it's great for all kinds of things :)

Example:

PHP Code:

<?php
include('Mail.php');
include(
'Mail/mime.php');

$text 'Text version of email';
$html '<html><body>HTML version of email</body></html>';
$file1 './example.txt';
$file2 './example2.txt';
$crlf "\n";
$hdrs = array(
              
'From'    => '[email protected]',
              
'Subject' => 'Test mime message'
              
);

$mime = new Mail_mime($crlf);

$mime->setTXTBody($text);
$mime->setHTMLBody($html);
$mime->addAttachment($file'text/plain');
$mime->addAttachment($file2'text/plain');

$body $mime->get();
$hdrs $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('postmaster@localhost'$hdrs$body);
?>


borked 11-03-2008 11:53 PM

not php, but I like dhtmlxVault

BigBen 11-03-2008 11:56 PM

I use http://www.phpguru.org/static/htmlMimeMail5.html.

Works great.

GrouchyAdmin 11-04-2008 12:06 AM

Use Pear's commonly installed functionality, or PHPMailer.

I won't even begin to bitch about blindly trusting is_uploaded_file() or the PHP3isms..

eMonk 11-04-2008 01:35 AM

thx for the help guys & thx varius for the instant msger help!

these include classes are new to me... very interesting stuff! :thumbsup


All times are GMT -7. The time now is 03:56 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123