FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Simple and Detailed Mail Script

 


DeuCe
Here is a detailed tutorial that many people ask about mail scripts. It will show you how to make a simple one.. NOTE: HTML knowledge is required and hardly any php knowledge.

Ok To start off, first you should make a page called mail.php and place this under a form field.

- Place some text boxes named Email, Subject, and text area named Message
- Place 2 Buttons called Submit and Reset (of course, submit button type will be "submit" and reset button will be "reset")

OK, so when the person fills out everything they will hit submit of course. So put this code in <form>.

Code:
<form method="POST" action="mail2.php"></form>


So what this does is take you to mail2.php when the user hits submit. So as you might've guessed, create a new file called mail2.php.

Ok on this is where the php code will be and error reporting. So what if the user did not fill out the email field? or the subject? or no message? As I said before this is where the error reporting comes in. So now type this where you want it to appear under the <body> field.

Code:
<?php
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Message = $_POST['Message'];

if($_POST['Email'] != "") {
   if($_POST['Subject'] !="") {
      if($_POST['Message'] != ""]) {
} else {


Ok if you know how IF condtions works you can skip down, if not then read on.

- The "$Email, $Subject, $Message" are called variables, which I will get to later.
- The $_POST is the method used on mail.php in <form method="POST">.
- $_POST['Email'], $_POST['Subject'], $_POST['Message'] = Email, Subject, and Message are the names you gave to the textboxes and Message to the textarea.
- != "" is an operator that simply means "is not" followed by 2 quotation marks "" which makes the statement mean "Email field is not blank", etc...
- The brackets " { " must have a space before them and after them as I typed it.
- Finally, the else command there, which will output the error if one of the fields are blank.

Onto the next section, how to show the errors. Add this code in to the above.

Code:
<?php
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Message = $_POST['Message'];

if($_POST['Email'] != "") {
   if($_POST['Subject'] !="") {
      if($_POST['Message'] != ""]) {
            } else {
                 echo "<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter A Message.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />";
                }
        } else {
                echo "<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter A Subject.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />";
            }
        } else {
            echo "<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter Your Email Address.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />";
        }
}


Ok so what this does here is output an error message(which you can change if you want) for one of the fields that were not filled.

- echo simply outputs a text. You must start echo with " and end it with a ". If your going to use html in it, you must do what I did and include a backslash for every quotation you put before you end the echo command. like this \"\". So a backslash before the quote begins and one more before it ends.
- You must include a " } " at the end of a echo command and at the end of the else statement as you see.
- Nothing else new here, lets move on. Smile

Now if the user did fill out everything correctly, lets mail the message to the email the user supplied and the subject he/she supplied. It's not hard to do this and will only take 1 line of code added to the above. Remember the variables I said I would get to? Well this is where they come in use. Add this code to the above.

Code:
<?php
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Message = $_POST['Message'];

if($_POST['Email'] != "") {
   if($_POST['Subject'] !="") {
      if($_POST['Message'] != ""]) {
   mail($Email, $Subject, $Message, "From: DeuCe<email@email.com>");
            } else {
                 echo "<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter A Message.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />";
                }
        } else {
                echo "<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter A Subject.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />";
            }
        } else {
            echo "<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter Your Email Address.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />";
}
?>


See wasn't so hard to add 1 line. Now to understand what you just added.

-The Variables come in handy because it makes less writing and more easy to read. Variables store things, in this case, the email, subject, and message.
- mail() is a command that emails a person to the address you or they supply. It has an order inside the () which you must follow if you want it to appear good...
- mail($Email) = The person you are emailing alwasy goes first in here
- mail($Email, $Subject) = The subject always goes after the email.
- mail($Email, $Subject, $Message) = The message always goes in third in the mail().
- mail($Email, $Subject, $Message, EXTRA) = You can add extra parameters to the mail(). In this case, as you see in italicized that I added an extra email. This extra email is the outgoing email, or the email the user will see when he/she recieves the message. You can change "DeuCe<email@email.com>" to whatever your email or name you want.

Well this is about it. Very simple and hopefully detailed enough for you to understand. Go ahead and test it out and tell me if there are any errors so I can fix it immediately.

Enjoy!
DeuCe Cool[/b]
topjeux
Good, very good
mustaq
well i paid for mail script before i dont know its so simple
SamiTheBerber
Why not just:
Code:
<?php
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Message = $_POST['Message'];

if(empty($Email)) die("<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter Your Email Address.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />");
if(empty($Subject)) die("<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter A Subject.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />");
if(empty($Message)) die("<font color=\"#FFFFFF\" face=\"Arial\" size=\"2\">Please Enter A Message.<br /><br />.: <a href=\"javascript: history.back()\">Go Back</a> :.</font><br /><br />");
mail($Email, $Subject, $Message, "From: DeuCe<email@email.com>");
?>


I prefer this, it is lighter and clear Wink
DeuCe
Thank you, my first tutorial. Laughing And I didn't do it the simple way because this is the only way I know how Laughing , and I dont use mail script that much, so that's why.

Cheers,
DeuCe Cool
b4Lz0R
thanks :X
Reply to topic    Frihost Forum Index -> Miscellaneous -> Tutorials

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.