I am trying to email a basic contact form using PHPMailer.
This form works for me:
<?php
$first_name = $_POST['first-name'];
$last_name = $_POST['last-name'];
$email = $_POST['email'];
$message = nl2br($_POST['message']);
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->addReplyTo( $email, $first_name );
$mail->addAddress( $email, $first_name );
$mail->addAddress( 'blah@fake.org', 'Staff' );
$mail->From = 'blah@fake.org';
$mail->FromName = 'Staff';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hotel Room Request';
$mail->Body = $message;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
if(!$mail->send()) {
header('location: a_url_here');
} else {
header('location: a_url_here');
}
Now, I'm trying to combine it with error-checking. Don't know how to combine it and still make it work. This is what I have so far, and it blanks out upon submittal. I wasn't sure where to put check_input function, so I put it in the else part at the bottom. How do I make this form functional so not only does it validate user's input but email it out?
<?php
$first_name = check_input($_POST['first-name'], "Please enter your name");
$last_name = check_input($_POST['last-name'], "Please enter your last name");
$email = check_input($_POST['email'], "Please enter your email address");
$message = check_input(nl2br($_POST['message']), "Please enter your message");
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ''; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ''; // SMTP username
$mail->Password = ''; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;
$mail->addReplyTo( $email, $first_name );
$mail->addAddress( $email, $first_name );
$mail->addAddress( 'blah@fake.org', 'Staff' );
$mail->From = 'blah@fake.org';
$mail->FromName = 'Staff';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Hotel Room Request';
$mail->Body = $message;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
if(!$mail->send()) {
header('location: a_url_here');
} else {
function check_input($data, $problem = ' ')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
}
?>
Create so-called
validator
class:And use it like this:
You should not validate and render the form in one file. It leads to poor maintainability and mixes responsibilities in a nasty way. Try structuring your project like so:
The form contains
<form action=validate-and-send.php ...><input ...
. The other file contains logic for validating and sending. Something like this:The tricky part is redirect back to form. You can either redirect with a header and set all valid fields through get-parameters
Location form.php?name=validname
or you can stuff them in $_SESSION and output them in the form from session.Taking it one step farther would be submitting via AJAX and responding with the validation result as JSON, for example. So the flow would be like