Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-05-2013, 12:59 PM   PM User | #1
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
Post Contact Form Problem

Hello people ,

First i have to tell you that my english is not perfect , but i hope you will understand me.

I got an problem with A Mail form on my website , I downloaded an template that have e mail form in contact page , but i dont know where to input my e-mail as owner who will recieve e mails that people send me from this form.

And normaly after pressing send there should come an message if it was sucessfully sent or not .

Here are the codes , and tell me how to fix it . How to bring this form working normaly how it should.

______________________________

I got 2 files named " MailHandler.php " and " MailHandler.ashx " . This Files are located in an folder named " bin " .

MailHandler.php code :

PHP Code:
<?php
    $owner_email 
$_POST["owner_email"];
    
$headers 'From:' $_POST["email"];
    
$subject 'A message from your site visitor ' $_POST["name"];
    
$messageBody "";
    
    
$messageBody .= '<p>Visitor: ' $_POST["name"] . '</p>' "\n";
    
$messageBody .= '<br>' "\n";
    
$messageBody .= '<p>Email Address: ' $_POST['email'] . '</p>' "\n";
    
$messageBody .= '<br>' "\n";
    if(
$_POST['state']!='nope'){        
        
$messageBody .= '<p>State: ' $_POST['state'] . '</p>' "\n";
        
$messageBody .= '<br>' "\n";
    }
    if(
$_POST['phone']!='nope'){        
        
$messageBody .= '<p>Phone Number: ' $_POST['phone'] . '</p>' "\n";
        
$messageBody .= '<br>' "\n";
    }    
    if(
$_POST['fax']!='nope'){        
        
$messageBody .= '<p>Fax Number: ' $_POST['fax'] . '</p>' "\n";
        
$messageBody .= '<br>' "\n";
    }
    
$messageBody .= '<p>Message: ' $_POST['message'] . '</p>' "\n";
    
    if(
$_POST["stripHTML"] == 'true'){
        
$messageBody strip_tags($messageBody);
    }

    try{
        if(!
mail($owner_email$subject$messageBody$headers)){
            throw new 
Exception('mail failed');
        }else{
            echo 
'mail sent';
        }
    }catch(
Exception $e){
        echo 
$e->getMessage() ."\n";
    }
?>
MailHandler.ashx code :

Code:
<%@ WebHandler Language="C#" Class="Handler" Debug="true" %>

using System;
using System.Web;
using System.Net.Mail;
using System.Text.RegularExpressions;

public class Handler : IHttpHandler {
	public void ProcessRequest (HttpContext context) {
		SmtpClient mailClient = new SmtpClient(context.Request.Form.Get("smtpMailServer"));
		string owner_email = context.Request.Form.Get("owner_email");
		string subject = "A message from your site visitor " + context.Request.Form.Get("name");
		string email = context.Request.Form.Get("email");
		string messageBody = "";
	
		messageBody += "<p>Visitor: " + context.Request.Form.Get("name") + "</p>\n";
		messageBody += "<br>\n";
		messageBody += "<p>Email Address: " + context.Request.Form.Get("email") + "</p>\n";
		messageBody += "<br>\n";
		messageBody += "<p>Phone Number: " + context.Request.Form.Get("phone") + "</p>\n";
		messageBody += "<br>\n";
		messageBody += "<p>Message: " + context.Request.Form.Get("message") + "</p>\n";
	
			
		MailMessage message = new MailMessage();
	
		try{
			message.From = new MailAddress(email.ToString());
		}catch (FormatException e) {
			context.Response.Write(e.Message);
		}
	
		message.To.Add(owner_email);
		message.Subject = subject;
		if(context.Request.Form.Get("stripHTML") == "true"){
			message.IsBodyHtml = false;
            messageBody = Regex.Replace(messageBody, "<.*?>", string.Empty);
		}else{
		  	message.IsBodyHtml = true;
		}
		message.Body = messageBody;
		
		try{
			mailClient.Send(message);
		}catch (SmtpException e) {
			context.Response.Write("mail failed");
		}
		context.Response.Write("mail sent");
	}

