How to code a simple mail script in PHP?
62Create mail.html
Here I am just displaying php codes as a mailing script.This script isn't just for practice, but will also applicable for real projects for web development. It enables you to place a fairly easy form for sending emails on any HTML page. This script displays you how to collect user input, perform form validation with PHP, and send an email.
Step I - Create the form page mail.html (or you can rename it, as you wish)...
mail.html code
<html> <head><title>Mail sender</title></head> <body> <form action="mail.php" method="POST"> <b>Email</b><br> <input type="text" name="email" size=40> <p><b>Subject</b><br> <input type="text" name="subject" size=40> <p><b>Message</b><br> <textarea cols=40 rows=10 name="message"></textarea> <p><input type="submit" value=" Send "> </form> </body> </html>
The above form have necessary text fields to be filled like Email, Subject, Message and the Submit button.
The line <form action="mail.php" method="POST">tells the browser which PHP file will process the form and which method to is used for sending data.
Create mail.php
Once the user fills up the form and hits the Submits, the mail.php file is called...
mail.php
<html>
<head><title>PHP Mail Sender</title></head>
<body>
<?php
/* All form fields are automatically passed to the PHP script through the array $HTTP_POST_VARS. */
$email = $HTTP_POST_VARS['email'];
$subject = $HTTP_POST_VARS['subject'];
$message = $HTTP_POST_VARS['message'];
/* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
echo "<h4>Invalid email address</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
} elseif ($subject == "") {
echo "<h4>No subject</h4>";
echo "<a href='javascript:history.back(1);'>Back</a>";
}
/* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
elseif (mail($email,$subject,$message)) {
echo "<h4>Thank you for sending email</h4>";
} else {
echo "<h4>Can't send email to $email</h4>";
}
?>
</body>
</html> As you may see, the script simply have one if ... elseif ... else statement. Initially, it validates the desired form fields. Note that PHP form validation is carried out on the server, after sending all of the data. Therefore, it is a smart idea to combine server-side form validation with PHP and client-side form validation with JavaScript to avoid unneeded data sending.
When the current email address is valid and subject isn't empty, the script sends the mail and displays the related message. Note the way the variable $email is included in to the output string.
You may also take this script to implement the safe "Contact Us" function in your website. Your visitors are able to send you a message, however your email address won't be shown on the page and spam bots, that parse pagessearching for potential email addresses, won't get it.
Just eliminate the Email text field from the form and replace the first line of the script with something such as...
$email = 'YourAddr@YourMail.com';
And, obviously, there's no need to validate the email address in this case.
Hire Php Developers/programmers
NetTrackers is offering opportunity to hire php developers and php programmers for any challenging web development and designing projects. Hire php developers and programmers at competitively low price than the market price.







