PHP Mailer Script Step by Step
WEBINAR: On-demand Event
Replace Oracle with the NoSQL Engagement Database: Why and how leading companies are making the switch REGISTER >
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 ScriptFirst 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 = 'firstname.lastname@example.org';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: $email\r\n"; $headers .= "Content-type: text/html\r\n";The first $header tag enables the person's email address to show up properly. "\r\n" 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 ScriptFor 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.
You may download the entire script here.