A Web Developer's Blog

Doing web based applications using JavaScript & PHP/MySQL. Browse for stuff related to JavaScript, DOM, PHP and MySQL. Leave your comment. Lets learn it by doing...

Tuesday, December 13, 2005

simple form 2 mail

This is a very simlpe form to mail script. I did it to teach my team how to send mails You can tweak it to send



  • Plain Text Mail

  • Formatted Html Mail


Just go thru the script it is very simple and self explaining.

<?php

/**********************************************************************************/

* If this is what we call is general approach then it needs to be more */

* generalised. */

/**********************************************************************************/

/*

* Name:Contact.php

* Dated:02-Aug-04 at 10:17:08

* Author:Kumar Chetan

* Desription:This is a plain form2mail script.

*/


/*

* Checking text pattern is not simple task. The *nix based technologies offer a

* very unique way to accomplish this task. The technique used is called regular

* expression. first Shell used it then Perl empowered it and now we have it as a

* common feature for nearly all text handling scripting languages. PHP has it. I

* didn't invested too much time and just copid this function. This function is

* from http://php.net/mail.php.

*/

function check_email_id($Email) {

if (ereg('^[-!#$%&'*+./0-9=?A-Z^_`a-z{|}~]+'. ‘@'.'[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+.'.'[-!#$%&'*+./0-9=?A-Z^_`a-z{|}~]+$', $Email)) {

return 1;//OK

}else{

return 0;//Naaaaaaa

}

}


/*

Please enter the email address which is going to recieve the mail. Multiple

Email ids can be added as ‘kumar@indianbizdirectory.com,chetan@indianbizdirectory.com'

and so on.

*/

$to = ‘';


/*

This is going to be the subject line of the mail $to variable recieves. Typical

value will be “Response from website".

*/

$subject = ‘';


/*

This is going to be the subject line of the mail which is send to the visitor

when he/she submits the form. Typical value will be “Thanks a lot!".

*/

$thanxsubject = ‘';


/*

This is going to be the Body or the message which will be recieved by the visitor.

*/

$thanxmessage = ‘';


/*

This var holds the meaasge which is PRINTED on page when mail is sent successfully.

*/

$thanks = ‘';


/*

Error Message if by any chance the server is unable to send mail. If this message

is displayed it means we need to check our webservers.

*/

$error = ‘<h1>Error!</h1><p class="txt">Error occured while sending mail. <a href="javascript:history.go(-1);” class="type1″>Please try again</a>.</p>';


/*

This is the main form. Along with this form is javascript for form validation.

*/

$formwidscript = ‘

<script language="javascript">

/*

* This function is a very simeple function. Even a dumb can modify it and reuse it.

*/

function checkcontactform(form2)

{

if(form2.name.value==""){alert("Please enter your name.");form2.name.focus();return (false);}

if(form2.email.value==""){alert("Please Enter Your Email ID.");form2.email.focus();return(false);}

if(form2.email.value!=""){pass = form2.email.value.indexOf('@',0);if(pass==-1){alert("Not a valid email address");form2.email.focus();form2.email.value=""; return (false);}}

if(form2.message.value==""){alert("Please enter some comments.");form2.message.focus();return (false);}

}

</script>

<form name="contact” action="<?echo $PHP_SELF;?>” method="POST” onSubmit="return checkcontact(this)">

<div align="center">

<table border="0″ cellspacing="1″ cellpadding="3″>

<tr>

<td align="right” valign="top">Name</td>

<td align="left” valign="top"> <b>

<input type="text” name="name” class="textfield">

</b></td>

</tr>

<tr>

<td align="right” valign="top">E-mail</td>

<td align="left” valign="top"><b>

<input type="text” name="email” class="textfield">

</b></td>

</tr>

<tr>

<td align="right” valign="top">Subject</td>

<td align="left” valign="top"><b>

<input type="text” name="subject” class="textfield">

</b></td>

</tr>

<tr>

<td align="right” valign="top">Message</td>

<td align="left” valign="top"><b>

<textarea name="msg” cols="30″ rows="3″ class="textfield"></textarea>

</b></td>

</tr>

<tr align="center">

<td valign="top” colspan="2″>

<input type="submit” name="Submit” value="Submit” class="button">

</td>

</tr>

</table>

</div>

</form>

‘;


/*Time to get into action. If the “Submit button has been hit."*/

if ($Submit=="Submit")

{

/*Check for valid email ID.*/

if (!check_email_id($email))

echo ‘Not a valid email id. Please enter valid email id.<a href="javascript:history.go(-1);” class="type1″>Please try again</a>.';


$msg='

<!–This is a html email. If you are looking at this text please upgarde your email client.–>

<html>

<head>

<title>Request for contact</title>

<meta http-equiv="Content-Type” content="text/html; charset=iso-8859-1″>

<META content="Sunrise Software Solutions - http://sunrise.indianbizdirectory.com” name="author">

<META content="KIM@ilR” name="generator">

<style type="text/css">

<!–

.txt { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 8pt; color: #333333}

–>

</style>

</head>

<body bgcolor="#FFFFFF">

<h3 align="center"><font face="Arial, Helvetica, sans-serif">Request for contact from website</font></h3>

<p class="text>”

Hi,

Following is a new contact request from website.

<div align="center” class="txt">

<table border="0″ cellspacing="1″ cellpadding="3″ width="90%">

<tr>

<td align="right” valign="top">Name</td>

<td align="left” valign="top"> <b>'.$name.'</b></td>

</tr>

<tr>

<td align="right” valign="top">E-mail</td>

<td align="left” valign="top"><b> ‘.$email.'</td>

</tr>

<tr>

<td align="right” valign="top">Subject</td>

<td align="left” valign="top"><b> ‘.$subject.'</b></td>

</tr>

<tr>

<td align="right” valign="top">Message</td>

<td align="left” valign="top"><b>'.$msg.'</b></td>

</tr>

</table>

</div></p>

</body>

</html>

‘;

/*Set the reciever and subject vars.*/


/*Email Header*/

$headers = “MIME-Version: 1.0rn";

$headers .= “Content-type: text/html; charset=iso-8859-1rn";


/* additional headers */

$headers .= “From: $name <$email>rn";


/* and now mail it */

if(mail($to, $subject, $msg, $headers) )

{

echo $thanks;

}else{

echo $error;

}


}else{

/*Use the form here*/

echo $formwidscript;

}

?>

0 Comments:

Post a Comment

<< Home