Multiple email attachments with php

I’ve been struggling to find any simple to follow guides or tutorials on how to do this so I thought I would ask here. I am sending out an email and all I want to do is attach 2 pdf files to it. I have found a script that allows me to send a single email with a single attachment.

<?php
$from = "me@domain.com";
$to = $_POST['email'];
$subject = "Uploaded file";
$mail_body = "Message body";
$fileatt = $_FILES['file']['tmp_name'];
$fileatt_type = $_FILES['file']['type'];
$fileatt_name = $_FILES['file']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt))
{
	$file= fopen($fileatt, 'rb');
	$data = fread($file, filesize($fileatt));

	$semi_rand = md5(time());
	$mime_boundary = "Multipart_Boundary_x{$semi_rand}x";

	$headers .= "\
MIME-Version: 1.0\
" . "Content-Type: multipart/mixed;\
" . " boundary=\\"{$mime_boundary}\\"";

	$mail_body = "This is a multi-part message in MIME format.\
\
" . "--{$mime_boundary}\
" . "Content-Type: text/html; charset=\\"iso-8859-1\\"\
" .
	"Content-Transfer-Encoding: 7bit\
\
" . $mail_body . "\
\
";

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

	$mail_body .= "--{$mime_boundary}\
" . "Content-Type: {$fileatt_type};\
" . " name=\\"{$fileatt_name}\\"\
" . "Content-Disposition: attachment;\
" .
	" filename=\\"{$fileatt_name}\\"\
" . "Content-Transfer-Encoding: base64\
\
" . $data . "\
\
" . "--{$mime_boundary}--\
";

	$ok = @mail($to, $subject, $mail_body, $headers);
	
	if($ok)
		echo "Email sent :)";
	else
		echo "Failed !!!";
	
	fclose($file);
}
?>

This works fine but how can I adapt this to send 2 attachments? Here’s the code for the form that gets the user info;

<form action="process.php" method="post" enctype="multipart/form-data">
To:<input type="text" name="email" /><br />
File: <input type="file" name="file" id="file"  /> <br />
<input type="submit" name="submit" value="Sent"  />
</form>

If anyone can help or point me in the right direction, then I would be very grateful.

Put this into a separate php file and submit the form with two attachments.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
<head>
	<title>Multiple File Upload Test</title>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<body>
	<?php if($_POST) : ?>
		<?php @extract($_FILES);?>
		<pre>
		<?php var_dump($file);?>	
		</pre>
	<?php endif; ?>
	<form action="test.php" method="post" enctype="multipart/form-data">
		To:<input type="text" name="email" /><br />
		File 1: <input type="file" name="file[]" id="file"  /> <br />
		File 2: <input type="file" name="file[]" id="file"  /> <br />
		<input type="submit" name="submit" value="Sent"  />
	</form>
</body>
</html>

You’ll see that $file becomes a multidimensional array:


array
  'name' =>
    array
      0 => string 'vlcsnap-2010-05-24-11h03m56s156.png' (length=35)
      1 => string 'screen.png' (length=10)
  'type' =>
    array
      0 => string 'image/png' (length=9)
      1 => string 'image/png' (length=9)
  'tmp_name' =>
    array
      0 => string 'C:\\wamp\	mp\\php718D.tmp' (length=23)
      1 => string 'C:\\wamp\	mp\\php71AD.tmp' (length=23)
  'error' =>
    array
      0 => int 0
      1 => int 0
  'size' =>
    array
      0 => int 234418
      1 => int 282939