View Single Post
Old 05-11-2015, 05:03 AM   #1
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,174
Default send a mail from your website

here is a simple form that will send a mail to you from a page on your web site.
First a form using some html5 validation that will show errors when you submit.
I have made some fields required, email and url will be validated
the action is set to send.php where the mail sending script will reside.
Code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Contact</title>
<style>
#form{
 position:relative;
 width: 500px;
 margin:0 auto;
}
 label {
     float:left;
    clear:left;
}
 input[type="text"], [type="url"], [type="email"]{
	 float:left;
    color: #555 ;
    width: 70%;
    border: 1px solid #e5e5e5;
    background: #fbfbfb;
    height: 25px;
}
 .textarea{
	  float:left;
    height:100px;
      width: 70%;
}

</style>
</head>
<body>
<div id="form">
<form action="send.php" method="post">     
   <fieldset>
            <label for="name">Name:</label>
            <input id="name" name="name" type="text" required 
            placeholder=" Your name"   >
          </fieldset>
         <fieldset>       
            <label >Email:</label>
            <input type="email" id="email" name="email" placeholder="me@mymail.com" required >
                    </fieldset>
         <fieldset>  
   
            <label>Website:</label>
            <input type="url" id="website" name="website"  placeholder=" http://mysite.com" required >
                    </fieldset>
         <fieldset>  
            <label>Message</label>
            <textarea class="textarea" name="Message" id="Message"  ></textarea>     
           </fieldset>
         <fieldset>  
            <input type="submit" name="Submit" value="Submit">    <input name="reset" type="reset">
           </fieldset>    
</form> 
 </div>     
</body>
</html>
now to send this php is required. the below script will firstly check that included in the $_POST array is $_POST[' Submit '] this has been named as the submit button in your form.
there is a var named $th_url that is the url redirect that the visitor will be directed to after a successful submission, with no errors.
send.php
Code:
<?php
 
    if (isset($_POST['Submit']) && $_POST['name'] != "") {		
		$th_url= "thankyou.html";   // modify to page redirect after mail succsessfully sent
 
        if ($_POST['name'] != "") {
            $_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
            if ($_POST['name'] == "") {
                $errors .= 'Please enter a valid name.<br/><br/>';
            }
        } else {
            $errors .= 'Please enter your name.<br/>';
        }
 
        if ($_POST['email'] != "") {
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
            }
        } else {
            $errors .= 'Please enter your email address.<br/>';
        }
		
	/*	Validating  web address in form field  **********************************************     */
		
		   if ($_POST['website'] != "") {
            $website = filter_var($_POST['website'], FILTER_SANITIZE_URL);
            if (!filter_var($website, FILTER_VALIDATE_URL)) {
                $errors .= "$website is <strong>NOT</strong> a valid URL.<br/><br/>";
            }
        } else {
            $errors .= 'Please enter your home page.<br/>';
        }
	/*	 end Validating  web address in form field  **********************************************     */	
 
      
        if ($_POST['Message'] != "") {
            $_POST['Message'] = filter_var($_POST['Message'], FILTER_SANITIZE_STRING);
            if ($_POST['Message'] == "") {
                $errors .= 'Please enter a message to send.<br/>';
            }
        } else {
            $errors .= 'Please enter a message to send.<br/>';
        }
 
        if (!$errors) {
            $to = 'me@somewhere.com';
            $subject = 'New Mail from  ' . $_POST['email'];
            $message  = 'From: ' . $_POST['name'] . "\n";
            $message .= 'Email from: ' . $_POST['email'] . "\n";
            $message .= "Message:\n" . $_POST['Message'] . "\n\n";
            mail($to, $subject, $message);
 
          
    header("Location: $th_url");
			
        } else {
            echo '<div style="color: red">' . $errors . '<br/></div>';
        }
    }
?>
as you can see the data is validated server side . this is the true validation as html5 or java script is only 'helper' validation not secure validation.

i intend to extend this with some more examples as anti spam , time permitting
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas

Last edited by edbr; 05-11-2015 at 08:41 AM..
edbr is offline   Reply With Quote