Here is some code to handle a simple contact form and email it to someone. This is another piece of code that has grown at of various sources, the is_dodgy function and usage came from a contact form plugin for wordpress which can be downloaded from http://chip.cuccio.us/projects/contact-form-ii/. You'll need to download XPertMailer from http://xpertmailer.sourceforge.net/ and put the files in the same directory as your contact code. XPertMailer handles the actual mailing, the php mail() function isn't very good and is open to all sorts of abuse by spammers if the coder has been slack.
You may also want to include my email validation code in with this script to check the incoming email address is valid.
I'll be posting up another version of this script with akismet filtering for the spam soon.
-
<?php
-
-
function is_dodgy($input) {
-
$is_malicious = false;
-
$bad_inputs = array("<", ">", "<", ">", "mime-version", "content-type", "cc:", "bcc:", "to:", "<a href", "</a>", "http://", "[/URL]", "[URL=");
-
foreach($bad_inputs as $bad_input) {
-
$is_malicious = true; break;
-
}
-
}
-
return $is_malicious;
-
}
-
-
{
-
$error_mesage = "";
-
-
$to = "youremail@address.com";
-
$from = "";
-
$subject = "";
-
$message = "";
-
-
{
-
}
-
-
{
-
}
-
-
{
-
}
-
-
if ($from == "")
-
{
-
$error_message .= "Please enter your e-mail address<br>";
-
}
-
-
if ($subject == "")
-
{
-
$error_message .= "Please enter a subject<br>";
-
}
-
-
if ($message == "")
-
{
-
$error_message .= "Please enter a message<br>";
-
}
-
-
$dodgy = false;
-
-
if (is_dodgy($from) || is_dodgy($subject))
-
{
-
$dodgy = true;
-
}
-
{
-
$dodgy = true;
-
}
-
-
if ($dodgy)
-
{
-
$error_message = "I'm sorry but you've passed illegal content, please try again";
-
}
-
-
if ($error_message == "")
-
{
-
require_once "smtp.php";
-
-
// turn off errors
-
-
// 0 -> no time limit
-
-
$mail = new SMTP;
-
if ($mail->From($from))
-
{
-
$mail->AddTo($to);
-
$mail->Text($message);
-
$sent = $mail->Send($subject);
-
-
if ($sent)
-
{
-
echo "Thank you for your message, we will attempt to contact you shortly";
-
}
-
else
-
{
-
echo "Thank you for your message, however there was an error whilst sending the e-mail. Please try again later";
-
}
-
}
-
else
-
{
-
$error_message .= "Your email address appears to be invalid<br>";
-
}
-
}
-
}
-
-
{
-
?>
-
-
<p>Please contact us using the form below, please include contact details so we can contact you back:</p>
-
-
<?php
-
-
-
?>
-
-
<form id="contact" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
-
<input type="hidden" name="send" value="1">
-
<table border="0" cellspacing="4" cellpadding="0">
-
<tr><td>E-mail :</td><td><input class="contact_input" type="text" name="from" size="20" maxlength="255" value="<?php echo htmlspecialchars($from) ?>"></td></tr>
-
<tr><td>Subject :</td><td><input class="contact_input" type="text" name="subject" size="20" maxlength="255" value="<?php echo htmlspecialchars($subject) ?>"></td></tr>
-
<tr><td valign="top">Message :</td><td><textarea class="contact_input" rows="8" cols="50" name="message"><?php echo htmlspecialchars($message) ?></textarea></td></tr>
-
<tr><td></td><td align="left"><input type="submit" value="Send Message"> <input type="reset" value="Reset Form"></td></tr>
-
</table>
-
</form>
-
-
<?php } ?>