	public bool IsReusable {
		get	{
			return false;
		}
	}
}
And in my contact.html is this form code :

Code:
<form id="contact-form">
								<fieldset>
									<label>
										<input type="text" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
									</label>
									<label>
									  <input type="text" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
									</label>
									<label>
									  <input type="text" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
									</label>
									<textarea onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
									<div class="buttons-wrapper">
										<a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
										<a class="button" onClick="document.getElementById('contact-form').submit()">Send</a>
									</div>
								</fieldset>
							</form>
Thats all , can anyone tell me please how to make it working correctly and where to put my e mail which should reciee all messages sent from this form ?

Thank you very much people!
HQWebs is offline   Reply With Quote
Old 01-05-2013, 04:49 PM   PM User | #2
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
You just need to insert the value name the contact form like this name="yorname"

simple mail function with php

PHP Code:
<?php
$to 
"cyberoot1@gmail.com";
$subject $_POST['subject'];
$message $_POST['message'];
$from =$_POST['youremail'];
$headers "From:" $from;
mail($to,$subject,$message,$headers);
echo 
"Mail Sent.";
?>
Quote:
<form id="contact-form">
<fieldset>
<label>
<input type="text" name = "typeyourname" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
</label>
<label>
<input type="text" name = "typeyourname"value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
</label>
<label>
<input type="text" name = "typeyourname"value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
</label>
<textarea name = "typeyourname"onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
<div class="buttons-wrapper">
<a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
<a class="button" onClick="document.getElementById('contact-form').submit()">Send</a>
</div>
</fieldset>
</form>
__________________
Quote:
Myanmar Web Developer & Designer
http://www.cyberoot.com
minkoko is offline   Reply With Quote
Old 01-05-2013, 05:00 PM   PM User | #3
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
Aham , Can you tell me where should I add this php code directly ?
Can you give me the right codes ?
And I dont understand what means this red words
Quote:
name = "typeyourname"
So This e-mail should recieve messages from this form > " hqwebs.eu@gmail.com "

Thank you very much
HQWebs is offline   Reply With Quote
Old 01-05-2013, 05:24 PM   PM User | #4
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
simple mail function with you contact form

PHP Code:
<form id=" action="" method="post" contact-form">
<
fieldset>
<
label>
<
input type="text" name "name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
</
label>
<
label>
<
input type="text" name "email"value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
</
label>
<
label>
<
input type="text" name "phone"value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
</
label>
<
textarea name "message"onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
<
div class="buttons-wrapper">
<
class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
<
class="button" onClick="document.getElementById('contact-form').submit()">Send</a>
</
div>
</
fieldset>
</
form

Code:
 <?php
$to = "hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone = $_POST['phone'];
$email = $_POST ['email'];
$message = $_POST['message'];
$headers = "From:" . $email;
mail($to,$name,$message,$headers);

if(mail){
echo "Mail Sent.";
}else{
    echo "Cannot Send your mail for error";
}
?>
__________________
Quote:
Myanmar Web Developer & Designer
http://www.cyberoot.com
minkoko is offline   Reply With Quote
Users who have thanked minkoko for this post:
HQWebs (01-05-2013)
Old 01-05-2013, 08:00 PM   PM User | #5
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
Okay , I fixed that now , its working fine .
But now i have another problem ...

This Phone field is not working

I become message with e mail , name and text message but there is not phone number ..

How to fix that ?

Thank you very much.
HQWebs is offline   Reply With Quote
Old 01-05-2013, 09:46 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
email injection

The following is not very effective in this regard:

PHP Code:
if($_POST["stripHTML"] == 'true'){ 
        
$messageBody strip_tags($messageBody); 
    } 
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 01-05-2013, 09:57 PM   PM User | #7
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
Quote:
Originally Posted by AndrewGSW View Post
email injection

The following is not very effective in this regard:

PHP Code:
if($_POST["stripHTML"] == 'true'){ 
        
$messageBody strip_tags($messageBody); 
    } 
