rendered paste body<?php
/*** vim: set expandtab tabstop=4 shiftwidth=4: ***/
/*** set the email address you want to send to here ***/
$email = 'phpro@phpro.org';
####################################################
### ###
### Do not touch anything below this line ###
### ###
### YOU HAVE BEEN WARNED! ###
### ###
####################################################
/**
* @Simple PHP Form mailer
*
* This little script was written in about an hour to
* meet a quick repair jobby. It is supplied without
* support or fitness for a purpose. Make of it what
* you will. YOU HAVE BEEN WARNED!
*
* @author Kevin Waterson <kevin@phpro.org>
*
* @copyright 2006 phPro
*
* @version CVS: $Id:$
*
* @since Release 0.0.1
*
*/
/**
* TODO
*
* @add attachments
*/
/*** begin a session ***/
session_start();
/*** a new mailer instance ***/
$mailer = new mailer;
/*** set the email address to post to here ***/
$mailer->setMailTo($email);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>PHP Mailer</title>
<script language="javascript">
function textCounter(field, countfield, maxlimit ) {
if ( field.value.length > maxlimit )
{
field.value = field.value.substring( 0, maxlimit );
alert( 'Textarea value can only be 2048 characters in length.' );
return false;
}
else
{
countfield.value = maxlimit - field.value.length;
}
}
</script>
<style type="text/css">
.textfield {
font:13px/20px verdana, arial, helvetica, sans-serif;
color:#333 ;
margin:3px;
height:20px;
border:solid 0 #fff;
padding: 3px 8px;
width:300px;
voice-family: "\"}\"";
voice-family:inherit;
width:300px;
}
.formfield{
font:13px/20px verdana, arial, helvetica, sans-serif;
color:#333 ;
margin:3px;
border:solid 0 #fff;
padding: 3px 8px;
width:300px;
voice-family: "\"}\"";
voice-family:inherit;
width:300px;
}
textarea {
font:13px/20px verdana, arial, helvetica, sans-serif;
color:#333 ;
margin:3px;
height:165px;
border:solid 0 #fff;
padding: 0 8px;
width:300px;
voice-family: "\"}\"";
voice-family:inherit;
width:300px;
}
fieldset>textarea {
width: 300px;
}
.submit {
margin:3px;
height:20px;
border:solid 0 #fff;
width:80px;
font:13px/20px verdana, arial, helvetica, sans-serif;
color:#666;
}
fieldset {
background-color: #e0e0e0;
margin: 2em 2em 2em 0;
padding: 1em .5em .5em .5em;
border: 1px solid black;
}
legend {
padding: .2em;
background-color: #920E00;;
font-weight: bold;
color: #ffffff;
border: 1px solid #000000;
}
</style>
</head>
<body>
<h3>PHP Mailer</h3>
<p>
You only get to send mail once with this mailer. It should prevent multiple postings, or postings from external sources.
</p>
<?php
/*** the mailer class ***/
class mailer{
public $token;
private $mail_to;
private $sender_email;
private $sender_name;
private $sender_subject;
private $sender_message;
private $sender_send_self;
/*** the constructor, duh ***/
function __construct(){
$this->token= $this->setToken();
}
/**
*
* @Set the form token
*
* @access private
*
* @return string
*
*/
function setToken(){
return md5(uniqid(rand(), TRUE));
}
/**
*
* @format a name
*
* @param string $name
*
* @return string formated name
*
**/
function formatName($name){
return ucwords(strtolower($name));
}
/**
*
* @strip nasty chars from email headers
*
* @access public
*
* @param string
*
* @return string
*
*/
public function sanitizeEmailHeaders($string){
$string = preg_replace('=((<CR>|<LF>|0x0A/%0A|0x0D/%0D|\\n|\\r)\S).*=i', null, $string);
return $string;
}
/**
*
* Mail to user
*
* @access public
*
* @throw exception on failure
*
*/
public function sendmail(){
/*** mail the damn thing ***/
if(!mail($this->mail_to, $this->sender_subject, $this->sender_message, $this->headers()))
{
throw new exception("Unable to Deliver Mail");
}
}
/**
*
* @Set email headers
*
* @acess private
*
* return string
*
*/
private function headers(){
/*** new line at end of line ***/
$eol = "\n";
/*** set the headers ***/
$headers = '';
$headers .= 'From: '.$this->sender_name.'<'.$this->sender_email.'>'.$eol;
$headers .= 'Reply-To: '.$this->sender_name.'<'.$this->sender_email.'>'.$eol;
$headers .= 'Return-Path: '.$this->mail_to.$eol;
$headers .= "Message-ID: <".time()." TheSystem@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
if($this->sender_send_self = true)
{
$headers.="BCC: ".$this->sender_email.$eol;
}
# add attachments here....
return $headers;
}
/**
*
* @set the send to self option
*
* @param bool
*
* return bool
*
*/
public function setSendToSelf($sender_send_self){
return $sender_send_self;
}
public function setMailTo($mail_to){
$this->mail_to = $mail_to;
}
public function setSenderName($sender_name){
$this->sender_name = $this->formatName($sender_name);
}
public function setSenderEmail($sender_email){
$this->sender_email = $sender_email;
}
public function setSenderSubject($sender_subject){
$this->sender_subject = $sender_subject;
}
public function setSenderMessage($sender_message){
$this->sender_message = $sender_message;
}
/**
*
* @check the max length of a string
*
* @param string
*
* @param INT
*
* @throw exception on failure
*
*/
public function checkLength($string, $max){
if(strlen($string)>$max)
{
throw new Exception("Field is too long");
}
}
} /*** end of class ***/
/*** make sure we have a token ***/
if(!isset($_SESSION['token']))
{
$_SESSION['token'] = $token = $mailer->token;
}
try {
/*** default form values ***/
$sender_name = 'Anonymous Coward';
$sender_email = '';
$sender_subject = '';
$sender_message = 'A small message here';
/*** check the POST and SESSION tokens match ***/
if(isset($_POST['token']) && $_POST['token'] === $_SESSION['token'])
{
/*** check the filter has token ***/
if(filter_has_var(INPUT_POST, "token") === false)
{
throw new Exception('Please Fill ALL fields');
}
/*** check if we are going to mail to self ***/
if(filter_input(INPUT_POST, 'sender_send_self', FILTER_VALIDATE_BOOLEAN) == TRUE)
{
$mailer->setSendToSelf(true);
}
/*** filters to check for string length ***/
$mailer->checkLength('sender_name', 50);
$mailer->checkLength('sender_email', 254);
$mailer->checkLength('sender_subject', 40);
$mailer->checkLength('sender_message', 2048);
/*** validation and sanitizing filters ***/
$filters = array(
"sender_name" => array('filter'=>FILTER_SANITIZE_STRING, 'flags' => FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW),
"sender_subject" => array('filter' => FILTER_CALLBACK, 'options' => array($mailer, 'sanitizeEmailHeaders')),
"sender_email" => FILTER_VALIDATE_EMAIL,
"sender_message" => FILTER_SANITIZE_SPECIAL_CHARS
);
/*** filter the POST array ***/
$filtered = filter_input_array(INPUT_POST, $filters);
/*** if none of the filters returns FALSE ***/
if(!in_array(FALSE, $filtered))
{
$mailer->setSenderName($filtered['sender_name']);
$mailer->setSenderEmail($filtered['sender_email']);
$mailer->setSenderSubject($filtered['sender_subject']);
$mailer->setSenderMessage($filtered['sender_message']);
/*** if we have got this far, all is well... I think ***/
$mailer->sendmail();
/*** if we got this far, all is done ***/
$legend = 'Your Mail Has been Sent';
if(filter_input(INPUT_POST, 'sender_send_self', FILTER_VALIDATE_BOOLEAN) == TRUE)
{
$mailer->setMailTo($filtered['sender_email']);
$mailer->setSenderName($filtered['sender_name']);
$mailer->setSenderEmail($filtered['sender_email']);
$mailer->setSenderSubject($filtered['sender_subject']);
$mailer->setSenderMessage($filtered['sender_message']);
}
/*** unset the session ***/
session_unset();
/*** and dash it against the rocks ***/
session_destroy();
}
else
{
throw new Exception("Invalid Arg Sent");
}
}
else
{
throw new Exception("Please Send a Message!");
}
}
catch(Exception $e)
{
$legend = $e->getMessage();
}
?>
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<fieldset>
<legend><?php echo $legend; ?></legend>
<label for="Name"> Name:</label><br />
<input type="hidden" value="<?php echo $mailer->token; ?>" name="token" />
<input type="text" maxlength="50" class="textfield" name="sender_name" value="<?php echo $sender_name; ?>" id="Name" />
<br />
<label for="Subject">Subject:</label><br />
<input type="text" maxlength="40" class="textfield" name="sender_subject" id="Subject" value="<?php echo $sender_subject; ?>" />
<br />
<label for="Email">Email:</label><br />
<input type="text" maxlength="248" class="textfield" name="sender_email" id="Email" value = "<?php echo $sender_email; ?>" />
<br />
<label for="Message">Message:</label><br />
<textarea rows="10" cols="50" name="sender_message" onkeypress="textCounter(this,this.form.counter,2048);"><?php echo $sender_message; ?></textarea>
<br />
<input type="text" name="counter" maxlength="4" size="4" value="2048" onblur="textCounter(this.form.counter,this,2048);"> characters remaining<br />
<label for="Self":>Send Copy To Me:</lable><br />
<input type="checkbox" name="sender_send_self"><br />
<p><input type="submit" class="submit" value="Submit" /></p>
</fieldset>
</form>
</body>
</html>