Jump to content

Email Form Multiple Attachments


amin1982

Recommended Posts

Hey Guys,

 

Some help needed please.

 

I have a form that requires to attachments to be sent. I have a PHP code that can handle one attachment but I seem unable to adapt it to send a second attachment or even third.

 

My form is

 

<form action="formsubmit.php" method="post" enctype="multipart/form-data" name="myform" onSubmit="return validateForm()">
<p>
<input type="hidden" value="[email protected]" name="to">
<input type="hidden" value="Application" name="subject">
</p>
<p>
<label for="name">*Name:</label>
<input type="text" name="name" class="txt" value="" />
</p>
<p>
<label for="age">*Age:</label>
<input type="text" name="age" class="txt" value="" />
</p>
<p>
<label for="Birthday">*Date of Birth(dd/mm/yyyy):</label>
<input type="text" name="birthday" id="birthday" class="txt" />
</p>
<p>
<label for="email">*Email:</label>
<input type="text" name="email" id="email" class="txt" />
</p>
<p>
<label for="mobile">*Mobile:</label>
<input name="mobile" type="text" class="txt" id="mobile" maxlength="11" />
</p>
<p>
<label for="label">*Address</label>
<textarea name="address" cols="28" rows="5" class="txt" id="address"></textarea>
</p>
<p>
<label for="ethnicity">*Ethnicity:</label>
<input name="ethnicity" type="text" class="txt" id="ethnicity"/>
</p
><p>
<label for="occupation">*Occupation:</label>
<input name="occupation" type="text" class="txt" id="occupation"/>
</p
><p>
<label for="passport">*Do you hold a Valid UK Passport?</label>
<select name="passport" class="txt">
  <option value="">Please Choose</option>
  <option value="Yes">Yes</option>
  <option value="No">No</option>
</select>
</p
>
<p>
<label for="hips">*Hips:</label>
<input name="hips" type="text" class="txt" id="hips" />
</p>
<p>
<label for="dress">*Dress Size:</label>
<input name="dress" type="text" class="txt" id="dress" />
</p>
<p>
<label for="bust">*Bust Size:</label>
<input name="bust" type="text" class="txt" id="bust" />
</p>
<p>
<label for="shoe">*Shoe Size:</label>
<input name="shoe" type="text" class="txt" id="shoe" />
</p>
<p><label for="picture">*Your Picture:</label>
<input type="file" name="fileatt" /></p>
<p><label for="picture">*Your Picture:</label>
<input type="file" name="fileatt2" /></p>
<p>
<label for="experience">*Relevant Experience:</label>
<textarea name="experience" cols="28" rows="5" class="txt" id="experience"></textarea>
</p>

<p>
<label for="why">*Why?<br />
</label>
<textarea name="why" cols="28" rows="5" class="txt" id="why"></textarea>
</p>
<p>
<input type="submit" name="btnSubmit" id="btnSubmit" value="Apply" class="btn" />
</p>
</form>

 

The PHP Code is:

 

 


<?php
// Read POST request params into global vars
$to      = $_POST['to'];
$email   = $_POST['email'];
$subject = $_POST['subject'];
$age   = $_POST['age'];
$birthday = $_POST['birthday'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
$ethnicity = $_POST['ethnicity'];
$occupation = $_POST['occupation'];
$passport = $_POST['passport'];
$hips = $_POST['hips'];
$dress = $_POST['dress'];
$bust = $_POST['bust'];
$shoe = $_POST['shoe'];
$experience = $_POST['experience'];
$why = $_POST['why'];


// Obtain file upload vars
$fileatt      = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];



$headers = "From: $email";

if (is_uploaded_file($fileatt)) {
  // Read the file to be attached ('rb' = read binary)
  $file = fopen($fileatt,'rb');
  $data = fread($file,filesize($fileatt));
  fclose($file);

  // 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" .
             "Age: " . $age . "\n\n" .
             "Birthday: " . $birthday . "\n\n" .
             "Mobile: " . $mobile . "\n\n" .
             "Email: " . $email . "\n\n" .
             "Address: " . $address . "\n\n" .
             "Ethnicity: " . $ethnicity . "\n\n" .
             "Occupation: " . $occupation . "\n\n" .
             "Passport: " . $passport. "\n\n" .
             "Hip Size: " . $hips. "\n\n" .
             "Dress Size: " . $dress . "\n\n" .
             "Bust Size: " . $bust . "\n\n" .
             "Shoe: " . $shoe . "\n\n" .
             "Experience: " . $experience . "\n\n" .
             "Why: " . $why . "\n\n";
            

  // Base64 encode the file data
  $data = chunk_split(base64_encode($data));

  // Add file attachment to the message
  $message .= "--{$mime_boundary}\n" .
              "Content-Type: {$fileatt_type};\n" .
              " name=\"{$fileatt_name}\"\n" .
              //"Content-Disposition: attachment;\n" .
              //" filename=\"{$fileatt_name}\"\n" .
              "Content-Transfer-Encoding: base64\n\n" .
              $data . "\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</p>";
}
?>

 

Please help. I have tried repeating some of the steps but no luck!

Link to comment
https://forums.phpfreaks.com/topic/129490-email-form-multiple-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.