I am not using this code anymore , I am using this code now :

PHP Code:
<?php header("Refresh: 5;url=http://www.hqwebs.eu/contacts.html"); 
$to "hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone $_POST['phone'];
$email $_POST ['email'];
$message $_POST['message'];
$headers "From:" $email;
mail($to,$name,$message,$headers);


if(
mail){
echo 
"<html>";
echo 
"<head>";
echo 
"<title>HQWebs - Mail Sent</title>";
echo 
"</head>";
echo 
"<body bgcolor='#000000'>";
    echo 
"<center><img src='http://www.hqwebs.eu/images/mailsent.jpg'></center>";
echo 
"</body>";
echo 
"</html>";
}else{
    echo 
"Cannot Send your mail for error";
}
?>
But This Phone field is not working , when you send an message i got , e mail , name , text message from sender , but there is not the phone number , what should i do to fix that too ?

Thank you
HQWebs is offline   Reply With Quote
Old 01-06-2013, 01:03 AM   PM User | #8
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
PHP Code:
<?php
$to 
"hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone $_POST['phone'];
$email $_POST ['email'];
$message $_POST['message'];
$headers "From:" $email "Phone Numbers is :" $phone ;
mail($to,$name,$message,$headers);

if(
mail){
echo 
"Mail Sent.";
}else{
    echo 
"Cannot Send your mail for error";
}
?>
__________________
Quote:
Myanmar Web Developer & Designer
http://www.cyberoot.com
minkoko is offline   Reply With Quote
Old 01-06-2013, 03:16 AM   PM User | #9
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
minkoko , this is an good solution , but there is an little problem.

Look at this Image :

http://s1239.beta.photobucket.com/us...5a979.jpg.html

Its telling me " Numbers is :0038766336703 <fightingforlove@greenmail.netphone> "
And it should be " Phone Number is : 0038766336703 <fightingforlove@greenmail.net> "

Can you fix this code please ? And thank you very much for spending time on my problem!
HQWebs is offline   Reply With Quote
Old 01-06-2013, 08:31 AM   PM User | #10
minkoko
New Coder

 
Join Date: Aug 2010
Location: myeik
Posts: 72
Thanks: 4
Thanked 5 Times in 5 Posts
minkoko can only hope to improve
PHP Code:
<?php
if(isset($_POST['send'])){

$to "hqwebs.eu@gmail.com";
$name=$_POST['name'];
$phone $_POST['phone'];
$email $_POST ['email'];
$message $_POST['message']. "\n\n Phone Number is " .$phone;
$headers "From:" $email  ;
mail($to,$name,$message,$headers);

if(
mail){
echo 
"Mail Sent.";
}else{
    echo 
"Cannot Send your mail for error";
}
}
?> 
<form id=" action="email" method="post" contact-form">
<fieldset>
<label>
<input type="text" name = "name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" />
</label>
<label>
<input type="text" name = "email"vvalue="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />
</label>
<label>
<input type="text" name = "phone" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" />
</label>
<textarea name = "message" onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>
<div class="buttons-wrapper">
<a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
<input type="submit" name="send" onClick="document.getElementById('contact-form').submit()" value="Send"></a>
</div>
</fieldset>
</form>
__________________
Quote:
Myanmar Web Developer & Designer
http://www.cyberoot.com
minkoko is offline   Reply With Quote
Users who have thanked minkoko for this post:
HQWebs (01-11-2013)
Old 01-11-2013, 04:33 AM   PM User | #11
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
Okay , now i got what i wanted.
Thank you very much !
HQWebs is offline   Reply With Quote
Old 01-28-2013, 06:55 AM   PM User | #12
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
Hmm , can anyone tell me how to add option "Required" to some fields ?
So when this required field in not filled then the message wont be sent , and he will get this message which is placed in this php " Cannot Send your mail for error " .



And how can I add drop down list option ?

