New: Advertise here on FlatColors!

Simple PHP email subscription form with email verification and confirmation – ideal for beginers and medium level developers

Simple PHP email subscription form in few minutes? Really?

The answer is “yes”!

For the sake of this tutorial you will need to include Jquery in in your <head> tag.

I will not include the database part in this PHP email subscription form tutorial, I will just show you how to verify the user’s email. The database part is a simple matter of INSERT and UPDATE. If you can’t handle it, just let me know in the comments.

Ok let’s get started and create a simple HTML form.

[code language=”html”]
<input type="text" name="email" id="email">
<button id="subscribeBtn" type="button">Subscribe!</button>

And the javascript part:

[code language=”javascript”] 
$(document).ready(function () {
$("#subscribeBtn").click(function () {
type: ‘POST’,
url: "sendMail.php", //The file w’re making a POST request to
dataType: ‘html’,
async: true,
data: {
email: $(‘#email’).val()
success: function (data) {
alert("PLease check andconfirm your email");
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Please, insert an email");

Now as you can see we need to create the sendMail.php file, which will send the verification email to the subscriber.

[code language=”php”]

$email = filter_input(INPUT_POST, ’email’); //Get the email submited from our ajax form

// HERE YOU HAVE TO INSERT THE EMAIL IN YOUR DATABASE. I suggest just 2 clumns ’email’ and ‘isVerified’ where email = $email and isVerified = false

/* Send the confirmation email */
$to = $email;
$subject = ‘Email confirmation’;
$message = "Hello!" . "rn" . "Please confirm your email by clicking on the link below: " . "rn" . "rn"
. "" . $email
. "rn" . "rn" . "This is an automated message, please do not respond to it!";

$headers = ‘From:’ . "rn" .
‘Reply-To:’ . "rn" .
‘Content-Type: text/plain; charset=UTF-8’ . "rn" .
‘MIME-Version: 1.0’ . "rn" .
‘Content-Transfer-Encoding: quoted-printable’ . "rn";

mail($to, $subject, $message, $headers);

The above code will send a verification email to the subscriber and in order to complete this simple PHP email subscription, we have to verify the user when he click on the link. If you take a look at the $message there is a link pointing to confirmEmail.php and it takes one parameter named email, where the value of this parameter is the user’s email adress, so here is how we can verify it when teh user click on our link

[code language=”php”]

$email = filter_input(INPUT_POST, ’email’);

// UPDATE YOUR DATABASE TABLE AND SET isActive = true WHERE email = $email

This will complete the email verification and you will be able to know at least, if the user’s email exists.
Again, if you’re facing problems with the SQL and database part of this simple php email subscription form, feel free to leave a comment below.