• PHP Help
  • Problem with Detailed Application Form with Attachment using PHPMailer

Hi everyone, first of all thanks for letting me join this Forum i look forward to being part of the group.
I have compiled a detailed application form for a website which uses normal text boxes, drop down option boxes, Calendar for dates, check boxes and attachment. I got the form to email beautifully using just normal PHP but i got stuck when dealing with the attachment section. I then learnt its best to use PHP Mailer so i have re done the PHP for this but getting the dreaded HTTP 500 error. Please could someone check out my code to see where i am going wrong please. Thank you very much in advance.

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'href="/PHPMailer/src/Exception.php"';
require 'href="/PHPMailer/src/PHPMailer.php"';

$message = '';

function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}

if(isset($POST["submit"]))
{
$path = 'href="/upload/"' . $
FILES["attachment"]["name"];
move_uploaded_file($FILES["attachment"]["tmp_name"], $path);
$message = '
<h3 align="center">Application Details</h3>
<table border="1" width="100%" cellpadding="5" cellspacing="5">
<tr>
<td width="30%">First Name</td>
<td width="70%">'.$
POST["first_name"].'</td>
</tr>
<tr>
<td width="30%">Last Name</td>
<td width="70%">'.$POST["last_name"].'</td>
</tr>
<tr>
<td width="30%">Email Address</td>
<td width="70%">'.$
POST["email"].'</td>
</tr>
<tr>
<td width="30%">Phone Number</td>
<td width="70%">'.$POST["phone_number"].'</td>
</tr>
<tr>
<td width="30%">Position</td>
<td width="70%">'.$
POST["position"].'</td>
</tr>
<tr>
<td width="30%">WhatsApp Number</td>
<td width="70%">'.$POST["whatsapp_no"].'</td>
</tr>
<tr>
<td width="30%">Yacht Name</td>
<td width="70%">'.$
POST["yacht_name"].'</td>
<tr>
<td width="30%">Yacht Type</td>
<td width="70%">'.$POST["yacht_type"].'</td>
</tr>
<tr>
<td width="30%">Yacht Size</td>
<td width="70%">'.$
POST["yacht_size"].'</td>
</tr>
<tr>
<td width="30%">Cruising Type</td>
<td width="70%">'.$POST["cruising_type"].'</td>
</tr>
<tr>
<td width="30%">Yacht Location</td>
<td width="70%">'.$
POST["yacht_location"].'</td>
</tr>
<tr>
<td width="30%">Yacht Itinenary</td>
<td width="70%">'.$POST["yacht_itinenary"].'</td>
</tr>
<tr>
<td width="30%">Work Posistion</td>
<td width="70%">'.$
POST["work_position"].'</td>
</tr>
<tr>
<td width="30%">Work Type</td>
<td width="70%">'.$POST["work_type"].'</td>
</tr>
<tr>
<td width="30%">Start Date</td>
<td width="70%">'.$
POST["start_date"].'</td>
</tr>
<tr>
<td width="30%">Finish Date</td>
<td width="70%">'.$POST["finishing_date"].'</td>
<tr>
<td width="30%">Visa Required</td>
<td width="70%">'.$
POST["visa_req"].'</td>
</tr>
<tr>
<td width="30%">Qualifications Required</td>
<td width="70%">'.$POST["qual_req"].'</td>
</tr>
<tr>
<td width="30%">Job Description</td>
<td width="70%">'.$
POST["job_desc"].'</td>
</tr>
<tr>
<td width="30%">Shared Cabin</td>
<td width="70%">'.$POST["shared_cabin"].'</td>
</tr>
<tr>
<td width="30%">Understood Terms</td>
<td width="70%">'.$
POST["terms"].'</td>
</tr>
<tr>
<td width="30%">Confidental</td>
<td width="70%">'.$POST["confidential"].'</td>
</tr>
</table>
';
if (isset($
POST['shared_cabin'])){
$POST['shared_cabin']; // Displays value of checked checkbox.
}
if (isset($
POST['terms'])){
$POST['terms']; // Displays value of checked checkbox.
}
if (isset($
POST['confidential'])){
$_POST['confidential']; // Displays value of checked checkbox.
}

require 'class/class.phpmailer.php';
$mail = new PHPMailer(TRUE);
$mail->From = $POST["email_from"]; //Sets the From email address for the message
$mail->FromName = $
POST["first_name"]["last_name"]; //Sets the From name of the message
$mail->AddAddress('timmy2872@gmail.com'); //Adds a "To" address
$mail->WordWrap = 50; //Sets word wrapping on the body of the message to a given number of characters
$mail->IsHTML(true); //Sets message type to HTML
$mail->AddAttachment($path); //Adds an attachment from a path on the filesystem
$mail->Subject = 'Employer Application Form details'; //Sets the Subject of the message
$mail->Body = $message; //An HTML or plain text message body
if($mail->Send()) //Send an Email. Return true on success or false on error
{
$message = '<div class="alert alert-success">Application Successfully Submitted</div>';
unlink($path);
}
else
{
$message = '<div class="alert alert-danger">There is an Error</div>';
}
}