Thank you very much !
HQWebs is offline   Reply With Quote
Old 01-28-2013, 03:37 PM   PM User | #13
ckirkster
New to the CF scene

 
Join Date: Jan 2013
Location: UK
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
ckirkster is an unknown quantity at this point
Try the following code. I've cleaned up yours a bit to make it easier to read.
It's untested but simple enough that it should work.
I've commented it to help you

PHP Code:
<!DOCTYPE html>
<html>
<?php
$fields 
= array('message','email');  // required fields go in here
$errors=false;
if(isset(
$_POST['send']))
    {
        
array_walk($_POST'cleanMe'); // Clean up the input
        
$to "hqwebs.eu@gmail.com";
        
$name=$_POST['name'];
        
$phone $_POST['phone'];
        
$email $_POST ['email'];
        
$message $_POST['message']. "\n\n Phone Number is " .$phone;
        
$headers "From:" $email  ;
      
       
/**
        * Checks fields to see if they are empty
        */
        
foreach($fields as $field
        {
            if( 
strlen($_POST[$field]) == 
            {
                
$errors true;
                break;
            }
        }  
        
        if(!
$errors// If none of the required fields produced an error then try to email
        
{
            
mail($to,$name,$message,$headers);

            if(
mail)
            {
                echo 
"Mail Sent.";
            }
            else
            {
                echo 
"Cannot Send your mail for error";
            }
        }
        else 
// One or more of the required fields wasn't filled in
        
{
            echo 
'Fields marked with a * are required';
        }            
        
        
    }
/**
 * 
 * Clean up input from the user
 * @param type $data
 * @return string
 */
function cleanMe($data)
    {
        
$data trim($data);
        
$data stripslashes($data);
        
$data htmlspecialchars($dataENT_QUOTES);
        return 
$data;
    }        
?> 
    <form id="" method="post" contact-form">
        <fieldset>
            <label>Name</label>
            <input type="text" name = "name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''" /><br />
            
            <label>Email</label>
            <input type="text" name = "email"vvalue="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''" />*<br />
            
            <label>Phone</label>
            <input type="text" name = "phone" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''" /><br />
            
            <textarea name = "message" onBlur="if(this.value=='') this.value='Message'" onFocus="if(this.value =='Message' ) this.value=''">Message</textarea>*<br />
            <div class="buttons-wrapper">
                <a class="button" onClick="document.getElementById('contact-form').reset()">Clear</a>
                <input type="submit" name="send" onClick="document.getElementById('contact-form').submit()" value="Send"></a>
            </div>
        </fieldset>
    </form>
</html>
Hope that helps

Last edited by ckirkster; 01-28-2013 at 03:40 PM..
ckirkster is offline   Reply With Quote
Old 01-28-2013, 08:24 PM   PM User | #14
HQWebs
New Coder

 
Join Date: Jan 2013
Location: Bosnia And Herzegowina
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
HQWebs is an unknown quantity at this point
Hey CkirSter , thank you for spending your time on my problem , but this codes are not working correctly .

After filling all fields and press send button , nothing happens , and i dont become the e mail , and there is not coming the message if it was sent sucessfully or not . Its not working :S
Can you take a look on this codes and find where is problem ? Thank you very much.
HQWebs is offline   Reply With Quote
Old 01-28-2013, 09:05 PM   PM User | #15
ckirkster
New to the CF scene

 
Join Date: Jan 2013
Location: UK
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
ckirkster is an unknown quantity at this point
I might be confusing you a little.
I was using minkoko's last code as an example. I also never tested it but can see at second glance the

PHP Code:
mail($to,$name,$message,$headers);
  if(
mail)
 {
     echo 
"Mail Sent.";
 }
 else
 {
     echo 
"Cannot Send your mail for error";
 } 
should read
PHP Code:
  if(mail($to,$name,$message,$headers))
 {
     echo 
"Mail Sent.";
 }
 else
 {
     echo 
"Cannot Send your mail for error";
 } 

The code I added to it is there to give you a simple example of setting errors and stopping the script calling the "mail" function if there are errors.

That should help point you in the right direction
ckirkster is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 +1. The time now is 07:02 PM.


Advertisement
Log in to turn off these ads.