Jump to content

Email with multiple file attachments


DarkPrince2005

Recommended Posts

I hope someone can help me urgently...

 

I've got a script which enables a user to upload 4 images, and after being submitted validates the form and should then email the form contents to an email address.

 

Upload.php

<?php
if(isset($_POST['Submit'])) 
{
if (preg_replace("/[^A-Z]/", "", $_POST["sub"]) == "M") {

   $err = "";
   $msg = "";

   $cell_number   = preg_replace("/[^0-9]/", "", $_POST["cellno"]);
   if (strlen($cell_number) == 10) {
      if (substr($cell_number, 0, 1) == "0") {
         $cell_number = "27" . substr($cell_number, 1);
      } else {
         $err++;
         $msg .= "Invalid Cellphone number<br>";
      }//end if
   } elseif (strlen($cell_number == 11)) {
      if (substr($cell_number, 0, 2) == "27") {
         $cell_number = "27" . substr($cell_number, 2);
      } else {
         $err++;
         $msg .= "Invalid Cellphone number<br>";
      }//end if
   } else {
      $err++;
      $msg .= "Invalid Cellphone number<br>";
   }//end if

   $t_and_c = preg_replace("/[^a-z]/", "", $_POST["chk_terms"]);
   if ($t_and_c <> "on") {
      $err++;
      $msg .= "You can't enter unless you agree to the terms and conditions<br>";
   }//end if
								//define a maxim size for the uploaded images in Kb
								define ("MAX_SIZE","100000000"); 
								//This function reads the extension of the file. It is used to determine if the file is an image by checking the extension. 
								function getExtension($str) {
								$i = strrpos($str,".");
								if (!$i) { return ""; }
								$l = strlen($str) - $i;
								$ext = substr($str,$i+1,$l);
								return $ext;
								}
								//This variable is used as a flag. The value is initialized with 0 (meaning no error found) and it will be changed to 1 if an errro occures. If the error occures the file will not be uploaded.
								$errors=0;
								//checks if the form has been submitted

								$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
								$path2= "upload/".$HTTP_POST_FILES['ufile']['name'][1];
								$path3= "upload/".$HTTP_POST_FILES['ufile']['name'][2];
								$path4= "upload/".$HTTP_POST_FILES['ufile']['name'][3];
								if(isset ($_POST['Submit'])){
										if (isset($path1) && ($path1 != "upload/")) 
												{
												//get the original name of the file from the clients machine
												$filename = stripslashes($HTTP_POST_FILES['ufile']['name'][0]);
												//get the extension of the file in a lower case format
												$extension = getExtension($filename);
												$extension = strtolower($extension);
												//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
												if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
												{
												//print error message
												$err++;
												$msg .="File 1 is an unknown extension!<br />";
												}
												else
												{
												//get the size of the image in bytes
												//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
												$size=filesize($HTTP_POST_FILES['ufile']['tmp_name'][0]);
												//compare the size with the maxim size we defined and print error if bigger
												if ($size > MAX_SIZE*2000)
												{
												$err++;
												$msg .="File 1 exceeds the size limit!<br />";
												}
												//we will give an unique name, for example the time in unix time format
												$image_name=time().'.'.$extension;
												//the new name will be containing the full path where will be stored (images folder)
												$newname="upload/".$image_name;
												//we verify if the image has been uploaded, and print error instead
												$copied = copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $newname);
												sleep(1);
												if (!$copied) 
												{
												echo '<h1>Copy unsuccessfull!</h1>';
												$errors=1;
												sleep(1);
												}}}
												//If no errors registred, print the success message				

												if (isset($path2) && ($path2 != "upload/")) 
												{
												//get the original name of the file from the clients machine
												$filename1 = stripslashes($HTTP_POST_FILES['ufile']['name'][1]);
												//get the extension of the file in a lower case format
												$extension = getExtension($filename1);
												$extension = strtolower($extension);
												//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests

												if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
												{
												//print error message
												$err++;
												$msg .="File 2 is an unknown extension!<br />";
												$err++;
												}
												else
												{
												//get the size of the image in bytes
												//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
												$size=filesize($HTTP_POST_FILES['ufile']['tmp_name'][1]);
												//compare the size with the maxim size we defined and print error if bigger
												if ($size > MAX_SIZE*2000)
												{
												echo 'File 2 exceeds the size limit!<br />';
												$err++;
												}
												//we will give an unique name, for example the time in unix time format
												$image_name=time().'.'.$extension;
												//the new name will be containing the full path where will be stored (images folder)
												$newname1="upload/".$image_name;
												//we verify if the image has been uploaded, and print error instead
												$copied1 = copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $newname1);
												sleep(1);
												if (!$copied1) 
												{
												echo '<h1>Copy unsuccessfull!</h1>';
												$errors=1;
												}}}
												//If no errors registred, print the success message



												if (isset($path3) && ($path3 != "upload/")) 
												{
												//get the original name of the file from the clients machine
												$filename2 = stripslashes($HTTP_POST_FILES['ufile']['name'][2]);
												//get the extension of the file in a lower case format
												$extension = getExtension($filename2);
												$extension = strtolower($extension);
												//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
												if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
												{
												//print error message
												$err++;
												$msg .="File 3 is an unknown extension!<br />";
												}
												else
												{
												//get the size of the image in bytes
												//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
												$size=filesize($HTTP_POST_FILES['ufile']['tmp_name'][2]);
												//compare the size with the maxim size we defined and print error if bigger
												if ($size > MAX_SIZE*2000)
												{
												$err++;
												$msg .="File 3 exceeds the size limit!<br />";
												}
												//we will give an unique name, for example the time in unix time format
												$image_name=time().'.'.$extension;
												//the new name will be containing the full path where will be stored (images folder)
												$newname2="upload/".$image_name;
												//we verify if the image has been uploaded, and print error instead
												$copied2 = copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $newname2);
												sleep(1);
												if (!$copied2) 
												{
												echo '<h1>Copy unsuccessfull!</h1>';
												$err++;
												}}}
												//If no errors registred, print the success message

												if (isset($path4) && ($path4 != "upload/")) 
												{
												//get the original name of the file from the clients machine
												$filename2 = stripslashes($HTTP_POST_FILES['ufile']['name'][3]);
												//get the extension of the file in a lower case format
												$extension = getExtension($filename2);
												$extension = strtolower($extension);
												//if it is not a known extension, we will suppose it is an error and will not upload the file, otherwize we will do more tests
												if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
												{
												//print error message
												$err++;
												$msg .="File 4 is an unknown extension!";
												}
												else
												{
												//get the size of the image in bytes
												//$_FILES['image']['tmp_name'] is the temporary filename of the file in which the uploaded file was stored on the server
												$size=filesize($HTTP_POST_FILES['ufile']['tmp_name'][3]);
												//compare the size with the maxim size we defined and print error if bigger
												if ($size > MAX_SIZE*2000)
												{
												echo '<h1>File 4 exceeds the size limit!</h1>';
												$err++;
												}
												//we will give an unique name, for example the time in unix time format
												$image_name=time().'.'.$extension;
												//the new name will be containing the full path where will be stored (images folder)
												$newname3="upload/".$image_name;
												//we verify if the image has been uploaded, and print error instead
												$copied2 = copy($HTTP_POST_FILES['ufile']['tmp_name'][3], $newname3);
												if (!$copied2) 
												{
												echo '<h1>Copy unsuccessfull!</h1>';
												$err++;
												}}}
												//If no errors registred, print the success message
												if(isset($_POST['Submit']) && ($err === 0)) 
												{
												$msg .= "File Uploaded Successfully! Try again!";
												}

								else {
								$err++;
								$msg .="There was a problem uploading<br />some of your files.";
								}}}}