?>

    Your code lints Just Fine(tm) ... what error message are you receiving?

      On 2nd glance, those first two require() statements look funky.

      Try changing this:

      require 'href="/PHPMailer/src/Exception.php"';
      require 'href="/PHPMailer/src/PHPMailer.php"';

      to this:

      require "actual_path_to_php_mailer/src/Exception.php";
      require "actual_path_to_php_mailer/src/PHPMailer.php";

      Where "actual_path_to_php_mailer" is, y'know, an actual path. πŸ™‚

      Thanks dalescop for replying i changed those 2 lines of code as you suggested, the actual path on my server is -
      public_html/temp/PHPMailer/src/

      Still this error comes up -
      This page isn’t working www.************.com is currently unable to handle this request.
      HTTP ERROR 500

      The only 2 lines that look weird to me are the very top ones -
      use PHPMailer\PHPMailer\PHPMailer;
      use PHPMailer\PHPMailer\Exception;

      The "use" is not in a different color as if its entered wrong?

      The use keyword assigns a namespace to the current script.

      You really need to figure out how to get at the real error message. All server-side issues are 500-class errors.

      If you have access to the logfiles on the server, try to find out what's going on there. You'd be looking for something like:

      [05-Jul-2019 12:24:03 America/Chicago] PHP Fatal error: Uncaught Error: Call to a member function query() on null in /www/data/fake.com/some_script:18

      Now, the error isn't "Call to a member function" in your case, mostly likely ... but whatever it is gives you the clue you need to solve the issue. Not only do you see the cause of the error, but also the file ("some_script") and the line number (18) that the parser encountered the error on.

      I still kind of lean towards some kind of scope resolution problem in your case ...

      If you have absolutely no access to the server's log files .. try putting this at the top of your script:

      ini_set("display_errors", TRUE);

      And see if you get the error message shown in your browser or the "view source" from your browser when the page is loaded.

      Just be sure and take this line OUT once you get it fixed ... you don't want every Tom, Dick & Harry on the WWW to know what your site's like .... they aren't all as nice as us folks on PHPBuilder. πŸ˜‰

      FYI: namespaces require at least PHP 5.3.0. One would hope nobody is running anything even older than that, but just in case... πŸ™‚

      dalecosp If you have absolutely no access to the server's log files .. try putting this at the top of your script:

      ini_set("display_errors", TRUE);

      You might also need to set the error-reporting level, e.g.:

      <?php
      ini_set('display_errors', true); // change to false in production
      error_reporting(E_ALL);
      

      When using include or require statements, you have to keep in mind that absolute paths do not depend on the current working directory and relative paths might be interpreted differently depending on what the current working directory is. Consider reviewing the concept of absolute vs relative paths.

      The concept of the current working directory is also something you should be familiar with. Basically, whenever a PHP script runs, the system assumes some file system location is where the script is operating. For apache or other web server scripts, this is usually the location of the file being executed. If you are executing a PHP script from the command line, it's usually the current working directory of the process that invoked the PHP script. You can ask PHP what its current working directory is with the command getcwd.

      All fixed and working like a dream now. dalescop you put me on the right line with those 2 lines of code.
      Problem was, the site i,m working on is in a "Temp" folder as not to upset the old site. I had the PHPMailer and Upload folder in the sub directory and not the main directory of the server so this is how it needed to be -

      require "/home/awolpm/PHPMailer/src/Exception.php";
      require "/home/awolpm/PHPMailer/src/PHPMailer.php";

      Also this line was not needed -
      require 'class/class.phpmailer.php';

      The Upload folder needed to be -
      $path = '/home/awolpm/upload/"' . $FILES["attachment"]["name"];
      move_uploaded_file($
      FILES["attachment"]["tmp_name"], $path);

      Thank you to dalescop and everyone who helped me sort this problem. Certainly made me scratch my head for a while!

      One last thing to do for it - make a pop up modal box on success of sending and return a confirmation email to the sender.

        NogDog hope nobody is running anything even older than that, but just in case...

        We had one box on 5.2.17 that only moved ahead less than six months ago ... believe me there was a good bit of pain porting that one ...

          Write a Reply...