Simple way of making a mail script

Simple way of making a mail script
An example for a very simple way to make a mail script that supports HTML tags in PHP.

Lets first create our HTML page to test with this script, i’ve commented out every line so its easier for you to copy and paste it into your text-editor and edit it 😉

   Retrieving user his/her IP Adress  
$ip = getenv("REMOTE_ADDR");
<form action="mail.php">
<b>Nickname:</b><br />
<input type="text" name="nickname" value="Your name/nickname" /><br /><br />
<b>E-mail:</b><br />
<input type="text" name="email" value="Your E-mail" /><br /><br />
<b>Message:</b><br />
<textarea name="message" rows="5" cols="50"></textarea><br />
<input name="Submit" type="submit" value="Submit" />
<input name="Submit" type="reset" value="Reset" />

So now we’ve got our HTML page, lets start creating the code for mail.php

// The error message if there are one or more fields not filled in
$error = "There are some fields not filled in, <a href=\"javascript:history.go(-1)\">click here to go back</a>";

$nickname = intval($_GET['nickname']);
$message = intval($_GET['message']);
$email = intval($_GET['email']);

  Let's check if all fields are filled in!
  This is a very easy one, but it works perfect..
  Why doing it the hard way while it can be done easely huh ;)
if($nickname == "" || $message == "" || $email == ""){
  Lets echo that error! ;)
  If you want you can also use a JavaScript Application to show a warning-popup.
  echo $error;

  if after all everything is filled in correct, lets start sending the mail..
  Edit the variable's below to fit your needs.

$yourwebsite = ""; // Your website adress.
$recipientname = "Yourname"; // Whats your name ?!
$subject = "E-mail from yoursite";  // The subject of the mail thats being sended.
$recipient = "[email protected]"; // the recipient (in most case's..this is your e-mail!).

  The headers for the e-mail, no need to change it unless you know what you're doing.
$header = "From: $nickname <$email>\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$header .= "X-Priority: 3\r\n";
$header .= "X-MSMail-Priority: Normal\r\n";
$header .= "X-Mailer: PHP / ".phpversion()."\r\n";

  You can change the text/html below to whatever you wish to have.
  you can use HTML!
$content= "
Dear $recipientname, $nickname has sended you an e-mail from $yourwebsite.<br />
<b>Message:</b><br />$message
<br /><br /><b>User IP:</b> <u>$ip</u>
<hr noshade=\"true\" size=\"1\" color=\"#000000\" />
You can contact $nickname back at $email.";

  Lets send the e-mail by using the mail() function.
  No need to change below...this can be edited above!
mail($recipient, $content, $header);

  Message the user will see if the e-mail is succesfully sended,
  You can also send an e-mail to the sender,
  for this place the following line's under the above mail() function:

  $thanks_mail = "
  Dear $nickname, thank you for sending us an e-mail.<br />
  We will try to contact you back withing 24hrs on $email.<br />
  Make sure to not forget to bookmark us! $website.";
  mail($email, $thanks_mail, $header);


echo "Thanks! Your e-mail has been sended succesfull $nickname. We will contact you back on $email Within 24Hrs.";

I hope you enjoyed this tutorial/example 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top