?>

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>PPC Cement and The Home Channel Garden Makeover - Upload Your Garden Picture</title>
<link href="css/ppc.css" type="text/css" rel="stylesheet" />
<meta http-equiv="Cache-Control" content="max-age=200" /> 
</head>
<body>
<div id="container" align="center">
<div id="imagine"></div>

<!-- site content start -->
<div id="content">
Upload four pictures of your latest outdoor PPC Cement garden masterpiece and your PPC Cement Bag to stand in line to win.
</div>
<!-- site content end -->

<!-- form content start -->
<div id="content"><?
									if (strlen($msg) > 0) {
									   echo "<tr><td colspan=2 class='textfield-b'><font color='#FFFFFF'><b>$msg<br /></b></font></td></tr>";
									}//end if
								 ?><form action="upload.php" method="post" enctype='multipart/form-data' name="form1" id="form1">
Name:<input type="hidden" name="sub" value="M"><br /><input id="name" name="name" class="textinput" type="text" /><br />
Cell:<br /><input id="cellno" name="cellno" class="textinput" type="text" /><br /><br />

<span class="bold">Photo Upload</span><br /><br />
* Picture 1:<br />
<input id="ufile[]" name="ufile[]" class="textinput" type="file" /><br />
* Picture 2:<br />
<input id="ufile[]" name="ufile[]" class="textinput" type="file" /><br />
* Picture 3:<br />
<input id="ufile[]" name="ufile[]" class="textinput" type="file" /><br />
* Picture 4:<br />
<input id="ufile[]" name="ufile[]" class="textinput" type="file" /><br />
</div>
<!-- form content end -->

