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 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
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio" id="optionsRadios2" value="Collab">Collaboration
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio" id="optionsRadios3" value="Other">Other
</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' );
}
}
?>