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”]
<form>
Email:<br>
<input type="text" name="email" id="email">
</form>
<br>
<button id="subscribeBtn" type="button">Subscribe!</button>
[/code]

And the javascript part:

[code language=”javascript”] 
$(document).ready(function () {
$("#subscribeBtn").click(function () {
if($(‘#email’).val()){
$.ajax({
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(errorThrown);
}
});
}else{
alert("Please, insert an email");
}
});
});
[/code]

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

[code language=”php”]
<?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"
. "yourdomain.com/confirmEmail.php?email=" . $email
. "rn" . "rn" . "This is an automated message, please do not respond to it!";

$headers = ‘From: [email protected]’ . "rn" .
‘Reply-To: [email protected]’ . "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);
[/code]

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”]
<?php

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

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

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.

10 Comments

  1. richard April 18, 2015
    • gelasoft April 19, 2015
  2. Jason May 29, 2015
  3. Lisa King August 20, 2015
    • gelasoft August 20, 2015
    • gelasoft August 27, 2015
  4. Donald August 31, 2015
    • gelasoft September 1, 2015
      • Donald September 2, 2015
  5. Donald September 2, 2015

Leave a Reply