-1

I've done my own mail.php before, but I've been learning about PHPMailer and wanted to implement it on my form for my new site in development. Every time I test it by entering information and hitting send, it gives me an Internal Server Error, and not really explaining why, but it may have something to do with my header perhaps.

my site/page for reference

My code:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form action="php/mail.php" method="POST">
  <div class="form-group">
    <input type="text" class="form-control" id="name" placeholder="Full Name" required>
  </div>
  <div class="form-group">
    <input type="email" class="form-control" id="email" placeholder="Email Address" required>
    <small class="text-muted">I will never share your email with anyone. Pinky swear.</small>
  </div>
  <div class="form-group">
    <input type="phone" class="form-control" id="phone" placeholder="Phone Number" required>
  </div>
  <div class="form-group">
    <input type="text" class="form-control" id="company" placeholder="Company (optional)">
  </div>

  <div class="form-group">
    <div class="radio">
      <label>
        <input type="radio" name="radio" id="optionsRadios1" value="Project" checked>Project&nbsp;&nbsp;
      </label>
    </div>
    <div class="radio">
      <label>
        <input type="radio" name="radio" id="optionsRadios2" value="Collab">Collaboration&nbsp;&nbsp;
      </label>
    </div>
    <div class="radio">
      <label>
        <input type="radio" name="radio" id="optionsRadios3" value="Other">Other&nbsp;&nbsp;
      </label>
    </div>
  </div>

  <div class="form-group">
    <textarea class="form-control" id="message" rows="4" placeholder="What do you need?" required></textarea>
  </div>
  <button class="btn btn-primary" style="float: right;" type="submit">Send Away!</button>
</form>

Here's my php

<?php

//gather all form fields
$name = mysqli_real_escape_string($db, $_POST['name']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$phone = mysqli_real_escape_string($db, $_POST['phone']);
$company = mysqli_real_escape_string($db, $_POST['company']);
$radio = mysqli_real_escape_string($db, $_POST['radio']); 
$message = mysqli_real_escape_string($db, $_POST['message']);

if($name == "" || $email == "" || $phone == "" || $company == "" || $radio == "" || $message == "" ){
  //this could load a modal or some type of error pop up
  echo '<div class="alert alert-danger">The form submission is missing values.</div>';
        exit();
 } 
 else {
  
 //include autoload
 require 'php/PHPMailerAutoload.php';
 $mail = new PHPMailer;

 $mail->isMail();                                      // Set mailer to use SMTP
 
 $mail->setFrom('from@example.com', 'Mailer');
 $mail->addAddress('renegade13design@gmail.com', 'Joe User');     // Add a recipient
 
 $mail->isHTML(true);                                  // Set email format to HTML
 
 $mail->Subject = 'Here is the subject';
 $mail->Body    = '
  <!doctype html>
  <html>
  <head>
  <link rel="stylesheet" href="" type="text/css" media="screen, projection" />
  <style>
  h1 { font-size: 14px; }
  body {font-family: helvetica neue, arial;}
  </style>
  </head>
  <body>
  
  <table style="width: 600px; padding: 12px;" align="center">
   <tr>
    <td><img src="img/form.jpg"></td>
   </tr>
   <tr>
    <td><strong>Full Name:</strong> '.$name.'</td>
   </tr>
   <tr>
    <td><strong>Email Address:</strong> '.$email.'</td>
   </tr>
   <tr>
    <td><strong>Phone Number:</strong> '.$phone.'</td>
   </tr>
   <tr>
    <td><strong>Company:</strong> '.$company.'</td>
   </tr>
   <tr>
    <td><strong>What do you want?:</strong> '.$radio.'</td>
   </tr>
   <tr>
    <td><strong>What do you need?:</strong> '.$message.'</td>
   </tr>
  </table>
  
  </body>
  </html>   
 
 ';
 
 if(!$mail->send()) {
  echo 'Message could not be sent.';
  echo 'Mailer Error: ' . $mail->ErrorInfo;
 } else {
  header( 'http://www.scratchmediaohio.com/twentysixteen' );
 } 
  
}
?>

1 Answers1

0

you dont have the "name" attribute for some of your inputs - therefore they will not be a part of the $_POST array, and therefore not be present for your mail script.

<input type="text" class="form-control" id="name" placeholder="Full Name" required>

should be

<input type="text" class="form-control" name="name" id="name" placeholder="Full Name" required>

for your inputs:name, email,phone and company - the only one that does have them is the radio selection. I would also suggest more validation for your inputs than you currently have.

gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • Of course I would forget something so obvious. I added those in, but I'm still getting the server error. – Ashley Duncan Apr 08 '16 at 00:53
  • you have ...header( 'http://www.scratchmediaohio.com/twentysixteen' );...but shouldn't that be...header("Location: http://www.scratchmediaohio.com/twentysixteen");... and then you might also need an extension such as .php – gavgrif Apr 08 '16 at 03:09
  • I noticed that error and made it header( "Location: http://www.scratchmediaohio.com/twentysixteen/contact.php" ); but I am still getting the 500 error. When I turn on the display errors I get nothing as well, which is strange. – Ashley Duncan Apr 08 '16 at 03:20