0

I am really sorry I am bothering you a lot but as I am totally new to php, I am relying on all of you experts. I want to allow user to attach multiple file to my form but I want to keep it optional. That means if the user attaches the file, the files come as an attachment along with the form and if the user does not attach files, the rest of the form comes in the email. With the help of all you people especially Rajapandian, Joel, I was able to create my php but in this the form attachment is compulsory not optional. Please somebody help me in making this attachment optional. I am using phpmailer. I will be highly obliged. Here is my php.

<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

$to = "saxena@designmaniac.in";
$mail->AddAddress($to);
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name_f'];
$mail->Subject = "Admission Form";

$body = "<table>
<tr>
<th colspan='2'>Admission Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Course :</td>
<td>".$_POST['course']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Payment Mode :</td>
<td>".$_POST['p_mode']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Payment options :</td>
<td>".$_POST['p_option']."</td>
</tr>

<tr>
<td style='font-weight:bold'>First Name :</td>
<td>".$_POST['name_f']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Middle Name : </td>
<td>".$_POST['name_m']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Last Name : </td>
<td>".$_POST['name_l']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-mail : </td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Date of Birth : </td>
<td>".$_POST['date']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Gender : </td>
<td>".$_POST['gender']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Nationality : </td>
<td>".$_POST['nationality']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Address : </td>
<td>".$_POST['address_line1']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City : </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Zip Code : </td>
<td>".$_POST['zip']."</td>
</tr>

<tr>
<td style='font-weight:bold'>State : </td>
<td>".$_POST['state']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Country : </td>
<td>".$_POST['country']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile Number : </td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother First Name : </td>
<td>".$_POST['fname_f']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother Middle Name : </td>
<td>".$_POST['fname_m']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother Last Name : </td>
<td>".$_POST['fname_l']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Father/Mother Mobile No : </td>
<td>".$_POST['mobile_number1']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Qualification : </td>
<td>".$_POST['qualification']."</td>
</tr>

<tr>
<td style='font-weight:bold'>University : </td>
<td>".$_POST['university']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Year : </td>
<td>".$_POST['year']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Qualification : </td>
<td>".$_POST['qualification_2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>University : </td>
<td>".$_POST['university_2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Year : </td>
<td>".$_POST['year_2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Qualification : </td>
<td>".$_POST['qualification_3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>University : </td>
<td>".$_POST['university_3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Year : </td>
<td>".$_POST['year_3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Discipline Information (School) : </td>
<td>".$_POST['discipline']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Discipline Information (Violation) : </td>
<td>".$_POST['violation']."</td>
</tr>

<tr>
<td style='font-weight:bold'>12th Mark List : </td>
<td>".$_POST['attachment1']."</td>
</tr>

<tr>
<td style='font-weight:bold'>12th Passing Certificate : </td>
<td>".$_POST['attachment2']."</td>
</tr>

<tr>
<td style='font-weight:bold'>10th Mark List : </td>
<td>".$_POST['attachment3']."</td>
</tr>

<tr>
<td style='font-weight:bold'>10th Passing Certificate : </td>
<td>".$_POST['attachment4']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Address Proof : </td>
<td>".$_POST['attachment8']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Photographs : </td>
<td>".$_POST['attachment9']."</td>
</tr>

<table>";
$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
//$mail->Host = "saxena@designmaniac.in"; // SMTP server
//$mail->Username = "name@domain.com"; // SMTP server username
//$mail->Password = "password"; // SMTP server password

$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("saxena@designmaniac.in");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "pdf" && $imageFileType != "docx"
&& $imageFileType != "doc" ) {
    echo "Sorry, only JPG, JPEG, PNG, GIF, pdf & Word files are allowed.";
    $uploadOk = 0;
}

if(!empty($_FILES['fileToUpload']['tmp_name']) &&  !empty($_FILES['fileToUpload']['name'])) {
  foreach($_FILES['fileToUpload']['tmp_name'] as $key => $value)
  $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'][$key], $_FILES['fileToUpload']['name'][$key]);
}
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}

?>

And the HTML is like this.

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="fileToUpload[]" id="fileToUpload[]" multiple="multiple">
    <input type="submit" value="submit">
</form>
Saxena
  • 39
  • 5
  • 2
    Possible duplicate of [Allow user to attach multiple files but optional](https://stackoverflow.com/questions/45671067/allow-user-to-attach-multiple-files-but-optional) –  Aug 17 '17 at 04:32
  • what currently happens when a file is included in the form upload? and when it is not. You dont tell us in what why it is not working –  Aug 17 '17 at 04:38
  • If user does not attach files, the form is not submitted and it gives an error "File not found" and if the user attaches file the form is perfectly submitted with the attachment. – Saxena Aug 17 '17 at 05:07
  • you need to chcekif a file was uploaded and wrap those functions. –  Aug 17 '17 at 05:26
  • Thanks rtfm. But I am totally new to php. can you please tell me how to do that. Please tel me the script for this. – Saxena Aug 17 '17 at 05:29
  • do you want to learn, or just expect other to do it for you ? –  Aug 17 '17 at 05:30
  • Actually I want to learn and I am doing the same at my end also but for me it is a bit confusing. – Saxena Aug 17 '17 at 05:32

0 Answers0