www.webdeveloper.com
Results 1 to 10 of 10

Thread: PHP Mail form

  1. #1
    Join Date
    Jun 2013
    Posts
    2

    PHP Mail form

    Hi, I am new to using PHP (in fact its my very first time) and I have to finish a contact form for class. We have to make a form that includes name, email, birthday, and a message box. When it submits, the information has to be sent to both me and the user, and the page displays a thank you message.

    Here is what I have so far:
    Code:
    <form method="post" action="contact.php"> 
    Name: <input name="name" type="text"><br>
    Birthday: <input name="birthday" type="text"><br>
     Email: <input name="email" type="text"><br>
     Message:<br> 
     <textarea name="message" rows="15" cols="40"></textarea><br> 
     <input type="submit"> 
     </form> 
    <?php 
     $to = "myemail@provider.com"; 
     $subject = "Contact Us"; 
    $birthday = $_REQUEST ['birthday'] ;
    $name = $_REQUEST['name'] ;
     $email = $_REQUEST['email'] ; 
     $message = $_REQUEST['message'] ; 
     $headers = "From: $email"; 
     $sent = mail($to, $subject, $message, $headers) ; 
     if($sent) 
     {print "Your mail was sent successfully"; }
     else 
     {print "We encountered an error sending your mail"; }
     ?>
    I am wondering how to get the code to also email to the recipient, myself, and display the thank you message at the end. Every time I hit submit I get a 404 error. What am I doing wrong and how can I fix this?

  2. #2
    Join Date
    Jun 2013
    Posts
    1
    look in to your form action
    <form method="post" action="contact.php">
    you are submitting your form to contact.php
    is your file named contact.php?

  3. #3
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    since you have the form submitting to its self. meaning the same page that the php is on remove the contact.php in action and just leave it blank like this action=""> start with that see if that helps

  4. #4
    Join Date
    Jun 2013
    Posts
    2
    Quote Originally Posted by Nicholas Diaz View Post
    since you have the form submitting to its self. meaning the same page that the php is on remove the contact.php in action and just leave it blank like this action=""> start with that see if that helps
    Ok, that got rid of the 404 error. So then I tested out the form several times but it only sent one notification to me. Sadly it just sent the notification, subject, and time, but not any of the information I need such as birthdate, sender's name and sender's email address.

  5. #5
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    ok good. that means we are narrowing down the problems. so we need to look at the code again now. I like to keep my php separate from my forms personally but. let me add it to my text editor and see if i can make some changes.

  6. #6
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    ok your all fixed up two files. try to always keep your php separate from your forms.

    the only thing you could change is the variable called $to change it to the email address this form is supposed to send to. as far as for the 2nd email it will just send to what ever someone types in to the form.

    index.php is your form file
    Code:
     <form method="POST" action="send.php"> 
    Name: <input name="name" type="text"><br>
    Birthday: <input name="birthday" type="text"><br>
     Email: <input name="email" type="text"><br>
     Message:<br> 
     <textarea name="message" rows="15" cols="40"></textarea><br> 
     <input type="submit"> 
     </form>
    send.php is your php script
    Code:
       
     <?php 
     sleep(2);
    //Sanitize incoming data and store in variable
    $name = trim(stripslashes(htmlspecialchars($_POST['name'])));
    $birthday = trim(stripslashes(htmlspecialchars($_POST['birthday'])));
    $email = trim(stripslashes(htmlspecialchars($_POST['email'])));
    $message = trim(stripslashes(htmlspecialchars($_POST['message'])));
    
    
    //Prepare information from form to be sent
    $to = 'CHANGETHISEMAIL@test.com';
    $from = 'admin@test.com';
    $headers = 'MIME-VERSION: 1.0' . '\n';
    $headers .= 'From: $from' . '\n';
    $subject = 'Contact Form Submission';
    $body = 'Name: ' .$name . PHP_EOL;
    $body .= 'Birthday: ' .$birthday . PHP_EOL;
    $body .= 'Email: ' .$email . PHP_EOL;
    $body .= 'Message: ' .$message . PHP_EOL;
    
    // Form data was successfull so we will now send admin email and return message to the user
    $success = (mail($to, $subject, $body, $headers, '-f admin@test.com.com')); 
    	echo 'Thank you!';				  
    	
    	if ($success) (mail($email, $subject, $body, $headers, '-f admin@test.com.com'));	
    	
    	else 
    	echo 'Woops! Something went wrong please contact us directly';
    		
    
    ?>

  7. #7
    Join Date
    Jun 2013
    Posts
    2

    In Single File

    kaida07,

    Here is a more elegant display with processing and display in a single file:
    Code:
    <?php 
    	//sleep(2);
    	define ('SHOW_ME',0);				// Declare Debug value
    	if (SHOW_ME==1) {
    		foreach ($_POST as $k => $v) {
    			echo "K=> $k V=> $v <br>";
    		}	// end foreach $_POST
    	}	// end if SHOW_ME
    	$name				=	"";
    	$birthday		=	"";
    	//$email			=	"";
    	$message			=	"";
    	$to				=	"";
    	$from				=	"";
    	$subject			=	"";
    	$headers			=	"";
    	$body				=	"";
    	if (isset($_POST['submit'])) {
    		$name			=	trim(stripslashes(htmlspecialchars($_POST['name'])));
    		$birthday	=	trim(stripslashes(htmlspecialchars($_POST['birthday'])));
    		//$email		=	trim(stripslashes(htmlspecialchars($_POST['email'])));
    		$message		=	trim(stripslashes(htmlspecialchars($_POST['message'])));
    		$to			=	trim(stripslashes(htmlspecialchars($_POST['to'])));
    		$from			=	trim(stripslashes(htmlspecialchars($_POST['from'])));
    		$subject		=	trim(stripslashes(htmlspecialchars($_POST['subject'])));
    		$headers		=	'MIME-VERSION: 1.0' . '\n';
    		$headers	  .=	'From: $from' . '\n';
    		$body			=	'Name: ' .$name . PHP_EOL;
    		$body		  .=	'Birthday: ' .$birthday . PHP_EOL;
    		$body		  .=	'Email: ' .$to . PHP_EOL;
    		$body		  .=	'Message: ' .$message . PHP_EOL;
    		if (SHOW_ME==1) {
    			echo			"T=> $to F=> $from S=> $subject H=> $headers <br>".
    							"M=> $message <p>";
    		}	// end if SHOW_ME
    		$checked		=	find_blanks();
    		if ($checked<>'') { echo $checked; exit; }
    		echo "Attempting to Mail your message.  There may be a delay. <br>";
    		if (!(mail($to, $subject, $body, $headers, "-f $from"))) {
    			echo 'Woops! Something went wrong please contact us directly';
    			//(mail($to, $subject, $body, $headers, "-f $from"));
    		} else {
    			echo 'Message Sent! Thank you! <br>';	  
    		}	// end if $success
    	}	// end if isset $_POST['submit']
    	
    	function find_blanks () {
    		foreach ($_POST as $k => $v) {
    			if ($k=='submit') { continue; }
    			if ($v=='') {
    				return 	"All fields must have values! Your =>$k<= field is blank! <br>".
    							"Fill in all fields and try again! <br>";
    			}	// end $v
    		}	// end foreach $_POST
    	}	// end function find_blanks
    	
    ?>		
    
    <html>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
    	<table border='1' cellspacing='0' cellpadding='5' width='50%' 
    			align='center' bgcolor='grey'>
    		<tr><td align='center' width='100%'>
    		<table border='0' cellspacing='0' cellpadding='0' width='100%' 
    				bgcolor='#dddddd' align='center'>
    			<tr>
    				<td align='center' colspan='2'>
    					<h1>User Email Form</h1>
    				</td>
    			</tr>
    
    			<tr>
    				<td align='right'>To:&nbsp;&nbsp;</td>
    				<td align='left'><input name="to" type="text"></td>
    			</tr>
    		
    			<tr>
    				<td align='right'>From:&nbsp;&nbsp;</td>
    				<td align='left'><input name="from" type="text"></td>
    			</tr>
    		
    			<tr>
    				<td align='right'>Subject:&nbsp;&nbsp;</td>
    				<td align='left'><input name="subject" type="text"></td>
    			</tr>
    		
    			<tr>
    				<td align='right'>Name:&nbsp;&nbsp;</td>
    				<td align='left'><input name="name" type="text"></td>
    			</tr>
    		
    			<tr>
    				<td align='right'>Birthday:&nbsp;&nbsp;</td>
    				<td align='left'><input name="birthday" type="text"></td>
    			</tr>
    		
    			<!--tr>
    				<td align='right'>Email:&nbsp;&nbsp;</td>
    				<td align='left'><input name="email" type="text"></td>
    			</tr-->
    		
    			<tr>
    				<td align='right' valign='top'>Message:&nbsp;&nbsp;</td>
    				<td align='left'><textarea name="message" rows="15" cols="40"></textarea></td>
    			</tr>
    		
    			<tr>
    				<td align='center' colspan='2'>
    					<input type="submit" name='submit' value='Submit'>
    				</td>
    			</tr>
    		</table>
    		</td></tr>
    	</table>
    </form> 
    
    </html>
    Was not able to get your "mail" command to run, as must be from a plugin you installed, but the rest works fine.

    Cheers!

    OMR

  8. #8
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    my code was working fine what problem did you have with it>
    Last edited by Nicholas Diaz; 06-21-2013 at 10:47 PM.

  9. #9
    Join Date
    Jun 2013
    Posts
    2
    ND,

    1. You were hard coding many of your vars that should be entered on the form,
    2. You were using 2 files to get this done when only one is needed.

    That's all

    Cheers!

    OMR

  10. #10
    Join Date
    Aug 2012
    Location
    TX
    Posts
    293
    I appreciate your feed back.
    But I am not the one asking a question on this thread. I am providing help to a person who had a question that has been answered for days now. as for putting php and html in the same file it should be avoided in most cases due to security risks. a few examples being when connecting to a database, dealing with a page controlled by a session, or when your passing personal information that should be secure through the server. At the end of the day to each his own and I think its important to see many different examples on how to accomplish any task. The more examples people are given when asking a question on a forum the better, because it allows them to have a better idea of what they are doing. its hard to learn for some people because there are so many ways to accomplish the same task.

    I make a point to answer as many questions as I can because I to have questions and i appreciate when people answer my questions promptly.

    I personally like my files to be organized easy to read and secure. So I make a point to avoid mixing php and html unless there is a reason for it.

    I think for the sake of being helpful to the person who started this thread, it would be more productive to give them an example of the way you would accomplish
    this task instead of telling me you don't like the way I structure my code. unfortunately all that does is leave confusion for someone who comes along looking for
    an answer on this thread and the last post they see is your post telling me what you feel I did wrong with out giving an example of how you feel the correct way of doing this was.

    I am sure you have your own style as I have mine, and I am sure it will be helpful for people to see because in some cases some people might find my code was easier for them to understand
    and in other cases people may relate better to the way you do things.

    Here is a reference to some common mistakes done with php.
    http://www.sitepoint.com/php-security-blunders/

    I realize this is an email form but just recently I was working on a form with a person who wanted to keep the email address private. That means it was necessary to keep the php in a separate file. So i try to keep good habits instead of picking and choosing when to make a simple script less secure just for the sake of not feeling like its necessary to be more secure on this one.

    Hope to see an example from you so we can add some more content to the site. The More examples more questions get answered.

    Also if you are seeing something in my script that is wrong please elaborate. I am here to learn and appreciate any chance to learn something new.
    Last edited by Nicholas Diaz; 06-27-2013 at 12:39 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles