Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 11-03-2008, 11:24 PM   #1
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
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?
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 11-03-2008, 11:41 PM   #2
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
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);
?>
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 11-03-2008, 11:53 PM   #3
borked
Totally Borked
 
borked's Avatar
 
Industry Role:
Join Date: Feb 2005
Posts: 6,284
not php, but I like dhtmlxVault
__________________

For coding work - hit me up on andy // borkedcoder // com
(consider figuring out the email as test #1)



All models are wrong, but some are useful. George E.P. Box. p202
borked is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 11-03-2008, 11:56 PM   #4
BigBen
Confirmed User
 
Join Date: Nov 2004
Location: scv
Posts: 2,299
I use http://www.phpguru.org/static/htmlMimeMail5.html.

Works great.
BigBen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 11-04-2008, 12:06 AM   #5
GrouchyAdmin
Now choke yourself!
 
GrouchyAdmin's Avatar
 
Industry Role:
Join Date: Apr 2006
Posts: 12,085
Use Pear's commonly installed functionality, or PHPMailer.

I won't even begin to bitch about blindly trusting is_uploaded_file() or the PHP3isms..
__________________
GrouchyAdmin is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 11-04-2008, 01:35 AM   #6
eMonk
Confirmed User
 
Industry Role:
Join Date: Aug 2003
Location: Canada
Posts: 2,310
thx for the help guys & thx varius for the instant msger help!

these include classes are new to me... very interesting stuff!
eMonk is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.