-1

I am responsible for a job application form that does not seem to be working well. The problem is that emails from this form are not always receiving. The weird fact is that I also configured a CC email address in this script (another server) and that email address is always receiving mail in that inbox. Personally I think this is a server problem but maybe I am missing something.

UPDATE: Another website is running on that same server with a contact form that appears to be working fine.

See my code below.

Any help would be greatly appreciated.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="nl">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>title</title>
  <meta name="description" content="" />
  <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.9/css/all.css" integrity="sha384-5SOiIsAziJl6AWe0HWRKTXlfcSHKmYV4RBF18PPJ173Kzn7jzMyFuTtk8JA7QQG1" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css" crossorigin="anonymous">
  <!--[if IE 9]><link rel="stylesheet" href="ie.css" type="text/css" /><![endif]-->
  <!--[if IE 8]><link rel="stylesheet" href="ie.css" type="text/css" /><![endif]-->
  <!--[if IE 7]><link rel="stylesheet" href="ie.css" type="text/css" /><![endif]-->
  <script src='https://www.google.com/recaptcha/api.js'></script>


</head>



<body>


  <div class="container">


    <div class="content">

      <div style="display:flex; align-items:flex-start; justify-content:flex-start!important; padding:20px 0px 20px 0px; height:100%; flex-wrap:wrap;" class="wrapper">

        <div class="block padding-left">

          <div class="padding-mobile" style="width:100%; height:auto; display:flex; align-items:flex-start; justify-content:flex-start; margin-top:60px; flex-wrap:wrap; box-sizing:border-box;">

            <h2 style="font-size:42px; margin:0px;">Apply</h2>

            <form enctype="multipart/form-data" id="cv" class="formulier" method="POST" action="">

              <p>
                <label>First name:</label>
                <input data-validation="required" type="text" name="naam" placeholder="Uw naam" />
              </p>

              <p>
                <label>Last name:</label>
                <input data-validation="required" type="text" name="achternaam" placeholder="Uw achternaam" />
              </p>

              <p>
                <label>Telephone</label>
                <input data-validation="required" type="text" name="telefoon" placeholder="Uw telefoonnummer" />
              </p>

              <p>
                <label>Email address</label>
                <input data-validation="required email" type="text" name="email" placeholder="Uw email adres" />
              </p>

              <p>
                <label>Select a job application</label>
                <select name="vacature" placeholder="Vacature" data-validation="length" data-validation-length="min1" data-validation-error-msg="Selecteer een vacature">
                  <option value="" disable selected>Vacature</option>
                  <option>- Open sollicitatie- </option>
                  <option>Administratief medewerker bedrijfskantoor (m/v)</option>
                  <option>Inkoper</option>
                  <option>Leerling Operators</option>
                  <option>Snijder (m/v)</option>
                  <option>Planner</option>
                  <option>Accountmanager Frankrijk</option>
                  <option>Offsetdrukker Vellen</option>
                  <option>Oproepkrachten</option>
                  <option>Weekend-/ vakantiewerkers</option>
                  <option>Assistent Machineoperator (m/v)</option>
                  <option>Offsetrotatiedrukkers Heatset (m/v)</option>
                  <option>Software developer</option>
                  <option>Allround onderhoudsmonteur</option>
                  <option>Magazijnmedewerker (m/v)</option>
                </select>

              </p>

              <p style="background:#fff; padding:20px; margin-top:44px; color:#000; border-radius:6px;">
                <label style="color:#555; text-shadow:none!important; font-size:18px;">Upload your CV</label>
                <input type="file" name="cv" data-validation="mime size" data-validation-allowing="pdf, docx, doc, docm, rtf" data-validation-max-size="2M" />
              </p>

              <p align="center">Please vrify that you are human.</p>

              <div class="g-recaptcha" data-sitekey="---sitekey----" style="margin-bottom:20px;" align="center"></div>

              <input type="submit" value="Verzend mijn sollicitatie!" />

            </form>

            <script>
              $("form#cv")
                .attr("enctype", "multipart/form-data")
                .attr("encoding", "multipart/form-data");
            </script>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.3.26/jquery.form-validator.min.js"></script>
            <script>
              $.validate({
                modules: 'file',
                lang: 'nl'
              });
            </script>



          </div>


        </div>

        <div class="block">

          <div class="circle" style="border-radius:600px; border:solid 10px #fff; margin-top:60px; background:url(wrapper.png); background-size:160% auto!important; background-position:-150px 0!important; box-sizing:border-box;"></div>

        </div>

        <div class="clear"></div>


        <div class="block padding-left inline-block">

          <div style="text-align:center; display:flex; align-items:center; justify-content:center; width:90%;">
            <?php 
     if(isset($_POST['email'])) {
     
     if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo '<div class="notific" style="background:rgba(192, 0, 0, 0.8); display:flex; align-items:center; justify-content:center; border-radius:6px; margin-top:20px; padding:0px 15px 0px 15px;"><i style="margin:0px 10px 0px 10px; color:#fff; font-size:32px; text-shadow: 1.2px 1.2px 1.2px #000;" class="fas fa-exclamation-triangle"></i><h2 style="font-size:28px!important;">Bevestig dat u een mens bent.</h2></div>';
  ?>

            <script>
              $('html, body').animate({
                scrollTop: $("div.notific").offset().top
              }, 1000)
            </script>

            <?php
          exit;
        }
     
  $secretKey = "--secretkey--";
        $ip = $_SERVER['REMOTE_ADDR'];
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
        $responseKeys = json_decode($response,true);
        if(intval($responseKeys["success"]) !== 1) {
          echo '<div class="notific" style="background:rgba(192, 0, 0, 0.8); display:flex; align-items:center; justify-content:center; border-radius:6px; margin-top:20px; padding:0px 15px 0px 15px;"><i style="margin:0px 10px 0px 10px; color:#fff; font-size:32px; text-shadow: 1.2px 1.2px 1.2px #000;" class="fas fa-exclamation-triangle"></i><h2 style="font-size:28px!important;">Er ging iets mis, probeer het nog eens.</h2></div>';
   
  ?>

              <script>
                $('html, body').animate({
                  scrollTop: $("div.notific").offset().top
                }, 1000)
              </script>

              <?php
   
        } else {

          echo '<div class="notific" style="background:rgba(0, 226, 37, 0.8); display:flex; align-items:center; justify-content:center; border-radius:6px; margin-top:20px; padding:0px 15px 0px 15px;"><i style="margin:0px 10px 0px 10px; color:#fff; font-size:32px; text-shadow: 1.2px 1.2px 1.2px #000;" class="fas fa-check-square"></i><h2 style="font-size:28px!important;">Bedankt voor uw aanmelding.</h2></div>';
   
  ?>

                <script>
                  $('html, body').animate({
                    scrollTop: $("div.notific").offset().top
                  }, 1000)
                </script>

                <?php
        }
      
     }
     
     ?>
          </div>

        </div>

      </div>

    </div>


    <?php
 
 if(isset($_POST['email']) && isset($_FILES['cv'])) { 
 
  $naam = $_POST['naam'];
  $achternaam = $_POST['achternaam'];
  $telefoon = $_POST['telefoon'];
  $email = $_POST['email'];
  $vacature = $_POST['vacature'];
  
  
  
  $to = 'email@email.com';
  $from = 'email@email.com';

  $subject = 'Nieuwe sollicitatie op vacature';
 
 
  $message = "<p><b>Details sollicitatie:</b></p>
  
  <p>Naam: $naam </p>
  <p>Achternaam: $achternaam </p>
  <p>Telefoon: $telefoon </p>
  <p>Email: $email </p>
  <p>Vacature: $vacature</p> 
  </html>
  ";
  
  
     $file_tmp_name    = $_FILES['cv']['tmp_name'];
     $file_name        = $_FILES['cv']['name'];
     $file_size        = $_FILES['cv']['size'];
     $file_type        = $_FILES['cv']['type'];
     $file_error       = $_FILES['cv']['error'];
 
     //read from the uploaded file & base64_encode content for the mail
     $handle = fopen($file_tmp_name, "r");
     $content = fread($handle, $file_size);
     fclose($handle);
     $encoded_content = chunk_split(base64_encode($content));
 

    $boundary = md5("em");
        //header
        $headers = "MIME-Version: 1.0\r\n"; 
        $headers .= "From:".$from."\r\n"; 
        $headers .= "Reply-To: ".$to."" . "\r\n";
  $headers .= 'Cc: cc@email.com' . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 
 
  
        //plain text 
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
        $body .= chunk_split(base64_encode($message)); 
        
  
        //attachment
        $body .= "--$boundary\r\n";
        $body .="Content-Type: $file_type; name=".$file_name."\r\n";
        $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
        $body .="Content-Transfer-Encoding: base64\r\n";
        $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
        $body .= $encoded_content; 

  mail($to, $subject, $body, $headers, '-f email@email.com');
   
 } else {}
  
 if(isset($_POST['email']) && !isset($_FILES['cv'])) { 
 
  $naam = $_POST['naam'];
  $achternaam = $_POST['achternaam'];
  $telefoon = $_POST['telefoon'];
  $email = $_POST['email'];
  $vacature = $_POST['vacature'];
  
  $to = 'email@email.com';
  $from = 'email@email.com';

  $subject = 'Nieuwe sollicitatie op vacature';
 
 
  $message = "<p><b>Details sollicitatie:</b></p>
  
  <p>Naam: $naam </p>
  <p>Achternaam: $achternaam </p>
  <p>Telefoon: $telefoon </p>
  <p>Email: $email </p>
  <p>Vacature: $vacature</p> 
  </html>
  ";
  
  
     $file_tmp_name    = $_FILES['cv']['tmp_name'];
     $file_name        = $_FILES['cv']['name'];
     $file_size        = $_FILES['cv']['size'];
     $file_type        = $_FILES['cv']['type'];
     $file_error       = $_FILES['cv']['error'];
 
     //read from the uploaded file & base64_encode content for the mail
     $handle = fopen($file_tmp_name, "r");
     $content = fread($handle, $file_size);
     fclose($handle);
     $encoded_content = chunk_split(base64_encode($content));
 

    $boundary = md5("sanwebe");
        //header
        $headers = "MIME-Version: 1.0\r\n"; 
        $headers .= "From:".$from."\r\n"; 
        $headers .= "Reply-To: ".$to."" . "\r\n";
  $headers .= 'Cc: cc@email.com' . "\r\n";
        $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 
 
  
        //plain text 
        $body = "--$boundary\r\n";
        $body .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
        $body .= "Content-Transfer-Encoding: base64\r\n\r\n"; 
        $body .= chunk_split(base64_encode($message)); 
        
  
        //attachment
        $body .= "--$boundary\r\n";
        $body .="Content-Type: $file_type; name=".$file_name."\r\n";
        $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
        $body .="Content-Transfer-Encoding: base64\r\n";
        $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n"; 
        $body .= $encoded_content; 

  mail($to, $subject, $body, $headers, '-f email@email.com ');
   
 } else {}
 
 ?>


