Saturday, July 27, 2024

PHP Mailer Script Step by Step

If you look around there are many different mailer scripts out there. Some are really complicated and hard to understand whereas some are not. Follow me through this tutorial, as I break a good script down so it’s not only easy to understand, but easy to use!


We’re going to look at a script that I use that is simple to use and edit without extensive PHP knowledge. If you’re just starting out you may be wondering just what PHP is? PHP is a server side scripting language and can only run on a web server that has been configured to process PHP. This script was originally written by Nathaniel from Tutvid.com. I found it when I was looking for a mailer script I could use on my own site. In this article I will explain how the code works and how to install it on your site.

Why Do We Need a Mailer Script?

Before we get into explaining the code and how it works, let’s see why we need a mailer script to begin with. A mailer script allows your customers to send you emails from your websites. The script gathers all the information from the form it has been attached to, processes it and sends it to your email.

The Script

First we want to start with the opening PHP tag and then add the subject variable and the mailto variable. This will add the subject to the email and tell the script where to send the email.


<?php
$emailSubject = ‘Customer Has a Question!’;
$webMaster = ‘you@yourdomain.com’;

Next we want to let the server know what information to get from the form. You need to make sure that the names of the fields are the same as what you put here, otherwise it will not gather the information properly.

$nameField = $_POST [’name’];
$emailField = $_POST[’email’];
$questionField = $_POST [’question’];

The “<<<EOD” lets the server know that everything between that tag goes in the email body. However you arrange it here, it will show up the same way in the email.

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
Questions: $question <br>
EOD;
$headers = “From: $emailrn”;
$headers .= “Content-type: text/htmlrn”;

The first $header tag enables the person’s email address to show up properly. “rn” is a line break that lets the server know that the line has ended. The other $header tag lets the server know that this is both text and HTML; if this is not there then all the <br> tags and other coding will show up in the email.


The following code lets the server know that if everything is successful, send all the previous variables to the email client


$success = mail($webMaster, $emailSubject, $body, $headers);

Between the following EOD tags you would want to put you confirmation page.

$theResults = <<<EOD
EOD;

The following code allows what is written between the opening and closing EOD statements to be seen on the page. Lastly you want to put the closing PHP tag in and close the script.

echo “$theResults”;
?>

Installation of the Script

For this to work correctly we will need two php pages. One will contain the form and the other will contain the mailing script itself.


Script Page – You will want to take the code that I have shown you and put into a new php page and name it form_mailer.php. Remember to change $webmaster to your own email address. Also, if you are going to have another text field, remember to add it to the code. Lastly once you have closed the php code, copy the code for your form page (or from a contact page on your website), and add after the closing php tag.


Form Page – You will want to make a second php page then name it form.php. Then add a form field, and then put a table with 4 rows and 2 columns in it. Then add 3 text fields, one per line, with the label and id of “name, email, and question”. Make sure that the labels are in the first column. The last thing you need is to add a submit button, and set the form’s action as form_mailer.php, with the method set to POST.


You may download the entire script here.

Conclusion


Having an mail/contact form on your website is a important method of contact in the business world. It allows customers to quickly and efficiently send emails. I hope this mailer script and sample will help you with your website. In my next article I will show you how to protect your message from spam using a Captcha mechanism.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Popular Articles

Featured