PAWDD E-Training
2Oct/090

PHP Email Form Script

This is a PHP script that you can use to place a form on any webpage that can parse PHP code. This PHP code snippet will allow you're users to email you from your web page. All you need to do is place the following XHTML and PHP code onto a page with the ".php" extension; otherwise it will not work as intended.

Paste the PHP code onto your page first. Then paste the XHTML onto your page below the PHP. You will need to change all instances of the email address to your own email; otherwise you will never see any email arrive into your inbox.

If you have any questions please post them and I will gladly point you into the right direction.

The PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
	<?php 
 
	date_default_timezone_set('America/Kentucky/Louisville'); // you may want to change this to your own timezone if you do not live in the EST timezone
 
	// Check for form submission:
	if (isset($_POST['submitted'])) {
 
		// Minimal form validation:
		if (!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['comments']) ) {
 
			// Create the body:
			$body = "Name: {$_POST['name']}\n\nComments: {$_POST['comments']}";
 
			// Make it no longer than 70 characters long:
			$body = wordwrap($body, 70);
 
			// Send the email:
			mail('your.email@yourdomain.com', 'Website Email Form Submission', $body, "From: {$_POST['email']}");
 
			// Print a message:
			echo '<p><strong><em>Thank you for contacting me at ' . date('g:i a') . ' ' . 'EST' . ' ' . 'on ' . date('l F j, Y') .'. I will reply ASAP.</em></strong></p>';
 
			// Clear $_POST (so that the form's not sticky):
			$_POST = array();
 
		} else {
			echo '<p style="font-weight: bold; color: #C00">Please fill out the form completely.</p>';
		}
 
	} // End of main isset() IF.
 
	// Create the HTML form:
	?>

The XHTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
<div id="email_page">
<!-- Fill in the action inside the quotes if you would like to use a seperate file to parse your file output.  
By default this is blank because the PHP code that handles the form output and response back to your visitors is in built into this code... -->
<form action=" " method="post"> 
	<p>Please fill out and submit this form to contact <em>Your Name</em> via e-mail at <a href="mailto:your.email@yourdomain.com" title="Email Me" title="Email Me">your.email@yourdomain.com</a></p>
	<p>Name: 
	<input type="text" name="name" size="42" maxlength="60" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
	</p>
<p>Email Address: <input type="text" name="email" size="34" maxlength="80" value="<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
	<p>Comments: <textarea name="comments" rows="5" cols="30"><?php if (isset($_POST['comments'])) echo $_POST['comments']; ?></textarea></p>
	<p><input type="submit" name="submit" value=" Submit E&#8211;mail "  title="Click Here to Submit Your E-mail to Me"/></p>
	<p><input type="hidden" name="submitted" value="TRUE" /></p>
</form>
</div>
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

You must be logged in to post a comment.

No trackbacks yet.

Dashboard

Training Categories

Shaded Pixel LLC

Monthly Archive

Page Links

PAWDD E-Training