</body>

</html>
Bart Bos
  • 11
  • 2
  • have you configured your send mail folder? – Ashutosh Kolambkar May 31 '18 at 07:53
  • I don't have access to the cpanel of the webserver, but a PHP contact form doesn't need a send mail folder to send emails or do they? – Bart Bos May 31 '18 at 07:58
  • Try to find proper programmer before this task, then he will nail it. Why not use PHP Mailer library (or other)? – Justinas May 31 '18 at 08:04
  • 1
    Possible duplicate of [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – CBroe May 31 '18 at 08:09
  • 1
    Use a proper mailer library, as outlined at the end of [this answer](https://stackoverflow.com/a/24644450/1427878) to the mentioned duplicate. Assembling e-mail _properly_ is a complex issue; the people providing those libraries have the know-how and the solutions are well tested. Don’t waste time trying to implement this yourself using PHP’s `mail`. – CBroe May 31 '18 at 08:10
  • start working on a local server and then execute on hosted server – Ashutosh Kolambkar May 31 '18 at 08:18

1 Answers1

0

It has been my experience that this type of situation is not related to your code. You could check your phpmail.log but I suspect you will see all of your emails listed.

The key here is that the CC is always received. That means your emails are being sent but not delivered. This points to your DNS configuration. It may be your SPF record, or lack of a DKIM entry.

Since you are using PHP's mail() function, you are not sending via authenticated SMTP. I would second @CBroe's recommendation to use a mail library, but for a different reason. It will provide you the option of using authenticated SMTP which will have a much higher possibility of delivery.

I would recommend:

  • Check your SPF Record and make sure it includes your server's IP address. Preferably it will end with "-all" (avoid "+all").
  • Use a PHP mail library and use authenticated SMTP. This isn't a requirement, but it makes coding email faster, easier and less error prone. Authenticated SMTP never hurts your chances of delivery.
  • If possible, set up the DKIM record for your domain.
Mr Glass
  • 1,186
  • 1
  • 6
  • 14