|
|
 |  | | Tutorials | | | Using MySQL with PHP Beginners Guide. |
|---|
Chris McIlroy http://www.c4sites.co.uk/ http://www.c4sites.co.uk/tutorials.php | | | This is a simple PHP mailer script. During this tutorial I assume you have a
basic knowledge of PHP, a PHP Editor, FTP Access to PHP Hosting.
The function used to send an email through PHP is mail().
The first thing we need to do is create a HTML form so we can gather the
information we need to send the email.
Open your PHP editor and paste this piece of code:
<?
if (!$_REQUEST["func"]) {
echo "
<form action='$PHP_SELF?func=email' method='POST'>
To Email:<BR><input name='email_to' size='50' MAXLENGTH='50'><BR>
From Email:<BR><input name='email_from' size='50'
MAXLENGTH='50'><BR>
Your Name:<BR><input name='name_from' size='50'
MAXLENGTH='50'><BR>
Subject:<BR><input name='subject' size='50' MAXLENGTH='50'><BR>
Message:<BR><TEXTAREA COLS='40' ROWS='8' WRAP='hard'
NAME='message'></TEXTAREA><BR>
<input type='submit' value='Send Email'>
</form>
";
}
?>
Now that we have our form we need to add the submit, error checking and mail
sending code:
<?
if ($_REQUEST["func"] == "email") {
if (!$_REQUEST["email_to"]) { echo "You must define an email address."; exit; }
if (!$_REQUEST["email_from"]) { echo "You must define an email title."; exit; }
if (!$_REQUEST["name_from"]) { echo "You must define your name."; exit; }
if (!$_REQUEST["subject"]) { echo "You must define a message."; exit; }
# Set Email Headers
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .="From: ".$_REQUEST["name_from"]."
<".$_REQUEST["email_from"].">\r\n";
$messagen="<html><body><pre>".$_REQUEST["message"]."</pre></body></html>";
# Send Mail
if(mail($_REQUEST["email_to"],$_REQUEST["subject"],$messagen, $headers)) {
echo "Email Sent.";
}
}
if (!$_REQUEST["func"]) {
echo "
<form action='$PHP_SELF?func=email' method='POST'>
To Email:<BR><input name='email_to' size='50' MAXLENGTH='50'><BR>
From Email:<BR><input name='email_from' size='50'
MAXLENGTH='50'><BR>
Your Name:<BR><input name='name_from' size='50'
MAXLENGTH='50'><BR>
Subject:<BR><input name='subject' size='50' MAXLENGTH='50'><BR>
Message:<BR><TEXTAREA COLS='40' ROWS='8' WRAP='hard'
NAME='message'></TEXTAREA><BR>
<input type='submit' value='Send Email'>
</form>
";
}
?>
Now simply save as your_script_name.php and test the script by completing the
form then hit send email and your done!
I hope you find this useful. | | | | Click HERE to add an Tutorial here for FREE |
|
|
|