<div id="upload"><input type="submit" value="Submit" name="Submit" /></div>

<div id="terms">
Terms and Conditions Apply<br />
* Required Fields
</div>

<div id="bag-top"></div>
<div id="white">
	<div id="bag-bot"></div>
	<div id="ppc"></div>
	<div id="home"></div>

	<!-- footer start -->
	<? include("inc/footer.php"); ?>  	
	<!-- footer end -->

</div>
</div>

</body>
</html>

 

I have done it before with the following scripts, but it only had one uploaded file.

 

upload.php

<?php

# Configuration files
#
ini_alter("error_reporting", "E_ALL");
ini_alter("display_errors", "On");
ini_alter("file_upload", "On");
#-----------------------------------------------------------------------------#
require("inc/config.inc.php");
require("inc/lib.inc.php");
include("random.php");

#-----------------------------------------------------------------------------#
session_start();
#-----------------------------------------------------------------------------#
if (preg_replace("/[^A-Z]/", "", $_POST["sub"]) == "M") {

   $err = "";
   $msg = "";

   $cell_number   = preg_replace("/[^0-9]/", "", $_POST["cellno"]);
   if (strlen($cell_number) == 10) {
      if (substr($cell_number, 0, 1) == "0") {
         $cell_number = "27" . substr($cell_number, 1);
      } else {
         $err++;
         $msg .= "Invalid Cellphone number<br>";
      }//end if
   } elseif (strlen($cell_number == 11)) {
      if (substr($cell_number, 0, 2) == "27") {
         $cell_number = "27" . substr($cell_number, 2);
      } else {
         $err++;
         $msg .= "Invalid Cellphone number<br>";
      }//end if
   } else {
      $err++;
      $msg .= "Invalid Cellphone number<br>";
   }//end if

   $t_and_c = preg_replace("/[^a-z]/", "", $_POST["chk_terms"]);
   if ($t_and_c <> "on") {
      $err++;
      $msg .= "You can't enter unless you agree to the terms and conditions<br>";
   }//end if
define ("MAX_SIZE","5120000");
function getExtension($str){
$i=strrpos($str,".");
if (!$i){return "";}
$l=strlen($str)-$i;
$ext=substr($str,$i+1,$l);
return $ext;
}
   $errors=0;
   
   if(isset($_POST['submit']))
   {
   $image=$_FILES['image']['name'];
   if($image)
   {
   $filename=stripslashes($_FILES['image']['name']);
   $extension=getExtension($filename);
   $extension=strtolower($extension);
   
   If(($extension != "jpg") && ($extension != "jpeg") && ($extension != "bmp") && ($extension != "gif") && ($extension != "png"))
   {
   $msg.="Unsuppoorted file format";
   $err++;
   }
   else
   {
   $size=filesize($_FILES['image']['tmp_name']);
   
   if ($size > MAX_SIZE*20000000)
   {
   $msg.="Filesize exceeds size limit";
   $err++;
   }
   else{
   $image_name=time().'.'.$extension;
   $newname="upload/".$image_name;
   $copied=copy($_FILES['image']['tmp_name'], $newname);
   }if (!$copied)
   {
   $msg.="Upload of image unsuccessful";
   $err++;
   }}
   
   }else if (!$image)
   {
   $msg.="No File Selected";
   $err++;
   }
   
   if (isset($_POST['submit']) && !$err)
   {
   $msg.="File uploaded successfully";
   
   $dbc=mysql_connect("localhost","root","");
   mysql_select_db("revlovdv_revlon24seven");
$sql="insert into entries values(NULL,'$_POST[name]','$cell_number','$bar_code','$newname','$_POST[why]','".date("Y-m-d H:i:s")."', " . time() . ")";
mysql_query($sql, $dbc);
 $id=mysql_insert_id($dbc);

$got_entry=true;

 $file_prefix = str_pad($id, 7, "0", STR_PAD_LEFT);
                  $file_name = $file_prefix . "." . $cfg_supported_ext[$type];

$name=$_POST['name'];
$why=$_POST['why'];

send_file_via_email($newname, $_FILES['image_file']['name'], "[email protected]", $cell_number, $id, $name,$why);
   

}
}}
//end if
#-----------------------------------------------------------------------------#

