Home : Advice : <?PHP ?> Tutorials : Sending Mail


In this tutorial you will learn how to send mail using PHP. All PHP code is commented to see how things work.

PHP is highlighted in blue

<?php

// if form is submitted then send mail
if ($submit)
{

// Where you want mail to be sent. This is the only thing you need to change for the script to work.
$email = "you@youremail.com";

// Senders email will go here
$from = "From:$email2\r\n";

// Message sent
$mesg = "Name: $name \n\n Message: $message \n";

// Call mail function
mail($email, $subject, $mesg, $from);

// Confirm that message has been sent
echo 'Your message has been sent. We will get back to you as soon as possible.';

}
// else if form not submitted show the HTML form
else
{
?>


<table width="500" border="0" align="center" cellpadding="5" cellspacing="0">
<form method="post" action="" name="submit">
<tr>
<td width="145" align="right"><font face="Geneva, Arial, Helvetica, sans-serif">Name</font></td>
<td width="340"><input name="name" type="text" class=formtext id="name" size="30"></td>
</tr>
<tr>
<td align="right"><font face="Geneva, Arial, Helvetica, sans-serif">Email</font></td>
<td><input name="email2" type="text" class=formtext id="email2" size="30"></td>
</tr>
<tr>
<td align="right" valign="top"><font face="Geneva, Arial, Helvetica, sans-serif">Subject</font></td>
<td> <input name="subject" type="text" class=formtext id="subject" size="30"></td>
</tr>
<tr>
<td align="right" valign="top"><font face="Geneva, Arial, Helvetica, sans-serif">Message</font></td>
<td><textarea name="message" cols="30" rows="6" id="message" class=formtext></textarea></td>
</tr>
<tr>
<td> </td>
<td><input class=button type="submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>

<?php
// end else
}
?>

Copy and paste the code above into a php file to test it.

 


Copyright 2004 - 2005 FREEWEBHOSTINGADVICE.COM Contact | Privacy Policy & Legal