Really need help

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
c_pattle
Forum Newbie
Posts: 9
Joined: Sat Apr 24, 2010 7:35 am

Really need help

Post by c_pattle »

I've been trying to get this script to work for ages but having no luck. I have an attachment for which I have shown below

Code: Select all

<form method="post" action="order_success2.php" enctype="multipart/form-data">
<ul>
<li><input type="file" name="att[]" size="26" /></li>
<li><input type="file" name="att[]" size="26" /></li>
<li><input type="submit" name="submit" value="Submit!" /></li>
</ul>
</form>
What I'm trying to achieve is to be able to send more than one attachment with an email. At the moment I can only get one to work. Can anyone help pleaseeeeeee?

I've attached the page "order_success2.php"

Thanks
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Really need help

Post by hypedupdawg »

Ummmm... you haven't actually attached any files.

:o

But I'll be happy to help when you do!
c_pattle
Forum Newbie
Posts: 9
Joined: Sat Apr 24, 2010 7:35 am

Re: Really need help

Post by c_pattle »

Thanks. I can't upload .php files so I'll just copy the code

Code: Select all

<?php 

$to = "someone@gmail.com"; 

foreach($_FILES['att'] as $key => $value){

$att = $value['att'];
$att_path = $value['att']['tmp_name'];
$att_name = $value['att']['name'];
$att_size = $value['att']['size'];
$att_type = $value['att']['type'];

$fp = fopen( $att_path, "rb");
$file = fread( $fp, $att_size );
fclose ($fp);

$num = md5(time());
$str = "==multipart_Boundary_x{$num}x";

$file = chunk_split(base64_encode($file));

$subject = "You have been sent some content by " . $_REQUEST['order_company_name']; 

$email = $_REQUEST['order_email'] ; 
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;";
$headers .= "boundary=\"{$str}\"\r\n";
$headers .= "From: $email"; 

$msg .= "This is a multi-part message in MIME format\r\n\n";
$msg .= "--{$str}\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 8bit\r\n";
$msg .= "--{$str}\r\n";

$msg .= "Content-Type: {$att_type}; ";
$msg .= "name=\"{$att_name}\"\r\n";
$msg .= "Content-Disposition: attachment; ";
$msg .= "filename =\"{$att_name}\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "$file\r\n\n";
$msg .= "--{$str}";

$sent = mail($to, $subject, $msg, $headers) ; 
if($sent) 
{print "Thank you.  Your order was sent successfully"; }
else 
{print "Sorry.  We encountered an error sending your mail"; }
}
?>
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: Really need help

Post by hypedupdawg »

I think that your file that deals with the request is ok - but in your initial form, you have two fields with the same name. Having an att[] array is a good way to go, but I think you may need to number them like so:

Code: Select all

<li><input type="file" name="att[0]" size="26" /></li>
<li><input type="file" name="att[1]" size="26" /></li>
If you are doing this for an infinite number of attachments, you could always have some sort of counter set up to loop through, add a field and change the name by 1.

Hope this helps!
Post Reply