Skip to content

Instantly share code, notes, and snippets.

@jim-at-jibba
Last active August 29, 2015 14:04
Show Gist options
  • Save jim-at-jibba/fb2bd69512aeb399447b to your computer and use it in GitHub Desktop.
Save jim-at-jibba/fb2bd69512aeb399447b to your computer and use it in GitHub Desktop.
Parts for form using swiftmailer - needs swiftmailer library
<!-- START form part -->
<?php
start_session();
//init variables
$cf = array();
$sr = FALSE;
if (isset($_SESSION['cf_returndata'])) {
$cf = $_SESSION['cf_returndata'];
$sr = TRUE;
}
?>
<ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
<li id="info">There were some problems with your form submission:</li>
<?php
if (isset($cf['errors']) && count($cf['errors']) > 0) :
foreach ($cf['errors'] as $error) :
?>
<li><?php echo $error ?></li>
<?php
endforeach;
endif;
?>
<!-- Move styles else where else-->
<style>
.no-show {
display: none;
}
</style>
<p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for your message. We will get
back to you ASAP!</p>
<form action="" method="POST">
<label for="Name">Name:</label>
<input type="text" name="given-name" id="Name"
value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>"/>
<div class="no-show"><label for="Name">Address</label>
<input type="text" name="no-show" id="Address"
value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['no-fill'] : '' ?>"/></div>
<label for="email">Email:</label>
<input type="text" name="Email" id="email"
value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>"/>
<label for="telephone">Telephone:</label>
<input type="text" name="telephone" id="telpehone"
value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['telephone'] : '' ?>"/>
<label for="message">Message:</label>
<input type="text" name="Message" id="message"
value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?>"/>
</form>
<?php unset($_SESSION['cf_returndata']); ?>
<!-- if there are any errors then they will be displayed and then the array is distroyed -->
<!-- mail code-->
<!-- END form part -->
<!-- START of mail part -->
<?php
if( $_POST['surprise'] !== "" ){
die("A honeypost has been filled in meaning you are a spambot. Bugger Off");
} else {
//form validation vars
$formok = TRUE;
$errors = array ();
$sendsms = FALSE;
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date( 'd/m/Y' );
$time = date( 'H:i:s' );
//form data
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//validate form data
//validate name is not empty
if( empty( $name ) ) {
$formok = FALSE;
$errors[] = "You have not entered a name";
}
//validate name contains no special chracters
elseif(preg_match("/^[A-Z][a-zA-Z -]+$/", $name) === 0) {
$formok = FALSE;
$errors[] = "Name must be from letters, dashes, spaces and must start with a capital letter";
}
//validate email address is not empty
if( empty( $email ) ) {
$formok = FALSE;
$errors[] = "You have not entered an email address";
//validate email address is valid
}
elseif( !filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
$formok = FALSE;
$errors[] = "You have not entered a valid email address";
}
//validate message is not empty
if( empty( $message ) ) {
$formok = FALSE;
$errors[] = "You have not entered a message";
}
//validate message is greater than 20 characters
elseif( strlen( $message ) < 20 ) {
$formok = FALSE;
$errors[] = "Your message must be greater than 20 characters";
}
//validate message contains no special chracters
elseif(preg_match("/^[A-Z][a-zA-Z ?.,-]+$/", $message) === 0){
$formok = FALSE;
$errors[] = "Messages must be from letters, numbers, spaces and any of the following .,-? This is a measure to stop SPAMBOTS sorry for the inconvience.";
}
//send email if all is ok
if( $formok ) {
// Load in the required Swift files
require_once "lib/swift_required.php";
// Create the mail transport configuration
$transport = Swift_MailTransport::newInstance();
//Create template
$template = <<<EOT
<p>You have recieved a new message from the English enquiries form on your website.</p>
<p><strong>Name: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Telephone: </strong> {$telephone} </p>
<p><strong>Enquiry: </strong> {$enquiry} </p>
<p><strong>Message: </strong> {$message} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>
EOT;
// Set to enquiries@invest-direct-online.com
$to = array ( "your-email"
);
$bcc = array ( "you-bcc"
);
// Create the message
$message = Swift_Message::newInstance();
$message->setTo( $to );
$message->setBcc( $bcc );
$message->setSubject( "You Subject - {$enquiry} Enquiry" );
$message->setBody( $template, 'text/html' );
$message->setFrom( $email, $name );
// Send the email
$mailer = Swift_Mailer::newInstance( $transport );
// Send the message
$result = $mailer->send($message);
}
//what we need to return back to our form
$returndata = array ( 'posted_form_data' => array ( 'name' => $name,
'email' => $email,
'telephone' => $telephone,
'enquiry' => $enquiry,
'message' => $message
),
'form_ok' => $formok,
'errors' => $errors
);
//if this is not an ajax request
if( empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) !== 'xmlhttprequest' ) {
//set session variables
session_start();
$_SESSION['cf_returndata'] = $returndata;
//redirect back to form
header( 'location: ' . $_SERVER['HTTP_REFERER'] );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment