0

I want to allow the user to attach multiple files in my form and those file come to me as an attachment in an email. But I want to keep this as optional i.e. if the user attaches the file, the files come as an attachment and if the user does not attach files, the rest of the form comes in the email.

Can somebody help me with this?

I am new to php so following is my php file.

<?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'])) {
$mail->AddAttachment($_FILES['fileToUpload']['tmp_name'],
$_FILES['fileToUpload']['name']);
}
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}

?>
Muthu Kumaran
  • 16,579
  • 5
  • 43
  • 69
Saxena
  • 39
  • 5

3 Answers3

1

Thanks to all you experts especially Rajapandian, Joel, NogDog etc. who have helped me a lot. Finally my problem is solved. Now the file attachment is optional for user. If he attaches files, the form comes with the attachment and if he does not attaches files, the form comes in mail without attachment. I am sharing this script with everybody. Here is the php. Once again thanks to all.

if (!empty($_FILES['fileToUpload']['tmp_name']) &&  !empty($_FILES['fileToUpload']['name'])) {
        foreach ($_FILES['fileToUpload']['tmp_name'] as $key => $value) {
            // added this IF block:
            if (file_exists($_FILES['fileToUpload']['tmp_name'][$key])) {
                $mail->AddAttachment($_FILES['fileToUpload']['tmp_name'][$key], $_FILES['fileToUpload']['name'][$key]);
            }
        }
    }  
Saxena
  • 39
  • 5
  • Wow, this was a serious lifesaver this bit of code. I was in the exact same predicament and couldn't find a solution. Thanks, @Saxena :-) – michaelmcgurk Apr 26 '19 at 13:27
0

If the user has not selected a file attachment you can check it via the size or temp name:

If no file is selected for upload in your form, PHP will return $_FILES['userfile']['size'] as 0, and $_FILES['userfile']['tmp_name'] as none.

from http://php.net/manual/en/features.file-upload.post-method.php

and not call $mail->AddAttachment, but from your code you're already doing that so I'm not sure if I understand your question or you may need to explain it a little more.

xander
  • 1,640
  • 5
  • 14
  • actually I want to allow user to attach multiple files but want to make this optional. That means that if the user attaches files the files come to me as attachment in mail and if the user does not attach files the rest of the form should come in email. In my present code it is only for one attachment and is also optional. I want to make the same for multiple attachments. – Saxena Aug 14 '17 at 11:17
0

First of all, your form should look like something like this:

<form method="POST" enctype="multipart/form-data">
    <input type="file" name="fileToUpload[]" id="fileToUpload[]" multiple>
    <input type="submit" value="upload version" />
</form>

Then you can use PHP to then loop through the uploaded files.

if(isset($_FILES['fileToUpload']){
  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]);
  }
}

Of course you need to apply the same foreach, for all the checks that you are doing on the uploaded file.

Joel'-'
  • 356
  • 1
  • 11
  • Thanks Joel for your help. I tested the script but if the user does not attach any file it is not submitting the form and giving an error "Could not access file:". Please help as I want to make attachment as optional. – Saxena Aug 14 '17 at 10:33
  • You can check that by checking if the file variable is set. – Joel'-' Aug 14 '17 at 11:26
  • As I already said, I am totally new to php. Could you please help me by giving me the new updated script. I will be obliged. The script you gave me earlier works perfectly if the use attaches the files. – Saxena Aug 14 '17 at 11:49
  • I added an if above it, it was already updated in the code :) – Joel'-' Aug 14 '17 at 12:02
  • Thanks Joel. I tried the revised script but it is not working. After submit, It is giving me an error "This Page Isn't Working HTTP Error 500". Pl. help. – Saxena Aug 16 '17 at 04:43
  • I think for development you should change your error reporting settings, to see what the errors actually say. If your webserver runs apache, this is a very useful [link](https://stackoverflow.com/questions/6127980/enabling-error-display-in-php-via-htaccess-only) – Joel'-' Aug 17 '17 at 06:22
  • Thanks Joel. You have helped me a lot. But my problem still remains. I tried everything what you and other experts suggested but still if the user does not attach files, the form is not submitted. It gives error "cannot access file". I really don't know what to do. – Saxena Aug 17 '17 at 07:10
  • Extremely thankful Joel. I am learning a lot from expert like you. I am a novice but hopefully with teachers like you, I may also become expert. I will try this and revert. – Saxena Aug 18 '17 at 04:16
  • I tested your script but it is still giving me the same error "This page isn't working design maniac cannot handle this site. http error 500" – Saxena Aug 18 '17 at 05:14
  • Then you must really enable the error display on the webserver. – Joel'-' Aug 21 '17 at 14:44
  • Thanks Joel. I am using wordpress. Could you please tell me how to enable error display. I checked .htaccess but could not find anything. I don't have access to php.ini file. – Saxena Aug 22 '17 at 06:18