PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Eric Sizemore   Dead Simple Email   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example form script that makes use of the class.
Class: Dead Simple Email
Compose and send email using text templates
Author: By
Last change:
Date: 10 years ago
Size: 4,577 bytes
 

Contents

Class file image Download
<?php

/**
* @author Eric Sizemore <admin@secondversion.com>
* @package Dead Simple Email
* @link http://www.secondversion.com
* @version 1.0.0
* @copyright (C) 2014 Eric Sizemore
* @license
*
* Dead Simple Email free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

// ################################## HTML ##################################
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Dead Simple Email</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="en" />
<script type="text/javascript" language="JavaScript">
<!--
function validate_form()
{
    var flag = true;
    var error_msg = 'The following errors occurred:\n';
    var sname = document.getElementById('sender_name');
    var semail = document.getElementById('sender_email');
    var smessage = document.getElementById('sender_message');

    // Check Name
    if (sname.value == '' || sname.value == null || sname.length < 2)
    {
        flag = false;
        error_msg += '\n Please enter your name';
    }

    // Check email
    if (semail.value == '' || semail.value == null)
    {
        flag = false;
        error_msg += '\n Please enter a valid email address';
    }

    // Check Message
    if (smessage.value == '' || smessage.value == null)
    {
        flag = false;
        error_msg += '\n Please enter a message';
    }

    if (!flag)
    {
        window.alert(error_msg + '\n\n');
    }
    return flag;
}
// -->
</script>
</head>

<body>

<div>
<?php

$result
= '';

// ################################################################
// Process the form and send the email..
if (!empty($_POST['submit']))
{
   
$name = filter_var($_POST['sender_name'], FILTER_SANITIZE_STRING);
   
$email = filter_var($_POST['sender_email'], FILTER_SANITIZE_STRING);
   
$message = str_replace("\r\n", "\n", $_POST['sender_message']);
   
$message = wordwrap(filter_var($message, FILTER_SANITIZE_STRING), 75);

    if (empty(
$name))
    {
       
$result .= 'Your name is required, please go back and enter your name.';
    }
    else if (empty(
$email))
    {
       
$result .= 'Your email is required, please go back and enter your email.';
    }
    else if (empty(
$message))
    {
       
$result .= 'A message is required, please go back and enter a message.';
    }
    else if (!
filter_var($email, FILTER_VALIDATE_EMAIL))
    {
       
$result .= 'Email is invalid. Please try again.';
    }
    else
    {
        require_once(
'./email.class.php');
       
$emailer = new emailer('email@domain.com', $email, 'Test Email');
       
$emailer->use_template(array(
           
'name' => $name,
           
'email' => $email,
           
'ip' => $_SERVER['REMOTE_ADDR'],
           
'message' => $message
       
), 'example.tpl');

        if (
$emailer->send())
        {
           
$result .= "Thank you, $name, your enquiry was sent.";
        }
        else
        {
           
$result .= 'Seems to have been a problem sending the email. Please try again.';
        }
    }
?>
<p><?php echo $result; ?></p>
<?php
}
else
{
?>
<h1>Contact</h1>
    <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post" style="display: inline;" onsubmit="return validate_form();">
    <table border="0" cellpadding="2" cellspacing="1">
    <tbody>
    <tr>
        <td><label for="sender_name">Name:*</label></td>
        <td><input type="text" name="sender_name" id="sender_name" maxlength="100" /></td>
    </tr>
    <tr>
        <td><label for="sender_email">E-mail:*</label></td>
        <td><input type="text" name="sender_email" id="sender_email" maxlength="100" /></td>
    </tr>
    <tr>
        <td valign="top"><label for="sender_message">Message:*</label></td>
        <td><textarea name="sender_message" id="sender_message" rows="4" cols="35"></textarea></td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" name="submit" value="Submit" /></td>
    </tr>
    </tbody>
    </table>
    </form>
<?php
}

?>
</div>

</body>
</html>