*
* @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);
?>
PHP Mailer
PHP Mailer
You only get to send mail once with this mailer. It should prevent multiple postings, or postings from external sources.
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('=((||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();
}
?>