<?php
// CONFIG
$mail_to = 'myemailaddress';
// END CONFIG
session_start();
function strip_mail_headers_single( $string ) {
return preg_replace('/(%0A|%0D|\\n+|\\r+)/i', '', $string);
}
function strip_mail_headers_multi( $string ) {
return preg_replace('/(%0A|%0D|\\n+|\\r+)(content-type:|to:|cc:|bcc:)/i', '', $string);
}
function is_valid_email( $string ) {
return preg_match('^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$', $string);
}
function send_mail($to, $from, $from_mail, $subject, $message) {
if ( empty($from) || empty($from_mail) || empty($subject) || empty($message) ) {
return -1;
}
if ( $_SESSION['last_mailed'] + 180 < time() )
return -2;
if ( !is_valid_email($from_mail) )
return -3;
$from = strip_mail_headers_single($from);
$from_mail = strip_mail_headers_single($from_mail);
$subject = strip_mail_headers_single($subject);
$message = strip_mail_headers_multi($message);
$_SESSION['last_mailed'] = time();
return mail($to, $subject, $message, "From: $from <$from_mail>\r\n");
}
if ( !empty($_POST) ) {
$result = send_mail($mail_to, $_POST['from'], $_POST['from_mail'], $_POST['subject'], $_POST['message']);
if ( $result == -1 ) {
echo "<p>Whoops! You need to complete all the fields.</p>";
} elseif ( $result == -2 ) {
echo "<p>Whoah, slow down there cowboy! You can only send one mail every three minutes.</p>";
} elseif ( $result == -3 ) {
echo "<p>Please enter a valid email address.</p>";
} else {
echo "<p>Mail sent successfully!</p>";
}
}
?>