#-----------------------------------------------------------------------------#
?>
<html>
<head>
<title>Revlon 24 Seven | Revlon 24 Seven Competition | Revlon 24 Seven Man</title>
<meta name="description" content="revlon, revlon 24 seven, 24 seven, revlon 24 seven competion, 24 seven competition, revlon 24 seven man, revlon man, 24 seven man, revlon promotion, revlon campaign">
<meta name="keywords" content="revlon, revlon 24 seven, revlon 24 seven man, revlon 24 seven competition, revlon 24 seven promotion, 24 seven, 24 seven man">
<link rel="STYLESHEET" type="text/css" href="css/div.css" />
<link rel="STYLESHEET" type="text/css" href="css/stylesheet.css" />
</head>
<body bgcolor="#0e0f1e" bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0">

<center><table cellpadding="0" cellspacing="0" border="0" height="100%" width="900">
<tr>
<td valign="center" align="center">
	<table cellpadding="0" cellspacing="0" border="0" height="640" width="900">
		<tr>
			<td height="640"><div class="main-body">
			<div class="bg"></div>

			<div class="border"></div>
			<div><? require("inc/nav.html");?></div>
<div class="content-window"><a href="http://www.revlon24seven.co.za/prizes.php" title="Revlon 24 Seven Prizes"><img src="images/middlewin.png" alt="Revlon 24 Seven Prizes" align="left" height="180" border="0" style="top:80px; left:20px; position:absolute;"></a></div><div class="cstripe"></div>
<div class="content1"><font color="#FFFFFF"><center><br /><?
						   if (!$got_entry && intval($_SESSION["entry_no"]) < $cfg_session_entries) {
						?>
<form action="<? echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data" name="frm_smile">
							  
							  <table align="right" width="275" cellpadding="1" cellspacing="0" class="entry-form">
								 <tr>
									<td colspan="2" align="left" valign="top" class="text">
									To upload your picture, please complete the following details:<input type="hidden" name="sub" value="M"></td>
								 </tr>
								 <tr>
									<td align="left" valign="middle" class="text">* Name:</td>
									<td align="left" valign="top" class="info-field"><input type="text" name="name" class="textfield-b" value="<? echo $_POST["name"]; ?>"></td>
								 </tr>
								 <tr>
									<td align="left" valign="middle" class="text">* Cellphone:</td>
									<td align="left" valign="top" class="info-field"><input type="text" name="cellno" class="textfield-b" value="<? echo $_POST["cellno"]; ?>"></td>
								 </tr>
								 <?
									if (strlen($msg) > 0) {
									   echo "<tr><td colspan=2 class='textfield-b'><font color='#FF0000'><b>$msg</b></font></td></tr>";
									}//end if
								 ?>
								 <tr>
									<td align="left" valign="middle" class="text">* Upload photo:</td><td><input type="file" name="image" size="20" id="file" class="textfield-c"></td>
								</tr>

								 <tr>
									<td colspan="2" align="left" valign="middle" class="text"><center>Tell us why you should be the<br /><strong><font color="#ee1654">24SEVEN Man</font></strong></center></td>
								</tr>
								<tr>	
									<td colspan="2" align="center" valign="top" class="info-field"><textarea cols="45" rows="5" name="why" class="textfield-c" value="<? echo $_POST["why"]; ?>"></textarea></td>
								 </tr>
								 <tr>
									<td colspan="2" align="center" valign="middle" scope="row" class="text">
									   <input type="checkbox" name="chk_terms">
									   I agree to the <a href="terms.php" title="Terms & Conditions">Terms and Conditions</a>
									</td>
								 </tr>
								 <tr>
									<td colspan="2" align="center" valign="top" scope="row"><input type="hidden" name="submit" value="true"><input type="image" value="submit" src="images/submit.png" id="submit"></td>
								 </tr>
							  </table>
		    </form>
						<?
						   } elseif ($got_entry) {
						?>
							  <table align="right" valign="center" height="280" width="275" cellpadding="0" cellspacing="0" class="entry-form">
								 <tr>
									<td align="center">Thank you, your entry has been captured.
									</td>
								 </tr>
							  </table>
						<?
						   } elseif (intval($_SESSION["entry_no"]) >= $cfg_session_entries) {
						?>
							  <table width="200" cellpadding="0" cellspacing="0" class="entry-form">
								 <tr>
									<td>
									   <p align=ceter> </p>
									   <p align=ceter> </p>
									   <p align=ceter> </p>
									   <p align=center class="thankyou">Please try again later.</p>
									</td>
								 </tr>
							  </table>
						<?
						   }//end if
						?></center>
