Multiple email attachments and upload script

I got several times question about sending email messages with multiple attachments while using the upload class together and the attachment mailer class together. This short tutorial will show how-to do that.

First I include both PHP  classes (check the file paths to get it work)


$error = '';

Next I create a small upload form that can upload 3 files (add more if you need).

<form  action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
  <label for="upload[]">File 1:</label>
  <input type="file" name="upload[]" size="30">
  <label for="upload[]">File 2:</label>
  <input type="file" name="upload[]" size="30">
  <label for="upload[]">File 3:</label>
  <input type="file" name="upload[]" size="30">
  <!-- Add here more file fields if you need. -->
  <input type="submit" name="Submit" value="Submit">

Note the the “name” attribute for the file elements: “upload[]”, these square brackets take care about that your can access the uploaded files as an array in your PHP code.

If I submit the form this code need to be executed (I placed the code above the HTML part)

if(isset($_POST['Submit'], $_FILES['upload'])) {
	$my_mail = new attach_mailer('MyName', '[email protected]', '[email protected]', '', '', 'test mail multi att.');
	$my_mail->text_body = 'Hello World';
	$num_files = count($_FILES['upload']['name']);
	for ($i = 0; $i < $num_files; $i++) {
		if ($_FILES['upload']['name'][$i] != '') {

			$my_upload = new file_upload;
			$my_upload->upload_dir = $_SERVER['DOCUMENT_ROOT']."/files/multi/";
			$my_upload->extensions = array(".png", ".jpg", ".gif");
			$my_upload->the_temp_file = $_FILES['upload']['tmp_name'][$i];
			$my_upload->the_file = $_FILES['upload']['name'][$i];
			$my_upload->http_error = $_FILES['upload']['error'][$i];
			$my_upload->replace = "n";
			if ($my_upload->upload()) {
				$full_path = $my_upload->upload_dir.$my_upload->file_copy;
				$error .= 'File "'.$my_upload->file_copy.'" uploaded';
			} else {
				$error = 'Error uploading file(s)';
	if ($my_mail->process_mail()) {
		$error .= 'Mail send!';
	} else {
		$error .= 'Error sending mail';

First I test if there is an array of files and than we count the number of files first, because I use this number inside the loop. I use the “for” loop to test each file that it really exists before I add the uploaded file as an email attachment to the mail object I’ve created before.
After all file uploads are processed I’m ready to send the email message.

Note, the tutorial is a little bit quick and dirty and should work on a Linux web server (I did a test on my local Ubuntu box). The e-mail class used in this tutorial is using the native PHP “mail()” function. The complete example file (multimail.php) is included in the recent version of the attachment mailer class.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.