logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
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
Old 05-11-2015, 08:11 AM   #2
d a v e
 
Join Date: Feb 2006
Location: finland
Posts: 1,684
Default

Ed - just trying this out but feel i'm missing something really obvious...
tried it locally in mamp and it allegedly sent, got to the thank you page but it simply doesn't arrive at my email. uploaded it to http://gekkoweb.com/hl2015/form.html and still no joy.

i removed the web site field from the form and the send.php
i used my email address and plugged it in here:

if (!$errors) {
$mail_to = 'gekkoweb@gmail.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);


I suspect that this is where i'm going wrong
"the action is set to send.php where the mail sending script will reside" - so my cgi bin or ???

this is really useful because for a while now i've been looking for a simple form like this but there's so much crud on the net finding something decent and made in the last 10 years also i was a bit embarassed to ask before...
d a v e is offline   Reply With Quote
Old 05-11-2015, 08:28 AM   #3
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,174
Default

id better test it, ime prone to rushinhg usingh a local setup you need a real stmp server . i use a tool called test sender from http://www.toolheap.com/test-mail-server-tool/. it saves the email a s a file . not sure of a apple or linux version though.
As for path the setup is for the form p[age and mailsending form to be in the same directory, another opyion is toset the action to itself action=" " and paste the php code at the page starht before the html
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 05-11-2015, 08:36 AM   #4
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,174
Default

yup its me

$mail_to = 'me@somewhere.com';
should read
$to = 'me@somewhere.com';
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 05-11-2015, 08:43 AM   #5
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,174
Default

I just edited it and tested live and all is ok. Sorry about that .and Dave feel free to emaiol me if you are embarassed though there is no need to be
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 05-11-2015, 08:58 AM   #6
d a v e
 
Join Date: Feb 2006
Location: finland
Posts: 1,684
Default

yep all works now, thanks.

embarassed because i've been doing this a long time and i should know how to do it by now, though i've never learnt php...

one more quick qestion for my benefit and anyone else using this thread: where you have

// modify to page redirect after mail succsessfully sent

after taking the user to the thankyou page, could you detail the redirect bit for us? or is it a better to just provide the user with a normal link back to the homepage?

look froward to the antispam
d a v e is offline   Reply With Quote
Old 05-11-2015, 09:02 AM   #7
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,174
Default

i would say that depends. f'rinstance, on a job site i redirect back to the new jobs on others a thank you page with various link options, or sometimes the index page
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 05-11-2015, 09:05 AM   #8
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,174
Default

i will post a version with html and an anti spam 'honey trap' tomorrow
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Old 05-11-2015, 09:35 AM   #9
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,174
Default

ok this adds a couple of things to include a honey-pot or honey trap.
in essence, it is a form that should not get filled in. if your site is being spammed by a bot it will with luck fill in the field. the receiving php script will not allow the mail then to be sent. it is pretty effective , although not foolproof but then i have not found any way that is 100% effective, even captchas which can be used as well as a honey trap.

ok the form adds a field, in this case i have called it myrealname .
I have also added a css class that will not allow the input to be shown. This can be removed or commented out while testing if you want
.robotic { display: none; }

so the code
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%;
}
.robotic { display: none; }
</style>
</head>
<body>
<div id="form">
<form action="" 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 class="robotic" type="text" name="myrealname" id="myrealname">
            <input type="submit" name="Submit" value="Submit">    <input name="reset" type="reset">
           </fieldset>
    
</form> 
 </div>     
</body>
</html>
now the php will be modified to include the part that exits the script after telling the spammer to naff off!

Code:
<?php
  $machine = $_POST['myrealname'];
    if ($machine != "")
    { echo 'naff off spammer';
        exit(); //if a spambot filled out the "machine"
                //field, we don't proceed
    }
        else{
 
 
    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@gmail.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>';
        }
    }
		}
?>
__________________
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-13-2015 at 02:09 AM..
edbr is offline   Reply With Quote
Old 05-11-2015, 10:54 AM   #10
d a v e
 
Join Date: Feb 2006
Location: finland
Posts: 1,684
Default

that's great ed, thanks loads. will have a look at that later today
d a v e is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:00 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com