php Subscribe Form

How to create a Subscribe Mail Form with php

This article gives information about how to create a simple subscribe web form with php. Such a script is often called mail form or form mailer.
Please note: There is no manual coding required when using Web Form Builder or our newsletter software MailList Controller.

 

 

HTML and php Code for a simple Subscribe Form

If you have PHP installed on your web server use the following code for a simple php subscribe (mail) form.

 

How does it work?

 

Add the following code to your HTML page for a simple form with text input fields for "Name" and "Email":

<form method="POST" action="enter the URL to your PHP script here">
<p>Name: <input type="text" name="Name" size="20"></p>
<p>Email: <input type="text" name="Email" size="20"></p>
<p><input type="submit" value="Submit" name="Submit"></p>
</form>

 

Create a new php file (e.g. subscribe.php) with any texteditor (e.g. notepad) and place it on your web server.
Please update the URL in the HTML form. (e.g. subscribe.php)

<?php

## CONFIG ##

# LIST EMAIL ADDRESS
$recipient = "enter the lists email address here";

# SUBJECT (Subscribe/Remove)
$subject = "Subscribe";

# RESULT PAGE
$location = "enter the URL of the result page here";

## FORM VALUES ##

# SENDER - WE ALSO USE THE RECIPIENT AS SENDER IN THIS SAMPLE
# DON'T INCLUDE UNFILTERED USER INPUT IN THE MAIL HEADER!
$sender = $recipient;

# MAIL BODY
$body .= "Name: ".$_REQUEST['Name']." \n";
$body .= "Email: ".$_REQUEST['Email']." \n";
# add more fields here if required

## SEND MESSGAE ##

mail( $recipient, $subject, $body, "From: $sender" ) or die ("Mail could not be sent.");

## SHOW RESULT PAGE ##

header( "Location: $location" );
?>

Now upload both files to your web server, open the .html file, fill out the form and click on "Submit".
Important: The php script will not work on your local computer - you need to upload it to your web server.

Please take a look at our software Arclab® Web Form Builder if you are looking for a more sophisticated form, e.g. use and verify the user's email address as sender, add multiple pages to your form, add a captcha, add required fields, etc.