Tutorial

Click on thumbnailed images to enlarge

CREATE TWO FILES

1) contact.php and mail.php
2) contact.php is where you design your mail form
3) mail.php is where you put all of your mail codes

THE SECTION BELOW IS YOUR FORM DESIGN. THIS ONE IS THE MOST BASIC FORM DESIGN, YOU CAN ADD MORE FIELDS AS YOU WISH :)

<form action="mail.php" method="post">
NAME <input type="textarea" name="sender" /><br />
* SUBJECT <input type="textarea" name="sender_subject" />

<br /><br />
* MESSAGE<br />
<textarea name="sender_message">
</textarea>

<br /><br />
<input type="submit" value="send" />
</form>


THE SECTION BELOW IS THE MAIL FUNCTION :)

<?php

# Bertoni Moretti @ bottie.org

$displaysendername = $_REQUEST['sender'];
$post_getsubject = $_REQUEST['sender_subject'];
$post_getmessage = $_REQUEST['sender_message'];

# the line below checks if either subject or message is not empty otherwise an error message is displayed

if($post_getsubject == "" || $post_getmessage == ""){

echo "Please fill in the required fields (marked *). Thank You."; # error message

}else{

$to_me = "your@email.com"; # your email address

$to_subject = $post_getsubject; # retrieves subject

$to_message = $post_getmessage; # retrieves messages

mail($to_me, $to_subject, "$to_message");

echo "Message has been sent :)";

}

?>


That's it! a single mail() function is used. If you need to add more fields, you make full use of the header parameter. E.g:

$header = "your_string";

mail($youremail, $subject, "$message plus other arrays as you wish", $header);


To call from an extra field:

<input type="text" name="extra_field" /> # the extra field to be included in contact.php

$getextrafield = $_REQUEST['extra_field']; # calls the content of extra_field


If you wish to display a fixed sender name for your designed form, this is how you implement the mail() function.

$fixedsendername = "Mailer at mywebsite.com";
$header = "From: $fixedsendername";

mail($youremail, $subject, "$message", $header);

To enable HTML tags for your senders be sure to include the following header commands.

$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


For even complex email, check out the PHP Manual at the following address:

http://my2.php.net/manual/en/function.mail.php

ENJOY! :)

Tutorial Comments

No comments yet. Be the first one to comment!

Tutorial Details

Author birdman View profile
Submitted on Mar 30, 2006
Page views 21,823
Favorites 5
Comments 0