<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "someone@example.com", "Subject: $subject",
$message, "From: $email" );
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
echo "<form method='post' action='mailform.php'>
Email: <input name='email' type='text' /><br />
Subject: <input name='subject' type='text' /><br />
Message:<br />
<textarea name='message' rows='15' cols='40'>
</textarea><br />
<input type='submit' />
</form>";
}
?>
</body>
</html>
<?php
session_start();
$cansend=$HTTP_POST_VARS['cansend'];
if($cansend==1) {
$mailTo = "Your E-Mail Address"; # Put your E-mail Address here
$name=$HTTP_POST_VARS["name"];
$from=$HTTP_POST_VARS["email"];
$message=$HTTP_POST_VARS["message"];
$subject=$HTTP_POST_VARS["subject"];
$mailSubject="Website: $subject (From: $name)"; # Use this to customise the subject of the E-Mail.
$mailbody=$mailbody."\n".$message;
$headers = "From: $from";
if(mail($mailTo,$mailSubject,$mailbody,$headers))
{
$status="<div style='color:green;font-weight:bold;'>Your message has been sent successfully!<BR>I will get back to you as soon as possible.</div><BR><A href=index.php>Back to the homepage</A></center>";
}
else
{
$status="<center><div style='color:red;font-weight:bold;'>There was an error in sending your message!</div><BR><A href=index.php>Back to the homepage</A></center>";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Contact the Admin</TITLE>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
</HEAD>
<BODY>
<SCRIPT type="text/javascript" language="javascript" src="email-check.js"></SCRIPT>
<TABLE cellpadding=5 cellspacing=2>
<?php
if($status) {
?>
<TR>
<TD colspan="2"><FONT size="+1"><B><?php echo $status;?></B></FONT></TD>
</TR>
<?php
}
else {
?>
<TR>
<TD colspan="2"><P style="color:#FF0000">* All fields are required. </TD>
</TR>
<FORM method="post" name="contact" action="contact.php" onSubmit="return validate();">
<TR>
<TD align="right"><STRONG>Your Name: </STRONG></TD>
<TD><INPUT name="name" type="text" maxlength="30"></TD>
</TR>
<TR>
<TD align="right"><STRONG>Your E-mail: </STRONG></TD>
<TD><INPUT name="email" type="text" maxlength="40"></TD>
</TR>
<TR>
<TD align="right"><STRONG>Subject: </STRONG></TD>
<TD><INPUT name="subject" type="text" maxlength="40"></TD>
</TR>
<TR>
<TD align="right" valign="top"><STRONG>Message: </STRONG></TD>
<TD><TEXTAREA name="message" cols=50 rows="10"></TEXTAREA></TD>
</TR>
<TR>
<INPUT type="hidden" name="cansend" value="0">
<TD> </TD>
<TD>
<INPUT type='submit'>Send</INPUT>
<INPUT type='button' onClick="redirect();">Cancel</A>
</TD>
</TR>
</FORM>
<?php
}
?>
</TABLE>
</BODY>
</HTML>
function redirect()
{
window.location="URL to your homepage";
}
function validate()
{
document.contact.cansend.value=1;
return true;
}
function isEmail(emailstr)
{
dotchar = emailstr.indexOf(".");
atchar = emailstr.indexOf("@");
dotlast = emailstr.lastIndexOf(".");
spacechar = emailstr.indexOf(" ");
len = emailstr.length;
if( (dotchar == -1) || (atchar == -1) || (spacechar != -1) || (dotlast < atchar) || (dotlast == len - 1) )
{
return false;
}
else
{
return true;
}
}
function trim(str)
{
ch = '';
for(i=0;i<str.length;i++)
{
cha = str.charAt(i);
if(cha != ' ')
{
ch = ch + cha;
}
}
return ch;
}
<html>
<head>
<style>
textarea.message{
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<form action="formpost.php" method="post" id="form">
<textarea id="message" value="" class="message"></textarea><br />
<input type="submit" value="Send Message" />
</form>
</body>
</html>
<?php
$message = $_REQUEST['message'] ;
?>
<html>
<body>
/*html code*/
<?php
if(mail(email@host.com,"subject",$message)
{
echo "/*html code for success*/";
}
else
{
echo "/*html code for failure*/";
}
?>
</body>
</html>
This is a basic version of the system i use:
'form.php'
HTML:<html> <head> <style> textarea.message{ width: 300px; height: 200px; } </style> </head> <body> <form action="formpost.php" method="post" id="form"> <textarea id="message" value="" class="message"></textarea><br /> <input type="submit" value="Send Message" /> </form> </body> </html>
'formpost.php'
PHP:<?php $message = $_REQUEST['message'] ; ?> <html> <body> /*html code*/ <?php if(mail(email@host.com,"subject",$message) { echo "/*html code for success*/"; } else { echo "/*html code for failure*/"; } ?> </body> </html>
its short and it works.
hope it helps!
TechAsh said:NOTE: If you haven't already you will need to upgrade to intermediate PHP for the mail() function to work.