PDA

View Full Version : PHP side of Mailforms


Justindukesdesign
12-21-2009, 02:19 PM
I have designed two mail forms in Dreamweaver and I need the PHP code to have it send to my email address. Anyone know where I can find some tutorials on coding this?

Justindukesdesign
12-21-2009, 02:39 PM
OK, I'd like to apologize for being a noob, I just found the exact tutuorial I needed on this website. sorry again

tux
12-22-2009, 06:21 PM
Justin,

Here is a script I use for sending form results. It may help.

<?php
///////////////////////////////////////////////declare your variables here from the form on the previous page
$submitted = $_POST['submitted'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$subject = $_POST['subject'];
// add or change these to suit yours
///////////////////////////////////////////////ensure required fields are filled in
if($submitted && $firstname && $lastname && $email && $comments && $subject){ // add or change these to suit yours
///////////////////////////////////////////////
$to = 'admin@paultuckerphotography.co.uk';//define the receiver of the email
$subject = '$subject';//define the subject of the email if you have it in your form
///////////////////////////////////////////////

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $email\r\nReply-To: $email";//put the 'From' & 'Reply To' email address here. Currently taken from variables
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for plain text email-->

First Name: <?php echo $firstname; ?>
Last Name: <?php echo $lastname; ?>
Email: <?php echo $email; ?>
Comments: <?php echo $comments; ?>

<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for HTML email-->

<p align="left" >First Name: <b><?php echo $firstname; ?></b><br>
Last Name: <b><?php echo $lastname; ?></b><br>
Email: <b><?php echo $email; ?></b><br>
Comments: <b><?php echo $comments; ?></b><br>

<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>--
<?php
//this copies current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
// if no errors redirect to success page
header("location:your success page here ");
exit;

}elseif($submitted){//if required fields not filled in redirect to error page
header("location:your error page here");
exit;
}


?>

Regards Paul

richcodesign
01-12-2010, 11:26 AM
Hey.... thanks for that code! worked a treat - just what i was after

tux
01-12-2010, 09:36 PM
No probs, glad to help.