Simple PHP mail script

Simple PHP mail scriptSimple PHP mail script
Simple php mail script is not only educational, but also applicable for practical Web development. It allows you to place a simple form for sending emails on any HTML page. The script shows you how to gather user input, perform form validation with PHP, and send an email.

First, make the form page mail.html (you may call it whatever you like)…

<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 form contains the necessary text fields Email, Subject, Message, and the Send button. The line

tells the browser which PHP file will process the form and what method to use for sending data.

When the user fills in the form and hits the Send button, the mail.php file is called…

<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>

Leave a Reply