<br><br>
</font>
						</div>
			<div class="24logo"><img src="images/24logo.png" style="left:5px; top:10px; position:absolute;"></div>
			<div class="stripe"></div>
			<div class="man"></div>
			<div class="footer"></div>
			<div class="testimonials"><? randomQuote("testimonials.txt"); ?></div>
			<div class="copyright"></div>
			<div class="blinks" align="center"><? require("inc/bottom-nav.html");?></div>
			</div>			
			</td>
		</tr>
	</table>
</td>
</tr>
</table>
</body>
</html>

 

Function

function send_file_via_email($file_path, $file_name, $to, $cell, $id, $name, $why) {
   global $from;

   $subject = "WEB ENTRY: $cell - Entry ID: $id";

   $fileatt = $file_path;
   $fileatttype = mime_content_type($fileatt);
   $fileattname = $file_name;

   $headers = "From: $from";

   $file = fopen($fileatt, 'rb');
   $data = fread($file, filesize($fileatt));
   fclose($file);

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

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

   $message  = "This is a multi-part message in MIME format.\n\n";
   $message .= "--{$mime_boundary}\n";
   $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
   $message .= "Content-Transfer-Encoding: 7bit\n\n";
   $message .= "Web entry received.\n\n";
   $message .= "Name: $name\n";
   $message .= "Cell Number: $cell\n";
   $message .= "Why: $why\n";
   $message .= "Entry ID: $id\n";
   $message .= "\n\n";

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

   $message .= "--{$mime_boundary}\n";
   $message .= "Content-Type: {$fileatttype};\n";
   $message .= " name=\"{$fileattname}\"\n";
   $message .= "Content-Disposition: attachment;\n";
   $message .= " filename=\"{$fileattname}\"\n";
   $message .= "Content-Transfer-Encoding: base64\n\n";
   $message .= $data . "\n\n";
   $message .= "--{$mime_boundary}--\n";

#echo "<pre>EMAIL\n\n$to, $subject, $message, $headers</pre>";

   if (mail($to, $subject, $message, $headers)) {
      #
      # Ok
      #      
   } else {
      echo "Error sending email.";
   }//end if

}//end function

 

Link to comment
https://forums.phpfreaks.com/topic/179824-email-with-multiple-file-attachments/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.