It's essentially a working contact form that Ryan helped me with to which I'm trying to integrate PHPMailer as I was having problems receiving mail natively.
Here is my code:
<?php
include("./header.inc");
$success_message = "<h3 class='success'>Thank you, your message has been sent.</h3>";
$success = false; // we assume it and set to true if mail sent
$error = false;
// set and sanitize our form field values
$form = array(
'name' => $input->post->name,
'email' => $input->post->email,
'phone' => $input->post->phone,
'subject' => $input->post->subject,
'level' => $input->post->level,
'hours' => $input->post->hours,
'message' => $input->post->message
);
$required_fields = array(
'name' => $input->post->name,
'email' => $input->post->email,
'subject' => $input->post->subject,
'level' => $input->post->level,
);
// check if the form was submitted
if($input->post->submit) {
// determine if any fields were ommitted or didn't validate
foreach($required_fields as $key => $value) {
if( trim($value) == '' ) {
$error_message = "<h3 class='error'>Please check that you have completed all the required fields.</h3>";
$error = true;
}
}
// if no errors, email the form results
if(!$error) {
$to_name = "To name";
$to = "recipient@email.com";
$subject = "Contact Form Request";
$from_name = "From Name";
$from = "sender@email.com";
foreach($form as $key => $value) $message .= "$key: $value\n";
require_once("./scripts/PHPMailer/class.phpmailer.php");
require_once("./scripts/PHPMailer/class.smtp.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->FromName = $from_name;
$mail->From = $from;
$mail->AddAddress($to, $to_name);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
if ($mail->Send()) {
$success = true;
}
}
}
?>
<?php if(!$success) { ?>
<?php
if($error) {
echo $error_message; // or pull from a PW field
}
?>
<p id="contact_header">If you'd like to get in touch regarding tuition, please use the form below.</p>
<div id="contact_aside">
<h6>Or you can find me, here: </h6>
<ul>
<li>Skype: <?php echo $page->skype_name; ?></li>
<li><a href="<?php echo $page->fb_url; ?>" target="_blank">Facebook</a></li>
<?php if ($page->twitter_url) {
echo "<li><a href='$page->twitter_url' target='_blank'>Twitter</a></li>";
} ?>
</ul>
</div><!-- /#contact_aside -->
<div class="clear"></div><!-- /.clear -->
<form action="./" method="post" id="contact_form">
<fieldset class="first">
<h6>About yourself: </h6>
<label for="name">Your name: (required) </label>
<input type="text" name="name" autofocus>
<label for="email">Your email: (required) </label>
<input type="email" name="email">
<label for="phone">Your phone number: </label>
<input type="phone" name="phone">
</fieldset>
<fieldset>
<h6 class="mt60">Your tuition: </h6>
<label for="subject">Subject: </label>
<select name="subject" id="subject" />
<?php
foreach ($page->subject_options as $subject) {
echo "<option>$subject->subject_option</option>";
}
?>
</select>
<label for="level">Level: </label>
<select name="level" id="level" />
<?php
foreach ($page->level_options as $level) {
echo "<option>$level->level_option</option>";
}
?>
</select>
<label for="hours">Hours per week: </label>
<input type="number" min="1" max="40" value="2" name="hours">
</fieldset>
<div class="clear"></div><!-- /.clear -->
<fieldset class="full">
<label for="message">Additional information: </label>
<textarea name="message" cols="30" rows="10"></textarea>
<input type="submit" name="submit" value="submit" id="submit">
</fieldset>
</form>
<div class="clear"></div><!-- /.clear -->
<?php } else {
echo $success_message; ?>
<p>Please click <a href="<?php echo $config->urls->root; ?>">here</a> if you would like to return to the homepage.</p>
<?php } ?>
<?php include("./footer.inc"); ?>At present when the form is submitted, the page seems to think about something for a while then the page gets reloaded exactly the same as before.
Thanks in advance if you have any ideas.













